1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.validate = validate;
|
7 | exports.validateSDL = validateSDL;
|
8 | exports.assertValidSDL = assertValidSDL;
|
9 | exports.assertValidSDLExtension = assertValidSDLExtension;
|
10 | exports.ABORT_VALIDATION = void 0;
|
11 |
|
12 | var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
|
13 |
|
14 | var _GraphQLError = require("../error/GraphQLError");
|
15 |
|
16 | var _visitor = require("../language/visitor");
|
17 |
|
18 | var _validate = require("../type/validate");
|
19 |
|
20 | var _TypeInfo = require("../utilities/TypeInfo");
|
21 |
|
22 | var _specifiedRules = require("./specifiedRules");
|
23 |
|
24 | var _ValidationContext = require("./ValidationContext");
|
25 |
|
26 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
27 |
|
28 | var ABORT_VALIDATION = Object.freeze({});
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | exports.ABORT_VALIDATION = ABORT_VALIDATION;
|
47 |
|
48 | function validate(schema, documentAST) {
|
49 | var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _specifiedRules.specifiedRules;
|
50 | var typeInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new _TypeInfo.TypeInfo(schema);
|
51 | var options = arguments.length > 4 ? arguments[4] : undefined;
|
52 | documentAST || (0, _devAssert.default)(0, 'Must provide document');
|
53 |
|
54 | (0, _validate.assertValidSchema)(schema);
|
55 | var abortObj = Object.freeze({});
|
56 | var errors = [];
|
57 | var maxErrors = options && options.maxErrors;
|
58 | var context = new _ValidationContext.ValidationContext(schema, documentAST, typeInfo, function (error) {
|
59 | if (maxErrors != null && errors.length >= maxErrors) {
|
60 | errors.push(new _GraphQLError.GraphQLError('Too many validation errors, error limit reached. Validation aborted.'));
|
61 | throw abortObj;
|
62 | }
|
63 |
|
64 | errors.push(error);
|
65 | });
|
66 |
|
67 |
|
68 | var visitor = (0, _visitor.visitInParallel)(rules.map(function (rule) {
|
69 | return rule(context);
|
70 | }));
|
71 |
|
72 | try {
|
73 | (0, _visitor.visit)(documentAST, (0, _visitor.visitWithTypeInfo)(typeInfo, visitor));
|
74 | } catch (e) {
|
75 | if (e !== abortObj) {
|
76 | throw e;
|
77 | }
|
78 | }
|
79 |
|
80 | return errors;
|
81 | }
|
82 |
|
83 |
|
84 | function validateSDL(documentAST, schemaToExtend) {
|
85 | var rules = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _specifiedRules.specifiedSDLRules;
|
86 | var errors = [];
|
87 | var context = new _ValidationContext.SDLValidationContext(documentAST, schemaToExtend, function (error) {
|
88 | errors.push(error);
|
89 | });
|
90 | var visitors = rules.map(function (rule) {
|
91 | return rule(context);
|
92 | });
|
93 | (0, _visitor.visit)(documentAST, (0, _visitor.visitInParallel)(visitors));
|
94 | return errors;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | function assertValidSDL(documentAST) {
|
105 | var errors = validateSDL(documentAST);
|
106 |
|
107 | if (errors.length !== 0) {
|
108 | throw new Error(errors.map(function (error) {
|
109 | return error.message;
|
110 | }).join('\n\n'));
|
111 | }
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function assertValidSDLExtension(documentAST, schema) {
|
122 | var errors = validateSDL(documentAST, schema);
|
123 |
|
124 | if (errors.length !== 0) {
|
125 | throw new Error(errors.map(function (error) {
|
126 | return error.message;
|
127 | }).join('\n\n'));
|
128 | }
|
129 | }
|