z-schema 3.18.1 → 3.19.0
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 +55 -54
- package/dist/ZSchema-browser-min.js +1 -3
- package/dist/ZSchema-browser-min.js.map +1 -1
- package/dist/ZSchema-browser-test.js +5746 -4016
- package/dist/ZSchema-browser.js +1853 -985
- package/package.json +19 -19
- package/src/SchemaCache.js +9 -1
- package/src/ZSchema.js +36 -21
package/README.md
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
+
# z-schema validator
|
|
2
|
+
|
|
1
3
|
[](http://badge.fury.io/js/z-schema)
|
|
2
4
|
[](http://badge.fury.io/bo/z-schema)
|
|
3
5
|
[](https://travis-ci.org/zaggino/z-schema)
|
|
4
6
|
[](https://coveralls.io/r/zaggino/z-schema)
|
|
5
7
|
|
|
8
|
+
[](https://greenkeeper.io/)
|
|
6
9
|
[](https://david-dm.org/zaggino/z-schema)
|
|
7
10
|
[](https://david-dm.org/zaggino/z-schema?type=dev)
|
|
8
11
|
[](https://david-dm.org/zaggino/z-schema?type=optional)
|
|
9
12
|
|
|
10
|
-
#z-schema validator
|
|
11
|
-
|
|
12
13
|
[](https://nodei.co/npm/z-schema/)
|
|
13
14
|
|
|
14
15
|
- version 3.0 runs also in the browsers now, run tests yourself [here](https://rawgit.com/zaggino/z-schema/master/test/SpecRunner.html)
|
|
15
16
|
|
|
16
|
-
#Topics
|
|
17
|
+
# Topics
|
|
17
18
|
|
|
18
19
|
- [Usage](#usage)
|
|
19
20
|
- [Features](#features)
|
|
@@ -21,16 +22,16 @@
|
|
|
21
22
|
- [Benchmarks](#benchmarks)
|
|
22
23
|
- [Contributors](#contributors)
|
|
23
24
|
|
|
24
|
-
#Usage
|
|
25
|
+
# Usage
|
|
25
26
|
|
|
26
27
|
Validator will try to perform sync validation when possible for speed, but supports async callbacks when they are necessary.
|
|
27
28
|
|
|
28
|
-
##Development:
|
|
29
|
+
## Development:
|
|
29
30
|
|
|
30
31
|
These repository has several submodules and should be cloned as follows:
|
|
31
32
|
>git clone **--recursive** https://github.com/zaggino/z-schema.git
|
|
32
33
|
|
|
33
|
-
##CLI:
|
|
34
|
+
## CLI:
|
|
34
35
|
|
|
35
36
|
```
|
|
36
37
|
npm install --global z-schema
|
|
@@ -40,7 +41,7 @@ z-schema mySchema.json myJson.json
|
|
|
40
41
|
z-schema --strictMode mySchema.json myJson.json
|
|
41
42
|
```
|
|
42
43
|
|
|
43
|
-
##NodeJS:
|
|
44
|
+
## NodeJS:
|
|
44
45
|
|
|
45
46
|
```javascript
|
|
46
47
|
var ZSchema = require("z-schema");
|
|
@@ -48,7 +49,7 @@ var options = ... // see below for possible option values
|
|
|
48
49
|
var validator = new ZSchema(options);
|
|
49
50
|
```
|
|
50
51
|
|
|
51
|
-
##Sync mode:
|
|
52
|
+
## Sync mode:
|
|
52
53
|
|
|
53
54
|
```javascript
|
|
54
55
|
var valid = validator.validate(json, schema);
|
|
@@ -59,7 +60,7 @@ var errors = validator.getLastErrors();
|
|
|
59
60
|
...
|
|
60
61
|
```
|
|
61
62
|
|
|
62
|
-
##Async mode:
|
|
63
|
+
## Async mode:
|
|
63
64
|
|
|
64
65
|
```javascript
|
|
65
66
|
validator.validate(json, schema, function (err, valid) {
|
|
@@ -67,7 +68,7 @@ validator.validate(json, schema, function (err, valid) {
|
|
|
67
68
|
});
|
|
68
69
|
```
|
|
69
70
|
|
|
70
|
-
##Browser:
|
|
71
|
+
## Browser:
|
|
71
72
|
|
|
72
73
|
```html
|
|
73
74
|
<script type="text/javascript" src="../dist/ZSchema-browser-min.js"></script>
|
|
@@ -78,7 +79,7 @@ validator.validate(json, schema, function (err, valid) {
|
|
|
78
79
|
</script>
|
|
79
80
|
```
|
|
80
81
|
|
|
81
|
-
##Remote references and schemas:
|
|
82
|
+
## Remote references and schemas:
|
|
82
83
|
|
|
83
84
|
In case you have some remote references in your schemas, you have to download those schemas before using validator.
|
|
84
85
|
Otherwise you'll get ```UNRESOLVABLE_REFERENCE``` error when trying to compile a schema.
|
|
@@ -116,7 +117,7 @@ ZSchema.setSchemaReader(function (uri) {
|
|
|
116
117
|
});
|
|
117
118
|
```
|
|
118
119
|
|
|
119
|
-
#Features
|
|
120
|
+
# Features
|
|
120
121
|
|
|
121
122
|
- [Validate against subschema](#validate-against-subschema)
|
|
122
123
|
- [Compile arrays of schemas and use references between them](#compile-arrays-of-schemas-and-use-references-between-them)
|
|
@@ -142,7 +143,7 @@ ZSchema.setSchemaReader(function (uri) {
|
|
|
142
143
|
- [Set validator to collect as many errors as possible](#breakonfirsterror)
|
|
143
144
|
- [Report paths in errors as arrays so they can be processed easier](#reportpathasarray)
|
|
144
145
|
|
|
145
|
-
##Validate against subschema
|
|
146
|
+
## Validate against subschema
|
|
146
147
|
|
|
147
148
|
In case you don't want to split your schema into multiple schemas using reference for any reason, you can use option schemaPath when validating:
|
|
148
149
|
|
|
@@ -152,7 +153,7 @@ var valid = validator.validate(cars, schema, { schemaPath: "definitions.car.defi
|
|
|
152
153
|
|
|
153
154
|
See more details in the [test](/test/spec/schemaPathSpec.js).
|
|
154
155
|
|
|
155
|
-
##Compile arrays of schemas and use references between them
|
|
156
|
+
## Compile arrays of schemas and use references between them
|
|
156
157
|
|
|
157
158
|
You can use validator to compile an array of schemas that have references between them and then validate against one of those schemas:
|
|
158
159
|
|
|
@@ -203,7 +204,7 @@ var valid = validator.validate(data, schemas[2]);
|
|
|
203
204
|
// valid === true
|
|
204
205
|
```
|
|
205
206
|
|
|
206
|
-
##Register a custom format
|
|
207
|
+
## Register a custom format
|
|
207
208
|
|
|
208
209
|
You can register any format of your own. Your sync validator function should always respond with a boolean:
|
|
209
210
|
|
|
@@ -222,17 +223,17 @@ ZSchema.registerFormat("xstring", function (str, callback) {
|
|
|
222
223
|
}, 1);
|
|
223
224
|
});
|
|
224
225
|
```
|
|
225
|
-
##Helper method to check the formats that have been registered
|
|
226
|
+
## Helper method to check the formats that have been registered
|
|
226
227
|
```javascript
|
|
227
228
|
var registeredFormats = ZSchema.getRegisteredFormats();
|
|
228
229
|
//registeredFormats will now contain an array of all formats that have been registered with z-schema
|
|
229
230
|
```
|
|
230
|
-
##Automatic downloading of remote schemas
|
|
231
|
+
## Automatic downloading of remote schemas
|
|
231
232
|
|
|
232
233
|
Automatic downloading of remote schemas was removed from version ```3.x``` but is still possible with a bit of extra code,
|
|
233
234
|
see [this test](test/spec/AutomaticSchemaLoadingSpec.js) for more information on this.
|
|
234
235
|
|
|
235
|
-
##Prefill default values to object using format
|
|
236
|
+
## Prefill default values to object using format
|
|
236
237
|
|
|
237
238
|
Using format, you can pre-fill values of your choosing into the objects like this:
|
|
238
239
|
|
|
@@ -253,9 +254,9 @@ validator.validate(data, schema);
|
|
|
253
254
|
// data.hello === "world"
|
|
254
255
|
```
|
|
255
256
|
|
|
256
|
-
#Options
|
|
257
|
+
# Options
|
|
257
258
|
|
|
258
|
-
##asyncTimeout
|
|
259
|
+
## asyncTimeout
|
|
259
260
|
|
|
260
261
|
Defines a time limit, which should be used when waiting for async tasks like async format validators to perform their validation,
|
|
261
262
|
before the validation fails with an ```ASYNC_TIMEOUT``` error.
|
|
@@ -266,7 +267,7 @@ var validator = new ZSchema({
|
|
|
266
267
|
});
|
|
267
268
|
```
|
|
268
269
|
|
|
269
|
-
##noEmptyArrays
|
|
270
|
+
## noEmptyArrays
|
|
270
271
|
|
|
271
272
|
When true, validator will assume that minimum count of items in any ```array``` is 1, except when ```minItems: 0``` is explicitly defined.
|
|
272
273
|
|
|
@@ -276,7 +277,7 @@ var validator = new ZSchema({
|
|
|
276
277
|
});
|
|
277
278
|
```
|
|
278
279
|
|
|
279
|
-
##noEmptyStrings
|
|
280
|
+
## noEmptyStrings
|
|
280
281
|
|
|
281
282
|
When true, validator will assume that minimum length of any string to pass type ```string``` validation is 1, except when ```minLength: 0``` is explicitly defined.
|
|
282
283
|
|
|
@@ -286,7 +287,7 @@ var validator = new ZSchema({
|
|
|
286
287
|
});
|
|
287
288
|
```
|
|
288
289
|
|
|
289
|
-
##noTypeless
|
|
290
|
+
## noTypeless
|
|
290
291
|
|
|
291
292
|
When true, validator will fail validation for schemas that don't specify a ```type``` of object that they expect.
|
|
292
293
|
|
|
@@ -296,7 +297,7 @@ var validator = new ZSchema({
|
|
|
296
297
|
});
|
|
297
298
|
```
|
|
298
299
|
|
|
299
|
-
##noExtraKeywords
|
|
300
|
+
## noExtraKeywords
|
|
300
301
|
|
|
301
302
|
When true, validator will fail for schemas that use keywords not defined in JSON Schema specification and doesn't provide a parent schema in ```$schema``` property to validate the schema.
|
|
302
303
|
|
|
@@ -306,7 +307,7 @@ var validator = new ZSchema({
|
|
|
306
307
|
});
|
|
307
308
|
```
|
|
308
309
|
|
|
309
|
-
##assumeAdditional
|
|
310
|
+
## assumeAdditional
|
|
310
311
|
|
|
311
312
|
When true, validator assumes that additionalItems/additionalProperties are defined as false so you don't have to manually fix all your schemas.
|
|
312
313
|
|
|
@@ -324,7 +325,7 @@ var validator = new ZSchema({
|
|
|
324
325
|
});
|
|
325
326
|
```
|
|
326
327
|
|
|
327
|
-
##forceAdditional
|
|
328
|
+
## forceAdditional
|
|
328
329
|
|
|
329
330
|
When true, validator doesn't validate schemas where additionalItems/additionalProperties should be defined to either true or false.
|
|
330
331
|
|
|
@@ -334,7 +335,7 @@ var validator = new ZSchema({
|
|
|
334
335
|
});
|
|
335
336
|
```
|
|
336
337
|
|
|
337
|
-
##forceItems
|
|
338
|
+
## forceItems
|
|
338
339
|
|
|
339
340
|
When true, validator doesn't validate schemas where ```items``` are not defined for ```array``` type schemas.
|
|
340
341
|
This is to avoid passing anything through an array definition.
|
|
@@ -345,7 +346,7 @@ var validator = new ZSchema({
|
|
|
345
346
|
});
|
|
346
347
|
```
|
|
347
348
|
|
|
348
|
-
##forceMinItems
|
|
349
|
+
## forceMinItems
|
|
349
350
|
|
|
350
351
|
When true, validator doesn't validate schemas where ```minItems``` is not defined for ```array``` type schemas.
|
|
351
352
|
This is to avoid passing zero-length arrays which application doesn't expect to handle.
|
|
@@ -356,7 +357,7 @@ var validator = new ZSchema({
|
|
|
356
357
|
});
|
|
357
358
|
```
|
|
358
359
|
|
|
359
|
-
##forceMaxItems
|
|
360
|
+
## forceMaxItems
|
|
360
361
|
|
|
361
362
|
When true, validator doesn't validate schemas where ```maxItems``` is not defined for ```array``` type schemas.
|
|
362
363
|
This is to avoid passing arrays with unlimited count of elements which application doesn't expect to handle.
|
|
@@ -367,7 +368,7 @@ var validator = new ZSchema({
|
|
|
367
368
|
});
|
|
368
369
|
```
|
|
369
370
|
|
|
370
|
-
##forceMinLength
|
|
371
|
+
## forceMinLength
|
|
371
372
|
|
|
372
373
|
When true, validator doesn't validate schemas where ```minLength``` is not defined for ```string``` type schemas.
|
|
373
374
|
This is to avoid passing zero-length strings which application doesn't expect to handle.
|
|
@@ -379,7 +380,7 @@ var validator = new ZSchema({
|
|
|
379
380
|
```
|
|
380
381
|
|
|
381
382
|
|
|
382
|
-
##forceMaxLength
|
|
383
|
+
## forceMaxLength
|
|
383
384
|
|
|
384
385
|
When true, validator doesn't validate schemas where ```maxLength``` is not defined for ```string``` type schemas.
|
|
385
386
|
This is to avoid passing extremly large strings which application doesn't expect to handle.
|
|
@@ -390,7 +391,7 @@ var validator = new ZSchema({
|
|
|
390
391
|
});
|
|
391
392
|
```
|
|
392
393
|
|
|
393
|
-
##forceProperties
|
|
394
|
+
## forceProperties
|
|
394
395
|
|
|
395
396
|
When true, validator doesn't validate schemas where ```properties``` or ```patternProperties``` is not defined for ```object``` type schemas.
|
|
396
397
|
This is to avoid having objects with unexpected properties in application.
|
|
@@ -401,7 +402,7 @@ var validator = new ZSchema({
|
|
|
401
402
|
});
|
|
402
403
|
```
|
|
403
404
|
|
|
404
|
-
##ignoreUnresolvableReferences
|
|
405
|
+
## ignoreUnresolvableReferences
|
|
405
406
|
|
|
406
407
|
When true, validator doesn't end with error when a remote reference is unreachable. **This setting is not recommended in production outside of testing.**
|
|
407
408
|
|
|
@@ -411,7 +412,7 @@ var validator = new ZSchema({
|
|
|
411
412
|
});
|
|
412
413
|
```
|
|
413
414
|
|
|
414
|
-
##strictUris
|
|
415
|
+
## strictUris
|
|
415
416
|
|
|
416
417
|
When true, all strings of format ```uri``` must be an absolute URIs and not only URI references. See more details in [this issue](https://github.com/zaggino/z-schema/issues/18).
|
|
417
418
|
|
|
@@ -421,17 +422,30 @@ var validator = new ZSchema({
|
|
|
421
422
|
});
|
|
422
423
|
```
|
|
423
424
|
|
|
424
|
-
##strictMode
|
|
425
|
+
## strictMode
|
|
425
426
|
|
|
426
427
|
Strict mode of z-schema is currently equal to the following:
|
|
427
428
|
|
|
429
|
+
```javascript
|
|
430
|
+
if (this.options.strictMode === true) {
|
|
431
|
+
this.options.forceAdditional = true;
|
|
432
|
+
this.options.forceItems = true;
|
|
433
|
+
this.options.forceMaxLength = true;
|
|
434
|
+
this.options.forceProperties = true;
|
|
435
|
+
this.options.noExtraKeywords = true;
|
|
436
|
+
this.options.noTypeless = true;
|
|
437
|
+
this.options.noEmptyStrings = true;
|
|
438
|
+
this.options.noEmptyArrays = true;
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
428
442
|
```javascript
|
|
429
443
|
var validator = new ZSchema({
|
|
430
444
|
strictMode: true
|
|
431
445
|
});
|
|
432
446
|
```
|
|
433
447
|
|
|
434
|
-
##breakOnFirstError
|
|
448
|
+
## breakOnFirstError
|
|
435
449
|
|
|
436
450
|
By default, z-schema stops validation after the first error is found. With this you can tell it to continue validating anyway:
|
|
437
451
|
|
|
@@ -441,7 +455,7 @@ var validator = new ZSchema({
|
|
|
441
455
|
});
|
|
442
456
|
```
|
|
443
457
|
|
|
444
|
-
##reportPathAsArray
|
|
458
|
+
## reportPathAsArray
|
|
445
459
|
|
|
446
460
|
Report error paths as an array of path segments instead of a string:
|
|
447
461
|
|
|
@@ -451,20 +465,7 @@ var validator = new ZSchema({
|
|
|
451
465
|
});
|
|
452
466
|
```
|
|
453
467
|
|
|
454
|
-
|
|
455
|
-
if (this.options.strictMode === true) {
|
|
456
|
-
this.options.forceAdditional = true;
|
|
457
|
-
this.options.forceItems = true;
|
|
458
|
-
this.options.forceMaxLength = true;
|
|
459
|
-
this.options.forceProperties = true;
|
|
460
|
-
this.options.noExtraKeywords = true;
|
|
461
|
-
this.options.noTypeless = true;
|
|
462
|
-
this.options.noEmptyStrings = true;
|
|
463
|
-
this.options.noEmptyArrays = true;
|
|
464
|
-
}
|
|
465
|
-
```
|
|
466
|
-
|
|
467
|
-
##ignoreUnknownFormats
|
|
468
|
+
## ignoreUnknownFormats
|
|
468
469
|
|
|
469
470
|
By default, z-schema reports all unknown formats, formats not defined by JSON Schema and not registered using
|
|
470
471
|
`ZSchema.registerFormat`, as an error. But the
|
|
@@ -478,7 +479,7 @@ var validator = new ZSchema({
|
|
|
478
479
|
});
|
|
479
480
|
```
|
|
480
481
|
|
|
481
|
-
##customValidator
|
|
482
|
+
## customValidator
|
|
482
483
|
|
|
483
484
|
**Warning**: Use only if know what you are doing. Always consider using [custom format](#register-a-custom-format) before using this option.
|
|
484
485
|
|
|
@@ -565,7 +566,7 @@ console.log(validator.getLastErrors())
|
|
|
565
566
|
```
|
|
566
567
|
**Note:** before creating your own keywords you should consider all compatibility issues.
|
|
567
568
|
|
|
568
|
-
#Benchmarks
|
|
569
|
+
# Benchmarks
|
|
569
570
|
|
|
570
571
|
So how does it compare to version 2.x and others?
|
|
571
572
|
|
|
@@ -573,7 +574,7 @@ So how does it compare to version 2.x and others?
|
|
|
573
574
|
|
|
574
575
|
[rawgithub.com/zaggino/z-schema/master/benchmark/results.html](https://rawgithub.com/zaggino/z-schema/master/benchmark/results.html)
|
|
575
576
|
|
|
576
|
-
#Contributors
|
|
577
|
+
# Contributors
|
|
577
578
|
|
|
578
579
|
Thanks for contributing to:
|
|
579
580
|
|