validate.js 4.0 KB

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