validate.mjs 3.6 KB

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