validate.js.flow 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @flow strict
  2. import devAssert from '../jsutils/devAssert';
  3. import { GraphQLError } from '../error/GraphQLError';
  4. import { type DocumentNode } from '../language/ast';
  5. import { visit, visitInParallel, visitWithTypeInfo } from '../language/visitor';
  6. import { type GraphQLSchema } from '../type/schema';
  7. import { assertValidSchema } from '../type/validate';
  8. import { TypeInfo } from '../utilities/TypeInfo';
  9. import { specifiedRules, specifiedSDLRules } from './specifiedRules';
  10. import {
  11. type SDLValidationRule,
  12. type ValidationRule,
  13. SDLValidationContext,
  14. ValidationContext,
  15. } from './ValidationContext';
  16. export const ABORT_VALIDATION = Object.freeze({});
  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. export function validate(
  34. schema: GraphQLSchema,
  35. documentAST: DocumentNode,
  36. rules?: $ReadOnlyArray<ValidationRule> = specifiedRules,
  37. typeInfo?: TypeInfo = new TypeInfo(schema),
  38. options?: {| maxErrors?: number |},
  39. ): $ReadOnlyArray<GraphQLError> {
  40. devAssert(documentAST, 'Must provide document');
  41. // If the schema used for validation is invalid, throw an error.
  42. assertValidSchema(schema);
  43. const abortObj = Object.freeze({});
  44. const errors = [];
  45. const maxErrors = options && options.maxErrors;
  46. const context = new ValidationContext(
  47. schema,
  48. documentAST,
  49. typeInfo,
  50. error => {
  51. if (maxErrors != null && errors.length >= maxErrors) {
  52. errors.push(
  53. new GraphQLError(
  54. 'Too many validation errors, error limit reached. Validation aborted.',
  55. ),
  56. );
  57. throw abortObj;
  58. }
  59. errors.push(error);
  60. },
  61. );
  62. // This uses a specialized visitor which runs multiple visitors in parallel,
  63. // while maintaining the visitor skip and break API.
  64. const visitor = visitInParallel(rules.map(rule => rule(context)));
  65. // Visit the whole document with each instance of all provided rules.
  66. try {
  67. visit(documentAST, visitWithTypeInfo(typeInfo, visitor));
  68. } catch (e) {
  69. if (e !== abortObj) {
  70. throw e;
  71. }
  72. }
  73. return errors;
  74. }
  75. // @internal
  76. export function validateSDL(
  77. documentAST: DocumentNode,
  78. schemaToExtend?: ?GraphQLSchema,
  79. rules?: $ReadOnlyArray<SDLValidationRule> = specifiedSDLRules,
  80. ): $ReadOnlyArray<GraphQLError> {
  81. const errors = [];
  82. const context = new SDLValidationContext(
  83. documentAST,
  84. schemaToExtend,
  85. error => {
  86. errors.push(error);
  87. },
  88. );
  89. const visitors = rules.map(rule => rule(context));
  90. visit(documentAST, visitInParallel(visitors));
  91. return errors;
  92. }
  93. /**
  94. * Utility function which asserts a SDL document is valid by throwing an error
  95. * if it is invalid.
  96. *
  97. * @internal
  98. */
  99. export function assertValidSDL(documentAST: DocumentNode): void {
  100. const errors = validateSDL(documentAST);
  101. if (errors.length !== 0) {
  102. throw new Error(errors.map(error => error.message).join('\n\n'));
  103. }
  104. }
  105. /**
  106. * Utility function which asserts a SDL document is valid by throwing an error
  107. * if it is invalid.
  108. *
  109. * @internal
  110. */
  111. export function assertValidSDLExtension(
  112. documentAST: DocumentNode,
  113. schema: GraphQLSchema,
  114. ): void {
  115. const errors = validateSDL(documentAST, schema);
  116. if (errors.length !== 0) {
  117. throw new Error(errors.map(error => error.message).join('\n\n'));
  118. }
  119. }