11# is-my-json-valid
22
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.
94
105It passes the entire JSONSchema v4 test suite except for ` remoteRefs ` and ` maxLength ` /` minLength ` when using unicode surrogate pairs.
116
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+ ```
1314
1415## Usage
1516
1617Simply pass a schema to compile it
1718
18- ``` js
19+ ``` js
1920var validator = require (' is-my-json-valid' )
2021
2122var validate = validator ({
@@ -39,13 +40,13 @@ console.log(validate.errors)
3940
4041You can also pass the schema as a string
4142
42- ``` js
43+ ``` js
4344var validate = validator (' {"type": ... }' )
4445```
4546
4647Optionally you can use the require submodule to load a schema from ` __dirname `
4748
48- ``` js
49+ ``` js
4950var validator = require (' is-my-json-valid/require' )
5051var validate = validator (' my-schema.json' )
5152```
@@ -55,7 +56,7 @@ var validate = validator('my-schema.json')
5556is-my-json-valid supports the formats specified in JSON schema v4 (such as date-time).
5657If you want to add your own custom formats pass them as the formats options to the validator
5758
58- ``` js
59+ ``` js
5960var validate = validator ({
6061 type: ' string' ,
6162 required: true ,
@@ -74,7 +75,7 @@ console.log(validate('ab')) // false
7475
7576You can pass in external schemas that you reference using the ` $ref ` attribute as the ` schemas ` option
7677
77- ``` js
78+ ``` js
7879var ext = {
7980 required: true ,
8081 type: ' string'
@@ -95,7 +96,7 @@ validate(42) // return false
9596
9697is-my-json-valid supports filtering away properties not in the schema
9798
98- ``` js
99+ ``` js
99100var filter = validator .filter ({
100101 required: true ,
101102 type: ' object' ,
@@ -116,7 +117,7 @@ When the `verbose` options is set to `true`, `is-my-json-valid` also outputs:
116117- ` value ` : The data value that caused the error
117118- ` schemaPath ` : an array of keys indicating which sub-schema failed
118119
119- ``` js
120+ ``` js
120121var schema = {
121122 required: true ,
122123 type: ' object' ,
@@ -141,7 +142,8 @@ console.log(validate.errors)
141142```
142143
143144Many popular libraries make it easy to retrieve the failing rule with the ` schemaPath ` :
144- ```
145+
146+ ``` js
145147var schemaPath = validate .errors [0 ].schemaPath
146148var R = require (' ramda' )
147149
@@ -160,7 +162,7 @@ console.log( 'All evaluate to the same thing: ', R.equals(
160162By default is-my-json-valid bails on first validation error but when greedy is
161163set to true it tries to validate as much as possible:
162164
163- ``` js
165+ ``` js
164166var validate = validator ({
165167 type: ' object' ,
166168 properties: {
@@ -182,40 +184,40 @@ console.log(validate.errors) // [{field: 'data.y', message: 'is required'},
182184
183185Here is a list of possible ` message ` values for errors:
184186
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 `
207209
208210## Performance
209211
210212is-my-json-valid uses code generation to turn your JSON schema into basic javascript code that is easily optimizeable by v8.
211213
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
213215
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 )
219221
220222If you know any other relevant benchmarks open a PR and I'll add them.
221223
0 commit comments