UNPKG

848 BJavaScriptView Raw
1import devAssert from "../jsutils/devAssert.mjs";
2import { GraphQLError } from "../error/GraphQLError.mjs";
3var NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
4/**
5 * Upholds the spec rules about naming.
6 */
7
8export function assertValidName(name) {
9 var error = isValidNameError(name);
10
11 if (error) {
12 throw error;
13 }
14
15 return name;
16}
17/**
18 * Returns an Error if a name is invalid.
19 */
20
21export function isValidNameError(name) {
22 typeof name === 'string' || devAssert(0, 'Expected name to be a string.');
23
24 if (name.length > 1 && name[0] === '_' && name[1] === '_') {
25 return new GraphQLError("Name \"".concat(name, "\" must not begin with \"__\", which is reserved by GraphQL introspection."));
26 }
27
28 if (!NAME_RX.test(name)) {
29 return new GraphQLError("Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but \"".concat(name, "\" does not."));
30 }
31}