assertValidName.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.assertValidName = assertValidName;
  6. exports.isValidNameError = isValidNameError;
  7. var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
  8. var _GraphQLError = require("../error/GraphQLError");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
  11. /**
  12. * Upholds the spec rules about naming.
  13. */
  14. function assertValidName(name) {
  15. var error = isValidNameError(name);
  16. if (error) {
  17. throw error;
  18. }
  19. return name;
  20. }
  21. /**
  22. * Returns an Error if a name is invalid.
  23. */
  24. function isValidNameError(name, node) {
  25. typeof name === 'string' || (0, _devAssert.default)(0, 'Expected string');
  26. if (name.length > 1 && name[0] === '_' && name[1] === '_') {
  27. return new _GraphQLError.GraphQLError("Name \"".concat(name, "\" must not begin with \"__\", which is reserved by GraphQL introspection."), node);
  28. }
  29. if (!NAME_RX.test(name)) {
  30. return new _GraphQLError.GraphQLError("Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but \"".concat(name, "\" does not."), node);
  31. }
  32. }