z-schema 6.0.2 → 7.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +154 -134
- package/bin/z-schema +128 -124
- package/cjs/ZSchema.d.ts +227 -0
- package/cjs/ZSchema.js +13785 -0
- package/dist/Errors.js +50 -0
- package/dist/FormatValidators.js +136 -0
- package/{src → dist}/JsonValidation.js +184 -213
- package/dist/Report.js +220 -0
- package/{src → dist}/SchemaCache.js +67 -82
- package/{src → dist}/SchemaCompilation.js +89 -129
- package/dist/SchemaValidation.js +631 -0
- package/{src → dist}/Utils.js +96 -104
- package/dist/ZSchema.js +365 -0
- package/dist/index.js +2 -0
- package/dist/schemas/hyper-schema.json +156 -0
- package/dist/schemas/schema.json +151 -0
- package/dist/types/Errors.d.ts +44 -0
- package/dist/types/FormatValidators.d.ts +12 -0
- package/dist/types/JsonValidation.d.ts +37 -0
- package/dist/types/Report.d.ts +87 -0
- package/dist/types/SchemaCache.d.ts +26 -0
- package/dist/types/SchemaCompilation.d.ts +1 -0
- package/dist/types/SchemaValidation.d.ts +6 -0
- package/dist/types/Utils.d.ts +64 -0
- package/dist/types/ZSchema.d.ts +97 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +59 -45
- package/src/Errors.ts +56 -0
- package/src/FormatValidators.ts +136 -0
- package/src/JsonValidation.ts +624 -0
- package/src/Report.ts +337 -0
- package/src/SchemaCache.ts +189 -0
- package/src/SchemaCompilation.ts +293 -0
- package/src/SchemaValidation.ts +629 -0
- package/src/Utils.ts +286 -0
- package/src/ZSchema.ts +467 -0
- package/src/index.ts +3 -0
- package/src/schemas/_ +0 -0
- package/umd/ZSchema.js +13791 -0
- package/umd/ZSchema.min.js +1 -0
- package/dist/ZSchema-browser-min.js +0 -2
- package/dist/ZSchema-browser-min.js.map +0 -1
- package/dist/ZSchema-browser-test.js +0 -32247
- package/dist/ZSchema-browser.js +0 -12745
- package/index.d.ts +0 -175
- package/src/Errors.js +0 -60
- package/src/FormatValidators.js +0 -129
- package/src/Polyfills.js +0 -16
- package/src/Report.js +0 -299
- package/src/SchemaValidation.js +0 -619
- package/src/ZSchema.js +0 -409
package/src/ZSchema.js
DELETED
|
@@ -1,409 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
require("./Polyfills");
|
|
4
|
-
var get = require("lodash.get");
|
|
5
|
-
var Report = require("./Report");
|
|
6
|
-
var FormatValidators = require("./FormatValidators");
|
|
7
|
-
var JsonValidation = require("./JsonValidation");
|
|
8
|
-
var SchemaCache = require("./SchemaCache");
|
|
9
|
-
var SchemaCompilation = require("./SchemaCompilation");
|
|
10
|
-
var SchemaValidation = require("./SchemaValidation");
|
|
11
|
-
var Utils = require("./Utils");
|
|
12
|
-
var Draft4Schema = require("./schemas/schema.json");
|
|
13
|
-
var Draft4HyperSchema = require("./schemas/hyper-schema.json");
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* default options
|
|
17
|
-
*/
|
|
18
|
-
var defaultOptions = {
|
|
19
|
-
// default timeout for all async tasks
|
|
20
|
-
asyncTimeout: 2000,
|
|
21
|
-
// force additionalProperties and additionalItems to be defined on "object" and "array" types
|
|
22
|
-
forceAdditional: false,
|
|
23
|
-
// assume additionalProperties and additionalItems are defined as "false" where appropriate
|
|
24
|
-
assumeAdditional: false,
|
|
25
|
-
// do case insensitive comparison for enums
|
|
26
|
-
enumCaseInsensitiveComparison: false,
|
|
27
|
-
// force items to be defined on "array" types
|
|
28
|
-
forceItems: false,
|
|
29
|
-
// force minItems to be defined on "array" types
|
|
30
|
-
forceMinItems: false,
|
|
31
|
-
// force maxItems to be defined on "array" types
|
|
32
|
-
forceMaxItems: false,
|
|
33
|
-
// force minLength to be defined on "string" types
|
|
34
|
-
forceMinLength: false,
|
|
35
|
-
// force maxLength to be defined on "string" types
|
|
36
|
-
forceMaxLength: false,
|
|
37
|
-
// force properties or patternProperties to be defined on "object" types
|
|
38
|
-
forceProperties: false,
|
|
39
|
-
// ignore references that cannot be resolved (remote schemas) // TODO: make sure this is only for remote schemas, not local ones
|
|
40
|
-
ignoreUnresolvableReferences: false,
|
|
41
|
-
// disallow usage of keywords that this validator can't handle
|
|
42
|
-
noExtraKeywords: false,
|
|
43
|
-
// disallow usage of schema's without "type" defined
|
|
44
|
-
noTypeless: false,
|
|
45
|
-
// disallow zero length strings in validated objects
|
|
46
|
-
noEmptyStrings: false,
|
|
47
|
-
// disallow zero length arrays in validated objects
|
|
48
|
-
noEmptyArrays: false,
|
|
49
|
-
// forces "uri" format to be in fully rfc3986 compliant
|
|
50
|
-
strictUris: false,
|
|
51
|
-
// turn on some of the above
|
|
52
|
-
strictMode: false,
|
|
53
|
-
// report error paths as an array of path segments to get to the offending node
|
|
54
|
-
reportPathAsArray: false,
|
|
55
|
-
// stop validation as soon as an error is found
|
|
56
|
-
breakOnFirstError: false,
|
|
57
|
-
// check if schema follows best practices and common sense
|
|
58
|
-
pedanticCheck: false,
|
|
59
|
-
// ignore unknown formats (do not report them as an error)
|
|
60
|
-
ignoreUnknownFormats: false,
|
|
61
|
-
// function to be called on every schema
|
|
62
|
-
customValidator: null
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
function normalizeOptions(options) {
|
|
66
|
-
var normalized;
|
|
67
|
-
|
|
68
|
-
// options
|
|
69
|
-
if (typeof options === "object") {
|
|
70
|
-
var keys = Object.keys(options),
|
|
71
|
-
idx = keys.length,
|
|
72
|
-
key;
|
|
73
|
-
|
|
74
|
-
// check that the options are correctly configured
|
|
75
|
-
while (idx--) {
|
|
76
|
-
key = keys[idx];
|
|
77
|
-
if (defaultOptions[key] === undefined) {
|
|
78
|
-
throw new Error("Unexpected option passed to constructor: " + key);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// copy the default options into passed options
|
|
83
|
-
keys = Object.keys(defaultOptions);
|
|
84
|
-
idx = keys.length;
|
|
85
|
-
while (idx--) {
|
|
86
|
-
key = keys[idx];
|
|
87
|
-
if (options[key] === undefined) {
|
|
88
|
-
options[key] = Utils.clone(defaultOptions[key]);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
normalized = options;
|
|
93
|
-
} else {
|
|
94
|
-
normalized = Utils.clone(defaultOptions);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (normalized.strictMode === true) {
|
|
98
|
-
normalized.forceAdditional = true;
|
|
99
|
-
normalized.forceItems = true;
|
|
100
|
-
normalized.forceMaxLength = true;
|
|
101
|
-
normalized.forceProperties = true;
|
|
102
|
-
normalized.noExtraKeywords = true;
|
|
103
|
-
normalized.noTypeless = true;
|
|
104
|
-
normalized.noEmptyStrings = true;
|
|
105
|
-
normalized.noEmptyArrays = true;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return normalized;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @class
|
|
113
|
-
*
|
|
114
|
-
* @param {*} [options]
|
|
115
|
-
*/
|
|
116
|
-
function ZSchema(options) {
|
|
117
|
-
this.cache = {};
|
|
118
|
-
this.referenceCache = [];
|
|
119
|
-
this.validateOptions = {};
|
|
120
|
-
|
|
121
|
-
this.options = normalizeOptions(options);
|
|
122
|
-
|
|
123
|
-
// Disable strict validation for the built-in schemas
|
|
124
|
-
var metaschemaOptions = normalizeOptions({ });
|
|
125
|
-
|
|
126
|
-
this.setRemoteReference("http://json-schema.org/draft-04/schema", Draft4Schema, metaschemaOptions);
|
|
127
|
-
this.setRemoteReference("http://json-schema.org/draft-04/hyper-schema", Draft4HyperSchema, metaschemaOptions);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* instance methods
|
|
132
|
-
*
|
|
133
|
-
* @param {*} schema
|
|
134
|
-
*
|
|
135
|
-
* @returns {boolean}
|
|
136
|
-
*/
|
|
137
|
-
ZSchema.prototype.compileSchema = function (schema) {
|
|
138
|
-
var report = new Report(this.options);
|
|
139
|
-
|
|
140
|
-
schema = SchemaCache.getSchema.call(this, report, schema);
|
|
141
|
-
|
|
142
|
-
SchemaCompilation.compileSchema.call(this, report, schema);
|
|
143
|
-
|
|
144
|
-
this.lastReport = report;
|
|
145
|
-
return report.isValid();
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
*
|
|
150
|
-
* @param {*} schema
|
|
151
|
-
*
|
|
152
|
-
* @returns {boolean}
|
|
153
|
-
*/
|
|
154
|
-
ZSchema.prototype.validateSchema = function (schema) {
|
|
155
|
-
if (Array.isArray(schema) && schema.length === 0) {
|
|
156
|
-
throw new Error(".validateSchema was called with an empty array");
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
var report = new Report(this.options);
|
|
160
|
-
|
|
161
|
-
schema = SchemaCache.getSchema.call(this, report, schema);
|
|
162
|
-
|
|
163
|
-
var compiled = SchemaCompilation.compileSchema.call(this, report, schema);
|
|
164
|
-
if (compiled) { SchemaValidation.validateSchema.call(this, report, schema); }
|
|
165
|
-
|
|
166
|
-
this.lastReport = report;
|
|
167
|
-
return report.isValid();
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
*
|
|
172
|
-
* @param {*} json
|
|
173
|
-
* @param {*} schema
|
|
174
|
-
* @param {*} [options]
|
|
175
|
-
* @param {function(*, *)} [callback]
|
|
176
|
-
*
|
|
177
|
-
* @returns {boolean}
|
|
178
|
-
*/
|
|
179
|
-
ZSchema.prototype.validate = function (json, schema, options, callback) {
|
|
180
|
-
|
|
181
|
-
if (Utils.whatIs(options) === "function") {
|
|
182
|
-
callback = options;
|
|
183
|
-
options = {};
|
|
184
|
-
}
|
|
185
|
-
if (!options) { options = {}; }
|
|
186
|
-
|
|
187
|
-
this.validateOptions = options;
|
|
188
|
-
|
|
189
|
-
var whatIs = Utils.whatIs(schema);
|
|
190
|
-
if (whatIs !== "string" && whatIs !== "object") {
|
|
191
|
-
var e = new Error("Invalid .validate call - schema must be a string or object but " + whatIs + " was passed!");
|
|
192
|
-
if (callback) {
|
|
193
|
-
process.nextTick(function () {
|
|
194
|
-
callback(e, false);
|
|
195
|
-
});
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
throw e;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
var foundError = false;
|
|
202
|
-
var report = new Report(this.options);
|
|
203
|
-
report.json = json;
|
|
204
|
-
|
|
205
|
-
if (typeof schema === "string") {
|
|
206
|
-
var schemaName = schema;
|
|
207
|
-
schema = SchemaCache.getSchema.call(this, report, schemaName);
|
|
208
|
-
if (!schema) {
|
|
209
|
-
throw new Error("Schema with id '" + schemaName + "' wasn't found in the validator cache!");
|
|
210
|
-
}
|
|
211
|
-
} else {
|
|
212
|
-
schema = SchemaCache.getSchema.call(this, report, schema);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
var compiled = false;
|
|
216
|
-
if (!foundError) {
|
|
217
|
-
compiled = SchemaCompilation.compileSchema.call(this, report, schema);
|
|
218
|
-
}
|
|
219
|
-
if (!compiled) {
|
|
220
|
-
this.lastReport = report;
|
|
221
|
-
foundError = true;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
var validated = false;
|
|
225
|
-
if (!foundError) {
|
|
226
|
-
validated = SchemaValidation.validateSchema.call(this, report, schema);
|
|
227
|
-
}
|
|
228
|
-
if (!validated) {
|
|
229
|
-
this.lastReport = report;
|
|
230
|
-
foundError = true;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (options.schemaPath) {
|
|
234
|
-
report.rootSchema = schema;
|
|
235
|
-
schema = get(schema, options.schemaPath);
|
|
236
|
-
if (!schema) {
|
|
237
|
-
throw new Error("Schema path '" + options.schemaPath + "' wasn't found in the schema!");
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (!foundError) {
|
|
242
|
-
JsonValidation.validate.call(this, report, schema, json);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
if (callback) {
|
|
246
|
-
report.processAsyncTasks(this.options.asyncTimeout, callback);
|
|
247
|
-
return;
|
|
248
|
-
} else if (report.asyncTasks.length > 0) {
|
|
249
|
-
throw new Error("This validation has async tasks and cannot be done in sync mode, please provide callback argument.");
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// assign lastReport so errors are retrievable in sync mode
|
|
253
|
-
this.lastReport = report;
|
|
254
|
-
return report.isValid();
|
|
255
|
-
};
|
|
256
|
-
ZSchema.prototype.getLastError = function () {
|
|
257
|
-
if (this.lastReport.errors.length === 0) {
|
|
258
|
-
return null;
|
|
259
|
-
}
|
|
260
|
-
var e = new Error();
|
|
261
|
-
e.name = "z-schema validation error";
|
|
262
|
-
e.message = this.lastReport.commonErrorMessage;
|
|
263
|
-
e.details = this.lastReport.errors;
|
|
264
|
-
return e;
|
|
265
|
-
};
|
|
266
|
-
ZSchema.prototype.getLastErrors = function () {
|
|
267
|
-
return this.lastReport && this.lastReport.errors.length > 0 ? this.lastReport.errors : null;
|
|
268
|
-
};
|
|
269
|
-
ZSchema.prototype.getMissingReferences = function (arr) {
|
|
270
|
-
arr = arr || this.lastReport.errors;
|
|
271
|
-
var res = [],
|
|
272
|
-
idx = arr.length;
|
|
273
|
-
while (idx--) {
|
|
274
|
-
var error = arr[idx];
|
|
275
|
-
if (error.code === "UNRESOLVABLE_REFERENCE") {
|
|
276
|
-
var reference = error.params[0];
|
|
277
|
-
if (res.indexOf(reference) === -1) {
|
|
278
|
-
res.push(reference);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
if (error.inner) {
|
|
282
|
-
res = res.concat(this.getMissingReferences(error.inner));
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return res;
|
|
286
|
-
};
|
|
287
|
-
ZSchema.prototype.getMissingRemoteReferences = function () {
|
|
288
|
-
var missingReferences = this.getMissingReferences(),
|
|
289
|
-
missingRemoteReferences = [],
|
|
290
|
-
idx = missingReferences.length;
|
|
291
|
-
while (idx--) {
|
|
292
|
-
var remoteReference = SchemaCache.getRemotePath(missingReferences[idx]);
|
|
293
|
-
if (remoteReference && missingRemoteReferences.indexOf(remoteReference) === -1) {
|
|
294
|
-
missingRemoteReferences.push(remoteReference);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
return missingRemoteReferences;
|
|
298
|
-
};
|
|
299
|
-
ZSchema.prototype.setRemoteReference = function (uri, schema, validationOptions) {
|
|
300
|
-
if (typeof schema === "string") {
|
|
301
|
-
schema = JSON.parse(schema);
|
|
302
|
-
} else {
|
|
303
|
-
schema = Utils.cloneDeep(schema);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (validationOptions) {
|
|
307
|
-
schema.__$validationOptions = normalizeOptions(validationOptions);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
SchemaCache.cacheSchemaByUri.call(this, uri, schema);
|
|
311
|
-
};
|
|
312
|
-
ZSchema.prototype.getResolvedSchema = function (schema) {
|
|
313
|
-
var report = new Report(this.options);
|
|
314
|
-
schema = SchemaCache.getSchema.call(this, report, schema);
|
|
315
|
-
|
|
316
|
-
// clone before making any modifications
|
|
317
|
-
schema = Utils.cloneDeep(schema);
|
|
318
|
-
|
|
319
|
-
var visited = [];
|
|
320
|
-
|
|
321
|
-
// clean-up the schema and resolve references
|
|
322
|
-
var cleanup = function (schema) {
|
|
323
|
-
var key,
|
|
324
|
-
typeOf = Utils.whatIs(schema);
|
|
325
|
-
if (typeOf !== "object" && typeOf !== "array") {
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
if (schema.___$visited) {
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
schema.___$visited = true;
|
|
334
|
-
visited.push(schema);
|
|
335
|
-
|
|
336
|
-
if (schema.$ref && schema.__$refResolved) {
|
|
337
|
-
var from = schema.__$refResolved;
|
|
338
|
-
var to = schema;
|
|
339
|
-
delete schema.$ref;
|
|
340
|
-
delete schema.__$refResolved;
|
|
341
|
-
for (key in from) {
|
|
342
|
-
if (from.hasOwnProperty(key)) {
|
|
343
|
-
to[key] = from[key];
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
for (key in schema) {
|
|
348
|
-
if (schema.hasOwnProperty(key)) {
|
|
349
|
-
if (key.indexOf("__$") === 0) {
|
|
350
|
-
delete schema[key];
|
|
351
|
-
} else {
|
|
352
|
-
cleanup(schema[key]);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
cleanup(schema);
|
|
359
|
-
visited.forEach(function (s) {
|
|
360
|
-
delete s.___$visited;
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
this.lastReport = report;
|
|
364
|
-
if (report.isValid()) {
|
|
365
|
-
return schema;
|
|
366
|
-
} else {
|
|
367
|
-
throw this.getLastError();
|
|
368
|
-
}
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
*
|
|
373
|
-
* @param {*} schemaReader
|
|
374
|
-
*
|
|
375
|
-
* @returns {void}
|
|
376
|
-
*/
|
|
377
|
-
ZSchema.prototype.setSchemaReader = function (schemaReader) {
|
|
378
|
-
return ZSchema.setSchemaReader(schemaReader);
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
ZSchema.prototype.getSchemaReader = function () {
|
|
382
|
-
return ZSchema.schemaReader;
|
|
383
|
-
};
|
|
384
|
-
|
|
385
|
-
ZSchema.schemaReader = undefined;
|
|
386
|
-
/*
|
|
387
|
-
static methods
|
|
388
|
-
*/
|
|
389
|
-
ZSchema.setSchemaReader = function (schemaReader) {
|
|
390
|
-
ZSchema.schemaReader = schemaReader;
|
|
391
|
-
};
|
|
392
|
-
ZSchema.registerFormat = function (formatName, validatorFunction) {
|
|
393
|
-
FormatValidators[formatName] = validatorFunction;
|
|
394
|
-
};
|
|
395
|
-
ZSchema.unregisterFormat = function (formatName) {
|
|
396
|
-
delete FormatValidators[formatName];
|
|
397
|
-
};
|
|
398
|
-
ZSchema.getRegisteredFormats = function () {
|
|
399
|
-
return Object.keys(FormatValidators);
|
|
400
|
-
};
|
|
401
|
-
ZSchema.getDefaultOptions = function () {
|
|
402
|
-
return Utils.cloneDeep(defaultOptions);
|
|
403
|
-
};
|
|
404
|
-
|
|
405
|
-
ZSchema.schemaSymbol = Utils.schemaSymbol;
|
|
406
|
-
|
|
407
|
-
ZSchema.jsonSymbol = Utils.jsonSymbol;
|
|
408
|
-
|
|
409
|
-
module.exports = ZSchema;
|