validate.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.validate = validate;
  6. exports.validateSDL = validateSDL;
  7. exports.assertValidSDL = assertValidSDL;
  8. exports.assertValidSDLExtension = assertValidSDLExtension;
  9. exports.ABORT_VALIDATION = void 0;
  10. var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
  11. var _GraphQLError = require("../error/GraphQLError");
  12. var _visitor = require("../language/visitor");
  13. var _validate = require("../type/validate");
  14. var _TypeInfo = require("../utilities/TypeInfo");
  15. var _specifiedRules = require("./specifiedRules");
  16. var _ValidationContext = require("./ValidationContext");
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. var ABORT_VALIDATION = Object.freeze({});
  19. /**
  20. * Implements the "Validation" section of the spec.
  21. *
  22. * Validation runs synchronously, returning an array of encountered errors, or
  23. * an empty array if no errors were encountered and the document is valid.
  24. *
  25. * A list of specific validation rules may be provided. If not provided, the
  26. * default list of rules defined by the GraphQL specification will be used.
  27. *
  28. * Each validation rules is a function which returns a visitor
  29. * (see the language/visitor API). Visitor methods are expected to return
  30. * GraphQLErrors, or Arrays of GraphQLErrors when invalid.
  31. *
  32. * Optionally a custom TypeInfo instance may be provided. If not provided, one
  33. * will be created from the provided schema.
  34. */
  35. exports.ABORT_VALIDATION = ABORT_VALIDATION;
  36. function validate(schema, documentAST) {
  37. var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _specifiedRules.specifiedRules;
  38. var typeInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new _TypeInfo.TypeInfo(schema);
  39. var options = arguments.length > 4 ? arguments[4] : undefined;
  40. documentAST || (0, _devAssert.default)(0, 'Must provide document'); // If the schema used for validation is invalid, throw an error.
  41. (0, _validate.assertValidSchema)(schema);
  42. var abortObj = Object.freeze({});
  43. var errors = [];
  44. var maxErrors = options && options.maxErrors;
  45. var context = new _ValidationContext.ValidationContext(schema, documentAST, typeInfo, function (error) {
  46. if (maxErrors != null && errors.length >= maxErrors) {
  47. errors.push(new _GraphQLError.GraphQLError('Too many validation errors, error limit reached. Validation aborted.'));
  48. throw abortObj;
  49. }
  50. errors.push(error);
  51. }); // This uses a specialized visitor which runs multiple visitors in parallel,
  52. // while maintaining the visitor skip and break API.
  53. var visitor = (0, _visitor.visitInParallel)(rules.map(function (rule) {
  54. return rule(context);
  55. })); // Visit the whole document with each instance of all provided rules.
  56. try {
  57. (0, _visitor.visit)(documentAST, (0, _visitor.visitWithTypeInfo)(typeInfo, visitor));
  58. } catch (e) {
  59. if (e !== abortObj) {
  60. throw e;
  61. }
  62. }
  63. return errors;
  64. } // @internal
  65. function validateSDL(documentAST, schemaToExtend) {
  66. var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _specifiedRules.specifiedSDLRules;
  67. var errors = [];
  68. var context = new _ValidationContext.SDLValidationContext(documentAST, schemaToExtend, function (error) {
  69. errors.push(error);
  70. });
  71. var visitors = rules.map(function (rule) {
  72. return rule(context);
  73. });
  74. (0, _visitor.visit)(documentAST, (0, _visitor.visitInParallel)(visitors));
  75. return errors;
  76. }
  77. /**
  78. * Utility function which asserts a SDL document is valid by throwing an error
  79. * if it is invalid.
  80. *
  81. * @internal
  82. */
  83. function assertValidSDL(documentAST) {
  84. var errors = validateSDL(documentAST);
  85. if (errors.length !== 0) {
  86. throw new Error(errors.map(function (error) {
  87. return error.message;
  88. }).join('\n\n'));
  89. }
  90. }
  91. /**
  92. * Utility function which asserts a SDL document is valid by throwing an error
  93. * if it is invalid.
  94. *
  95. * @internal
  96. */
  97. function assertValidSDLExtension(documentAST, schema) {
  98. var errors = validateSDL(documentAST, schema);
  99. if (errors.length !== 0) {
  100. throw new Error(errors.map(function (error) {
  101. return error.message;
  102. }).join('\n\n'));
  103. }
  104. }