assertValidName.mjs 844 B

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