UNPKG

1.05 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9import { GraphQLError } from '../error/GraphQLError';
10import invariant from '../jsutils/invariant';
11var NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
12/**
13 * Upholds the spec rules about naming.
14 */
15
16export function assertValidName(name) {
17 var error = isValidNameError(name);
18
19 if (error) {
20 throw error;
21 }
22
23 return name;
24}
25/**
26 * Returns an Error if a name is invalid.
27 */
28
29export function isValidNameError(name, node) {
30 !(typeof name === 'string') ? invariant(0, 'Expected string') : void 0;
31
32 if (name.length > 1 && name[0] === '_' && name[1] === '_') {
33 return new GraphQLError("Name \"".concat(name, "\" must not begin with \"__\", which is reserved by ") + 'GraphQL introspection.', node);
34 }
35
36 if (!NAME_RX.test(name)) {
37 return new GraphQLError("Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but \"".concat(name, "\" does not."), node);
38 }
39}
\No newline at end of file