swc/crates/swc_bundler/tests/.cache/deno/068c2d45fb9edc362a3af0a37718179290fbd722.ts
2021-11-09 20:42:49 +09:00

34 lines
909 B
TypeScript

// Loaded from https://deno.land/x/graphql_deno@v15.0.0/lib/utilities/assertValidName.js
import devAssert from '../jsutils/devAssert.js';
import { GraphQLError } from '../error/GraphQLError.js';
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
/**
* Upholds the spec rules about naming.
*/
export function assertValidName(name) {
const error = isValidNameError(name);
if (error) {
throw error;
}
return name;
}
/**
* Returns an Error if a name is invalid.
*/
export function isValidNameError(name) {
devAssert(typeof name === 'string', 'Expected name to be a string.');
if (name.length > 1 && name[0] === '_' && name[1] === '_') {
return new GraphQLError(`Name "${name}" must not begin with "__", which is reserved by GraphQL introspection.`);
}
if (!NAME_RX.test(name)) {
return new GraphQLError(`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "${name}" does not.`);
}
}