z-schema 7.0.0-beta.5 → 7.0.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 +63 -65
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,34 +1,41 @@
|
|
|
1
1
|
# z-schema validator
|
|
2
2
|
|
|
3
|
-
[](http://badge.fury.io/js/z-schema)
|
|
4
|
-
|
|
5
|
-
[](https://coveralls.io/r/zaggino/z-schema)
|
|
6
|
-
|
|
7
|
-
[](https://greenkeeper.io/)
|
|
8
|
-
|
|
9
3
|
[](https://nodei.co/npm/z-schema/)
|
|
10
4
|
|
|
11
|
-
|
|
5
|
+
[](https://coveralls.io/r/zaggino/z-schema)
|
|
12
6
|
|
|
13
|
-
|
|
7
|
+
## Topics
|
|
14
8
|
|
|
15
9
|
- [Usage](#usage)
|
|
16
10
|
- [Features](#features)
|
|
17
11
|
- [Options](#options)
|
|
18
12
|
- [Benchmarks](#benchmarks)
|
|
13
|
+
- [Contributing](#contributing)
|
|
19
14
|
- [Contributors](#contributors)
|
|
20
15
|
|
|
21
|
-
|
|
16
|
+
## Usage
|
|
22
17
|
|
|
23
18
|
Validator will try to perform sync validation when possible for speed, but supports async callbacks when they are necessary.
|
|
24
19
|
|
|
25
|
-
|
|
20
|
+
### ESM and Typescript:
|
|
26
21
|
|
|
27
|
-
|
|
22
|
+
```javascript
|
|
23
|
+
import ZSchema from 'z-schema';
|
|
24
|
+
const validator = new ZSchema();
|
|
25
|
+
console.log(validator.validate(1, { type: 'number' })); // true
|
|
26
|
+
console.log(validator.validate(1, { type: 'string' })); // false
|
|
27
|
+
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
### CommonJs:
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
```javascript
|
|
32
|
+
const ZSchema = require('z-schema');
|
|
33
|
+
const validator = new ZSchema();
|
|
34
|
+
console.log(validator.validate(1, { type: 'number' })); // true
|
|
35
|
+
console.log(validator.validate(1, { type: 'string' })); // false
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### CLI:
|
|
32
39
|
|
|
33
40
|
```bash
|
|
34
41
|
npm install --global z-schema
|
|
@@ -38,15 +45,7 @@ z-schema mySchema.json myJson.json
|
|
|
38
45
|
z-schema --strictMode mySchema.json myJson.json
|
|
39
46
|
```
|
|
40
47
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
```javascript
|
|
44
|
-
import ZSchema from 'z-schema';
|
|
45
|
-
var options = ... // see below for possible option values
|
|
46
|
-
var validator = new ZSchema(options);
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## Sync mode:
|
|
48
|
+
### Sync mode:
|
|
50
49
|
|
|
51
50
|
```javascript
|
|
52
51
|
var valid = validator.validate(json, schema);
|
|
@@ -57,7 +56,7 @@ var errors = validator.getLastErrors();
|
|
|
57
56
|
...
|
|
58
57
|
```
|
|
59
58
|
|
|
60
|
-
|
|
59
|
+
### Async mode:
|
|
61
60
|
|
|
62
61
|
```javascript
|
|
63
62
|
validator.validate(json, schema, function (err, valid) {
|
|
@@ -65,24 +64,17 @@ validator.validate(json, schema, function (err, valid) {
|
|
|
65
64
|
});
|
|
66
65
|
```
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
```javascript
|
|
71
|
-
import ZSchema from 'z-schema/dist/ZSchema.cjs';
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## Browser:
|
|
67
|
+
### Browser:
|
|
75
68
|
|
|
76
69
|
```html
|
|
77
|
-
<script type="text/javascript" src="
|
|
70
|
+
<script type="text/javascript" src="z-schema/umd/ZSchema.min.js"></script>
|
|
78
71
|
<script type="text/javascript">
|
|
79
72
|
var validator = new ZSchema();
|
|
80
|
-
|
|
81
|
-
console.log(valid);
|
|
73
|
+
console.log(validator.validate('string', { type: 'string' }));
|
|
82
74
|
</script>
|
|
83
75
|
```
|
|
84
76
|
|
|
85
|
-
|
|
77
|
+
### Remote references and schemas:
|
|
86
78
|
|
|
87
79
|
In case you have some remote references in your schemas, you have to download those schemas before using validator.
|
|
88
80
|
Otherwise you'll get `UNRESOLVABLE_REFERENCE` error when trying to compile a schema.
|
|
@@ -120,7 +112,7 @@ ZSchema.setSchemaReader(function (uri) {
|
|
|
120
112
|
});
|
|
121
113
|
```
|
|
122
114
|
|
|
123
|
-
|
|
115
|
+
## Features
|
|
124
116
|
|
|
125
117
|
- [Validate against subschema](#validate-against-subschema)
|
|
126
118
|
- [Compile arrays of schemas and use references between them](#compile-arrays-of-schemas-and-use-references-between-them)
|
|
@@ -147,7 +139,7 @@ ZSchema.setSchemaReader(function (uri) {
|
|
|
147
139
|
- [Set validator to collect as many errors as possible](#breakonfirsterror)
|
|
148
140
|
- [Report paths in errors as arrays so they can be processed easier](#reportpathasarray)
|
|
149
141
|
|
|
150
|
-
|
|
142
|
+
### Validate against subschema
|
|
151
143
|
|
|
152
144
|
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:
|
|
153
145
|
|
|
@@ -157,7 +149,7 @@ var valid = validator.validate(cars, schema, { schemaPath: 'definitions.car.defi
|
|
|
157
149
|
|
|
158
150
|
See more details in the [test](/test/spec/schemaPathSpec.js).
|
|
159
151
|
|
|
160
|
-
|
|
152
|
+
### Compile arrays of schemas and use references between them
|
|
161
153
|
|
|
162
154
|
You can use validator to compile an array of schemas that have references between them and then validate against one of those schemas:
|
|
163
155
|
|
|
@@ -225,19 +217,19 @@ ZSchema.registerFormat('xstring', function (str, callback) {
|
|
|
225
217
|
});
|
|
226
218
|
```
|
|
227
219
|
|
|
228
|
-
|
|
220
|
+
### Helper method to check the formats that have been registered
|
|
229
221
|
|
|
230
222
|
```javascript
|
|
231
223
|
var registeredFormats = ZSchema.getRegisteredFormats();
|
|
232
224
|
//registeredFormats will now contain an array of all formats that have been registered with z-schema
|
|
233
225
|
```
|
|
234
226
|
|
|
235
|
-
|
|
227
|
+
### Automatic downloading of remote schemas
|
|
236
228
|
|
|
237
229
|
Automatic downloading of remote schemas was removed from version `3.x` but is still possible with a bit of extra code,
|
|
238
230
|
see [this test](test/spec/AutomaticSchemaLoadingSpec.js) for more information on this.
|
|
239
231
|
|
|
240
|
-
|
|
232
|
+
### Prefill default values to object using format
|
|
241
233
|
|
|
242
234
|
Using format, you can pre-fill values of your choosing into the objects like this:
|
|
243
235
|
|
|
@@ -258,9 +250,9 @@ validator.validate(data, schema);
|
|
|
258
250
|
// data.hello === "world"
|
|
259
251
|
```
|
|
260
252
|
|
|
261
|
-
|
|
253
|
+
## Options
|
|
262
254
|
|
|
263
|
-
|
|
255
|
+
### asyncTimeout
|
|
264
256
|
|
|
265
257
|
Defines a time limit, which should be used when waiting for async tasks like async format validators to perform their validation,
|
|
266
258
|
before the validation fails with an `ASYNC_TIMEOUT` error.
|
|
@@ -271,7 +263,7 @@ var validator = new ZSchema({
|
|
|
271
263
|
});
|
|
272
264
|
```
|
|
273
265
|
|
|
274
|
-
|
|
266
|
+
### noEmptyArrays
|
|
275
267
|
|
|
276
268
|
When true, validator will assume that minimum count of items in any `array` is 1, except when `minItems: 0` is explicitly defined.
|
|
277
269
|
|
|
@@ -281,7 +273,7 @@ var validator = new ZSchema({
|
|
|
281
273
|
});
|
|
282
274
|
```
|
|
283
275
|
|
|
284
|
-
|
|
276
|
+
### noEmptyStrings
|
|
285
277
|
|
|
286
278
|
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.
|
|
287
279
|
|
|
@@ -291,7 +283,7 @@ var validator = new ZSchema({
|
|
|
291
283
|
});
|
|
292
284
|
```
|
|
293
285
|
|
|
294
|
-
|
|
286
|
+
### noTypeless
|
|
295
287
|
|
|
296
288
|
When true, validator will fail validation for schemas that don't specify a `type` of object that they expect.
|
|
297
289
|
|
|
@@ -301,7 +293,7 @@ var validator = new ZSchema({
|
|
|
301
293
|
});
|
|
302
294
|
```
|
|
303
295
|
|
|
304
|
-
|
|
296
|
+
### noExtraKeywords
|
|
305
297
|
|
|
306
298
|
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.
|
|
307
299
|
|
|
@@ -311,7 +303,7 @@ var validator = new ZSchema({
|
|
|
311
303
|
});
|
|
312
304
|
```
|
|
313
305
|
|
|
314
|
-
|
|
306
|
+
### assumeAdditional
|
|
315
307
|
|
|
316
308
|
When true, validator assumes that additionalItems/additionalProperties are defined as false so you don't have to manually fix all your schemas.
|
|
317
309
|
|
|
@@ -329,7 +321,7 @@ var validator = new ZSchema({
|
|
|
329
321
|
});
|
|
330
322
|
```
|
|
331
323
|
|
|
332
|
-
|
|
324
|
+
### forceAdditional
|
|
333
325
|
|
|
334
326
|
When true, validator doesn't validate schemas where additionalItems/additionalProperties should be defined to either true or false.
|
|
335
327
|
|
|
@@ -339,7 +331,7 @@ var validator = new ZSchema({
|
|
|
339
331
|
});
|
|
340
332
|
```
|
|
341
333
|
|
|
342
|
-
|
|
334
|
+
### forceItems
|
|
343
335
|
|
|
344
336
|
When true, validator doesn't validate schemas where `items` are not defined for `array` type schemas.
|
|
345
337
|
This is to avoid passing anything through an array definition.
|
|
@@ -350,7 +342,7 @@ var validator = new ZSchema({
|
|
|
350
342
|
});
|
|
351
343
|
```
|
|
352
344
|
|
|
353
|
-
|
|
345
|
+
### forceMinItems
|
|
354
346
|
|
|
355
347
|
When true, validator doesn't validate schemas where `minItems` is not defined for `array` type schemas.
|
|
356
348
|
This is to avoid passing zero-length arrays which application doesn't expect to handle.
|
|
@@ -361,7 +353,7 @@ var validator = new ZSchema({
|
|
|
361
353
|
});
|
|
362
354
|
```
|
|
363
355
|
|
|
364
|
-
|
|
356
|
+
### forceMaxItems
|
|
365
357
|
|
|
366
358
|
When true, validator doesn't validate schemas where `maxItems` is not defined for `array` type schemas.
|
|
367
359
|
This is to avoid passing arrays with unlimited count of elements which application doesn't expect to handle.
|
|
@@ -372,7 +364,7 @@ var validator = new ZSchema({
|
|
|
372
364
|
});
|
|
373
365
|
```
|
|
374
366
|
|
|
375
|
-
|
|
367
|
+
### forceMinLength
|
|
376
368
|
|
|
377
369
|
When true, validator doesn't validate schemas where `minLength` is not defined for `string` type schemas.
|
|
378
370
|
This is to avoid passing zero-length strings which application doesn't expect to handle.
|
|
@@ -383,7 +375,7 @@ var validator = new ZSchema({
|
|
|
383
375
|
});
|
|
384
376
|
```
|
|
385
377
|
|
|
386
|
-
|
|
378
|
+
### forceMaxLength
|
|
387
379
|
|
|
388
380
|
When true, validator doesn't validate schemas where `maxLength` is not defined for `string` type schemas.
|
|
389
381
|
This is to avoid passing extremly large strings which application doesn't expect to handle.
|
|
@@ -394,7 +386,7 @@ var validator = new ZSchema({
|
|
|
394
386
|
});
|
|
395
387
|
```
|
|
396
388
|
|
|
397
|
-
|
|
389
|
+
### forceProperties
|
|
398
390
|
|
|
399
391
|
When true, validator doesn't validate schemas where `properties` or `patternProperties` is not defined for `object` type schemas.
|
|
400
392
|
This is to avoid having objects with unexpected properties in application.
|
|
@@ -405,7 +397,7 @@ var validator = new ZSchema({
|
|
|
405
397
|
});
|
|
406
398
|
```
|
|
407
399
|
|
|
408
|
-
|
|
400
|
+
### ignoreUnresolvableReferences
|
|
409
401
|
|
|
410
402
|
When true, validator doesn't end with error when a remote reference is unreachable. **This setting is not recommended in production outside of testing.**
|
|
411
403
|
|
|
@@ -415,7 +407,7 @@ var validator = new ZSchema({
|
|
|
415
407
|
});
|
|
416
408
|
```
|
|
417
409
|
|
|
418
|
-
|
|
410
|
+
### enumCaseInsensitiveComparison
|
|
419
411
|
|
|
420
412
|
When true, validator will return a `ENUM_CASE_MISMATCH` when the enum values mismatch only in case.
|
|
421
413
|
|
|
@@ -425,7 +417,7 @@ var validator = new ZSchema({
|
|
|
425
417
|
});
|
|
426
418
|
```
|
|
427
419
|
|
|
428
|
-
|
|
420
|
+
### strictUris
|
|
429
421
|
|
|
430
422
|
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).
|
|
431
423
|
|
|
@@ -435,7 +427,7 @@ var validator = new ZSchema({
|
|
|
435
427
|
});
|
|
436
428
|
```
|
|
437
429
|
|
|
438
|
-
|
|
430
|
+
### strictMode
|
|
439
431
|
|
|
440
432
|
Strict mode of z-schema is currently equal to the following:
|
|
441
433
|
|
|
@@ -458,7 +450,7 @@ var validator = new ZSchema({
|
|
|
458
450
|
});
|
|
459
451
|
```
|
|
460
452
|
|
|
461
|
-
|
|
453
|
+
### breakOnFirstError
|
|
462
454
|
|
|
463
455
|
default: `false`<br />
|
|
464
456
|
When true, will stop validation after the first error is found:
|
|
@@ -469,7 +461,7 @@ var validator = new ZSchema({
|
|
|
469
461
|
});
|
|
470
462
|
```
|
|
471
463
|
|
|
472
|
-
|
|
464
|
+
### reportPathAsArray
|
|
473
465
|
|
|
474
466
|
Report error paths as an array of path segments instead of a string:
|
|
475
467
|
|
|
@@ -479,7 +471,7 @@ var validator = new ZSchema({
|
|
|
479
471
|
});
|
|
480
472
|
```
|
|
481
473
|
|
|
482
|
-
|
|
474
|
+
### ignoreUnknownFormats
|
|
483
475
|
|
|
484
476
|
By default, z-schema reports all unknown formats, formats not defined by JSON Schema and not registered using
|
|
485
477
|
`ZSchema.registerFormat`, as an error. But the
|
|
@@ -493,7 +485,7 @@ var validator = new ZSchema({
|
|
|
493
485
|
});
|
|
494
486
|
```
|
|
495
487
|
|
|
496
|
-
|
|
488
|
+
### includeErrors
|
|
497
489
|
|
|
498
490
|
By default, z-schema reports all errors. If interested only in a subset of the errors, passing the option `includeErrors` to `validate` will perform validations only for those errors.
|
|
499
491
|
|
|
@@ -503,7 +495,7 @@ var validator = new ZSchema();
|
|
|
503
495
|
validator.validate(json, schema, { includeErrors: ['INVALID_TYPE'] });
|
|
504
496
|
```
|
|
505
497
|
|
|
506
|
-
|
|
498
|
+
### customValidator
|
|
507
499
|
|
|
508
500
|
**Warning**: Use only if know what you are doing. Always consider using [custom format](#register-a-custom-format) before using this option.
|
|
509
501
|
|
|
@@ -604,7 +596,7 @@ console.log(validator.getLastErrors());
|
|
|
604
596
|
|
|
605
597
|
**Note:** before creating your own keywords you should consider all compatibility issues.
|
|
606
598
|
|
|
607
|
-
|
|
599
|
+
## Benchmarks
|
|
608
600
|
|
|
609
601
|
So how does it compare to version 2.x and others?
|
|
610
602
|
|
|
@@ -612,7 +604,13 @@ So how does it compare to version 2.x and others?
|
|
|
612
604
|
|
|
613
605
|
[rawgithub.com/zaggino/z-schema/master/benchmark/results.html](https://rawgithub.com/zaggino/z-schema/master/benchmark/results.html)
|
|
614
606
|
|
|
615
|
-
|
|
607
|
+
## Contributing:
|
|
608
|
+
|
|
609
|
+
These repository has several submodules and should be cloned as follows:
|
|
610
|
+
|
|
611
|
+
> git clone **--recursive** https://github.com/zaggino/z-schema.git
|
|
612
|
+
|
|
613
|
+
## Contributors
|
|
616
614
|
|
|
617
615
|
Thanks for contributing to:
|
|
618
616
|
|