validate.js.flow 3.8 KB

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