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 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export 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');
|
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 | });
|
44 |
|
45 |
|
46 | var visitor = visitInParallel(rules.map(function (rule) {
|
47 | return rule(context);
|
48 | }));
|
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 | }
|
60 |
|
61 | export 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 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | export 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 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | export 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 | }
|