validate.mjs 3.8 KB

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