UNPKG

4.24 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validate = validate;
7exports.validateSDL = validateSDL;
8exports.assertValidSDL = assertValidSDL;
9exports.assertValidSDLExtension = assertValidSDLExtension;
10exports.ABORT_VALIDATION = void 0;
11
12var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
13
14var _GraphQLError = require("../error/GraphQLError");
15
16var _visitor = require("../language/visitor");
17
18var _validate = require("../type/validate");
19
20var _TypeInfo = require("../utilities/TypeInfo");
21
22var _specifiedRules = require("./specifiedRules");
23
24var _ValidationContext = require("./ValidationContext");
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28var ABORT_VALIDATION = Object.freeze({});
29/**
30 * Implements the "Validation" section of the spec.
31 *
32 * Validation runs synchronously, returning an array of encountered errors, or
33 * an empty array if no errors were encountered and the document is valid.
34 *
35 * A list of specific validation rules may be provided. If not provided, the
36 * default list of rules defined by the GraphQL specification will be used.
37 *
38 * Each validation rules is a function which returns a visitor
39 * (see the language/visitor API). Visitor methods are expected to return
40 * GraphQLErrors, or Arrays of GraphQLErrors when invalid.
41 *
42 * Optionally a custom TypeInfo instance may be provided. If not provided, one
43 * will be created from the provided schema.
44 */
45
46exports.ABORT_VALIDATION = ABORT_VALIDATION;
47
48function 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'); // If the schema used for validation is invalid, throw an error.
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 }); // This uses a specialized visitor which runs multiple visitors in parallel,
66 // while maintaining the visitor skip and break API.
67
68 var visitor = (0, _visitor.visitInParallel)(rules.map(function (rule) {
69 return rule(context);
70 })); // Visit the whole document with each instance of all provided rules.
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} // @internal
82
83
84function 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 * Utility function which asserts a SDL document is valid by throwing an error
98 * if it is invalid.
99 *
100 * @internal
101 */
102
103
104function 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 * Utility function which asserts a SDL document is valid by throwing an error
115 * if it is invalid.
116 *
117 * @internal
118 */
119
120
121function 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}