validate.js 4.2 KB

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