UNPKG

3.7 kBJavaScriptView Raw
1import devAssert from '../jsutils/devAssert';
2import { GraphQLError } from '../error/GraphQLError';
3import { visit, visitInParallel, visitWithTypeInfo } from '../language/visitor';
4import { assertValidSchema } from '../type/validate';
5import { TypeInfo } from '../utilities/TypeInfo';
6import { specifiedRules, specifiedSDLRules } from './specifiedRules';
7import { SDLValidationContext, ValidationContext } from './ValidationContext';
8export 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
26export function validate(schema, documentAST) {
27 var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : specifiedRules;
28 var typeInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new TypeInfo(schema);
29 var options = arguments.length > 4 ? arguments[4] : undefined;
30 documentAST || devAssert(0, 'Must provide document'); // If the schema used for validation is invalid, throw an error.
31
32 assertValidSchema(schema);
33 var abortObj = Object.freeze({});
34 var errors = [];
35 var maxErrors = options && options.maxErrors;
36 var context = new ValidationContext(schema, documentAST, typeInfo, function (error) {
37 if (maxErrors != null && errors.length >= maxErrors) {
38 errors.push(new GraphQLError('Too many validation errors, error limit reached. Validation aborted.'));
39 throw abortObj;
40 }
41
42 errors.push(error);
43 }); // This uses a specialized visitor which runs multiple visitors in parallel,
44 // while maintaining the visitor skip and break API.
45
46 var visitor = visitInParallel(rules.map(function (rule) {
47 return rule(context);
48 })); // Visit the whole document with each instance of all provided rules.
49
50 try {
51 visit(documentAST, visitWithTypeInfo(typeInfo, visitor));
52 } catch (e) {
53 if (e !== abortObj) {
54 throw e;
55 }
56 }
57
58 return errors;
59} // @internal
60
61export function validateSDL(documentAST, schemaToExtend) {
62 var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : specifiedSDLRules;
63 var errors = [];
64 var context = new SDLValidationContext(documentAST, schemaToExtend, function (error) {
65 errors.push(error);
66 });
67 var visitors = rules.map(function (rule) {
68 return rule(context);
69 });
70 visit(documentAST, visitInParallel(visitors));
71 return errors;
72}
73/**
74 * Utility function which asserts a SDL document is valid by throwing an error
75 * if it is invalid.
76 *
77 * @internal
78 */
79
80export function assertValidSDL(documentAST) {
81 var errors = validateSDL(documentAST);
82
83 if (errors.length !== 0) {
84 throw new Error(errors.map(function (error) {
85 return error.message;
86 }).join('\n\n'));
87 }
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
96export function assertValidSDLExtension(documentAST, schema) {
97 var errors = validateSDL(documentAST, schema);
98
99 if (errors.length !== 0) {
100 throw new Error(errors.map(function (error) {
101 return error.message;
102 }).join('\n\n'));
103 }
104}