validate.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 { SDLValidationContext, ValidationContext } from "./ValidationContext.mjs";
  8. /**
  9. * Implements the "Validation" section of the spec.
  10. *
  11. * Validation runs synchronously, returning an array of encountered errors, or
  12. * an empty array if no errors were encountered and the document is valid.
  13. *
  14. * A list of specific validation rules may be provided. If not provided, the
  15. * default list of rules defined by the GraphQL specification will be used.
  16. *
  17. * Each validation rules is a function which returns a visitor
  18. * (see the language/visitor API). Visitor methods are expected to return
  19. * GraphQLErrors, or Arrays of GraphQLErrors when invalid.
  20. *
  21. * Optionally a custom TypeInfo instance may be provided. If not provided, one
  22. * will be created from the provided schema.
  23. */
  24. export function validate(schema, documentAST) {
  25. var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : specifiedRules;
  26. var typeInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new TypeInfo(schema);
  27. var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
  28. maxErrors: undefined
  29. };
  30. documentAST || devAssert(0, 'Must provide document.'); // If the schema used for validation is invalid, throw an error.
  31. assertValidSchema(schema);
  32. var abortObj = Object.freeze({});
  33. var errors = [];
  34. var context = new ValidationContext(schema, documentAST, typeInfo, function (error) {
  35. if (options.maxErrors != null && errors.length >= options.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. }
  54. /**
  55. * @internal
  56. */
  57. export function validateSDL(documentAST, schemaToExtend) {
  58. var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : specifiedSDLRules;
  59. var errors = [];
  60. var context = new SDLValidationContext(documentAST, schemaToExtend, function (error) {
  61. errors.push(error);
  62. });
  63. var visitors = rules.map(function (rule) {
  64. return rule(context);
  65. });
  66. visit(documentAST, visitInParallel(visitors));
  67. return errors;
  68. }
  69. /**
  70. * Utility function which asserts a SDL document is valid by throwing an error
  71. * if it is invalid.
  72. *
  73. * @internal
  74. */
  75. export function assertValidSDL(documentAST) {
  76. var errors = validateSDL(documentAST);
  77. if (errors.length !== 0) {
  78. throw new Error(errors.map(function (error) {
  79. return error.message;
  80. }).join('\n\n'));
  81. }
  82. }
  83. /**
  84. * Utility function which asserts a SDL document is valid by throwing an error
  85. * if it is invalid.
  86. *
  87. * @internal
  88. */
  89. export function assertValidSDLExtension(documentAST, schema) {
  90. var errors = validateSDL(documentAST, schema);
  91. if (errors.length !== 0) {
  92. throw new Error(errors.map(function (error) {
  93. return error.message;
  94. }).join('\n\n'));
  95. }
  96. }