1
1
# is-my-json-valid
2
2
3
- A [ JSONSchema] ( http://json-schema.org/ ) validator that uses code generation
4
- to be extremely fast
5
-
6
- ```
7
- npm install is-my-json-valid
8
- ```
3
+ A [ JSONSchema] ( https://json-schema.org/ ) validator that uses code generation to be extremely fast.
9
4
10
5
It passes the entire JSONSchema v4 test suite except for ` remoteRefs ` and ` maxLength ` /` minLength ` when using unicode surrogate pairs.
11
6
12
- [ ![ build status] ( http://img.shields.io/travis/mafintosh/is-my-json-valid.svg?style=flat )] ( http://travis-ci.org/mafintosh/is-my-json-valid )
7
+ [ ![ build status] ( https://img.shields.io/travis/mafintosh/is-my-json-valid.svg?style=flat )] ( https://travis-ci.org/mafintosh/is-my-json-valid )
8
+
9
+ ## Installation
10
+
11
+ ``` sh
12
+ npm install --save is-my-json-valid
13
+ ```
13
14
14
15
## Usage
15
16
16
17
Simply pass a schema to compile it
17
18
18
- ``` js
19
+ ``` js
19
20
var validator = require (' is-my-json-valid' )
20
21
21
22
var validate = validator ({
@@ -39,13 +40,13 @@ console.log(validate.errors)
39
40
40
41
You can also pass the schema as a string
41
42
42
- ``` js
43
+ ``` js
43
44
var validate = validator (' {"type": ... }' )
44
45
```
45
46
46
47
Optionally you can use the require submodule to load a schema from ` __dirname `
47
48
48
- ``` js
49
+ ``` js
49
50
var validator = require (' is-my-json-valid/require' )
50
51
var validate = validator (' my-schema.json' )
51
52
```
@@ -55,7 +56,7 @@ var validate = validator('my-schema.json')
55
56
is-my-json-valid supports the formats specified in JSON schema v4 (such as date-time).
56
57
If you want to add your own custom formats pass them as the formats options to the validator
57
58
58
- ``` js
59
+ ``` js
59
60
var validate = validator ({
60
61
type: ' string' ,
61
62
required: true ,
@@ -74,7 +75,7 @@ console.log(validate('ab')) // false
74
75
75
76
You can pass in external schemas that you reference using the ` $ref ` attribute as the ` schemas ` option
76
77
77
- ``` js
78
+ ``` js
78
79
var ext = {
79
80
required: true ,
80
81
type: ' string'
@@ -95,7 +96,7 @@ validate(42) // return false
95
96
96
97
is-my-json-valid supports filtering away properties not in the schema
97
98
98
- ``` js
99
+ ``` js
99
100
var filter = validator .filter ({
100
101
required: true ,
101
102
type: ' object' ,
@@ -116,7 +117,7 @@ When the `verbose` options is set to `true`, `is-my-json-valid` also outputs:
116
117
- ` value ` : The data value that caused the error
117
118
- ` schemaPath ` : an array of keys indicating which sub-schema failed
118
119
119
- ``` js
120
+ ``` js
120
121
var schema = {
121
122
required: true ,
122
123
type: ' object' ,
@@ -141,7 +142,8 @@ console.log(validate.errors)
141
142
```
142
143
143
144
Many popular libraries make it easy to retrieve the failing rule with the ` schemaPath ` :
144
- ```
145
+
146
+ ``` js
145
147
var schemaPath = validate .errors [0 ].schemaPath
146
148
var R = require (' ramda' )
147
149
@@ -160,7 +162,7 @@ console.log( 'All evaluate to the same thing: ', R.equals(
160
162
By default is-my-json-valid bails on first validation error but when greedy is
161
163
set to true it tries to validate as much as possible:
162
164
163
- ``` js
165
+ ``` js
164
166
var validate = validator ({
165
167
type: ' object' ,
166
168
properties: {
@@ -182,40 +184,40 @@ console.log(validate.errors) // [{field: 'data.y', message: 'is required'},
182
184
183
185
Here is a list of possible ` message ` values for errors:
184
186
185
- * ` is required `
186
- * ` is the wrong type `
187
- * ` has additional items `
188
- * ` must be FORMAT format ` (FORMAT is the ` format ` property from the schema)
189
- * ` must be unique `
190
- * ` must be an enum value `
191
- * ` dependencies not set `
192
- * ` has additional properties `
193
- * ` referenced schema does not match `
194
- * ` negative schema matches `
195
- * ` pattern mismatch `
196
- * ` no schemas match `
197
- * ` no (or more than one) schemas match `
198
- * ` has a remainder `
199
- * ` has more properties than allowed `
200
- * ` has less properties than allowed `
201
- * ` has more items than allowed `
202
- * ` has less items than allowed `
203
- * ` has longer length than allowed `
204
- * ` has less length than allowed `
205
- * ` is less than minimum `
206
- * ` is more than maximum `
187
+ - ` is required `
188
+ - ` is the wrong type `
189
+ - ` has additional items `
190
+ - ` must be FORMAT format ` (FORMAT is the ` format ` property from the schema)
191
+ - ` must be unique `
192
+ - ` must be an enum value `
193
+ - ` dependencies not set `
194
+ - ` has additional properties `
195
+ - ` referenced schema does not match `
196
+ - ` negative schema matches `
197
+ - ` pattern mismatch `
198
+ - ` no schemas match `
199
+ - ` no (or more than one) schemas match `
200
+ - ` has a remainder `
201
+ - ` has more properties than allowed `
202
+ - ` has less properties than allowed `
203
+ - ` has more items than allowed `
204
+ - ` has less items than allowed `
205
+ - ` has longer length than allowed `
206
+ - ` has less length than allowed `
207
+ - ` is less than minimum `
208
+ - ` is more than maximum `
207
209
208
210
## Performance
209
211
210
212
is-my-json-valid uses code generation to turn your JSON schema into basic javascript code that is easily optimizeable by v8.
211
213
212
- At the time of writing, is-my-json-valid is the __ fastest validator __ when running
214
+ At the time of writing, is-my-json-valid is the ** fastest validator ** when running
213
215
214
- * [ json-schema-benchmark] ( https://github.com/Muscula/json-schema-benchmark )
215
- * [ cosmicreals.com benchmark] ( http://cosmicrealms.com/blog/2014/08/29/benchmark-of-node-dot-js-json-validation-modules-part-3/ )
216
- * [ jsck benchmark] ( https://github.com/pandastrike/jsck/issues/72#issuecomment-70992684 )
217
- * [ themis benchmark] ( https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html )
218
- * [ z-schema benchmark] ( https://rawgit.com/zaggino/z-schema/master/benchmark/results.html )
216
+ - [ json-schema-benchmark] ( https://github.com/Muscula/json-schema-benchmark )
217
+ - [ cosmicreals.com benchmark] ( http://cosmicrealms.com/blog/2014/08/29/benchmark-of-node-dot-js-json-validation-modules-part-3/ )
218
+ - [ jsck benchmark] ( https://github.com/pandastrike/jsck/issues/72#issuecomment-70992684 )
219
+ - [ themis benchmark] ( https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html )
220
+ - [ z-schema benchmark] ( https://rawgit.com/zaggino/z-schema/master/benchmark/results.html )
219
221
220
222
If you know any other relevant benchmarks open a PR and I'll add them.
221
223
0 commit comments