1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.validate = validate;
|
7 | Object.defineProperty(exports, "ValidationError", {
|
8 | enumerable: true,
|
9 | get: function () {
|
10 | return _ValidationError.default;
|
11 | }
|
12 | });
|
13 |
|
14 | var _absolutePath = _interopRequireDefault(require("./keywords/absolutePath"));
|
15 |
|
16 | var _ValidationError = _interopRequireDefault(require("./ValidationError"));
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 |
|
21 | const Ajv = require('ajv');
|
22 |
|
23 | const ajvKeywords = require('ajv-keywords');
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | const ajv = new Ajv({
|
60 | allErrors: true,
|
61 | verbose: true,
|
62 | $data: true
|
63 | });
|
64 | ajvKeywords(ajv, ['instanceof', 'formatMinimum', 'formatMaximum', 'patternRequired']);
|
65 |
|
66 | (0, _absolutePath.default)(ajv);
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function validate(schema, options, configuration) {
|
75 | let errors = [];
|
76 |
|
77 | if (Array.isArray(options)) {
|
78 | errors = Array.from(options, nestedOptions => validateObject(schema, nestedOptions));
|
79 | errors.forEach((list, idx) => {
|
80 | const applyPrefix =
|
81 | |
82 |
|
83 |
|
84 | error => {
|
85 |
|
86 | error.dataPath = `[${idx}]${error.dataPath}`;
|
87 |
|
88 | if (error.children) {
|
89 | error.children.forEach(applyPrefix);
|
90 | }
|
91 | };
|
92 |
|
93 | list.forEach(applyPrefix);
|
94 | });
|
95 | errors = errors.reduce((arr, items) => {
|
96 | arr.push(...items);
|
97 | return arr;
|
98 | }, []);
|
99 | } else {
|
100 | errors = validateObject(schema, options);
|
101 | }
|
102 |
|
103 | if (errors.length > 0) {
|
104 | throw new _ValidationError.default(errors, schema, configuration);
|
105 | }
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function validateObject(schema, options) {
|
115 | const compiledSchema = ajv.compile(schema);
|
116 | const valid = compiledSchema(options);
|
117 | if (valid) return [];
|
118 | return compiledSchema.errors ? filterErrors(compiledSchema.errors) : [];
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | function filterErrors(errors) {
|
127 |
|
128 | let newErrors = [];
|
129 |
|
130 | for (const error of
|
131 |
|
132 | errors) {
|
133 | const {
|
134 | dataPath
|
135 | } = error;
|
136 |
|
137 |
|
138 | let children = [];
|
139 | newErrors = newErrors.filter(oldError => {
|
140 | if (oldError.dataPath.includes(dataPath)) {
|
141 | if (oldError.children) {
|
142 | children = children.concat(oldError.children.slice(0));
|
143 | }
|
144 |
|
145 |
|
146 | oldError.children = undefined;
|
147 | children.push(oldError);
|
148 | return false;
|
149 | }
|
150 |
|
151 | return true;
|
152 | });
|
153 |
|
154 | if (children.length) {
|
155 | error.children = children;
|
156 | }
|
157 |
|
158 | newErrors.push(error);
|
159 | }
|
160 |
|
161 | return newErrors;
|
162 | } |
\ | No newline at end of file |