z-schema 3.18.0 → 3.18.4

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