Gathering detailed insights and metrics for is-my-json-valid
Gathering detailed insights and metrics for is-my-json-valid
Gathering detailed insights and metrics for is-my-json-valid
Gathering detailed insights and metrics for is-my-json-valid
@types/is-my-json-valid
Type definitions for is-my-json-valid from https://www.github.com/DefinitelyTyped/DefinitelyTyped
is-my-ssb-valid
build scuttlebutt message validators from JSON Schema
@bahmutov/is-my-json-valid
A JSONSchema validator that uses code generation to be extremely fast
assert-my-json-valid
Use is-my-json-valid to validate, throw if error
A JSONSchema validator that uses code generation to be extremely fast
npm install is-my-json-valid
Typescript
Module System
Node Version
NPM Version
99.3
Supply Chain
99.5
Quality
82
Maintenance
100
Vulnerability
100
License
JavaScript (77.29%)
TypeScript (22.71%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
968 Stars
236 Commits
112 Forks
26 Watchers
3 Branches
25 Contributors
Updated on Jun 07, 2025
Latest Version
2.20.6
Package Id
is-my-json-valid@2.20.6
Unpacked Size
39.24 kB
Size
9.57 kB
File Count
7
NPM Version
8.0.0
Node Version
16.11.1
Published on
Nov 10, 2021
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
A JSONSchema validator that uses code generation to be extremely fast.
It passes the entire JSONSchema v4 test suite except for remoteRefs
and maxLength
/minLength
when using unicode surrogate pairs.
1npm install --save is-my-json-valid
Simply pass a schema to compile it
1var validator = require('is-my-json-valid') 2 3var validate = validator({ 4 required: true, 5 type: 'object', 6 properties: { 7 hello: { 8 required: true, 9 type: 'string' 10 } 11 } 12}) 13 14console.log('should be valid', validate({hello: 'world'})) 15console.log('should not be valid', validate({})) 16 17// get the last list of errors by checking validate.errors 18// the following will print [{field: 'data.hello', message: 'is required'}] 19console.log(validate.errors)
You can also pass the schema as a string
1var validate = validator('{"type": ... }')
Optionally you can use the require submodule to load a schema from __dirname
1var validator = require('is-my-json-valid/require') 2var validate = validator('my-schema.json')
is-my-json-valid supports the formats specified in JSON schema v4 (such as date-time). If you want to add your own custom formats pass them as the formats options to the validator
1var validate = validator({ 2 type: 'string', 3 required: true, 4 format: 'only-a' 5}, { 6 formats: { 7 'only-a': /^a+$/ 8 } 9}) 10 11console.log(validate('aa')) // true 12console.log(validate('ab')) // false
You can pass in external schemas that you reference using the $ref
attribute as the schemas
option
1var ext = { 2 required: true, 3 type: 'string' 4} 5 6var schema = { 7 $ref: '#ext' // references another schema called ext 8} 9 10// pass the external schemas as an option 11var validate = validator(schema, {schemas: {ext: ext}}) 12 13validate('hello') // returns true 14validate(42) // return false
is-my-json-valid supports filtering away properties not in the schema
1var filter = validator.filter({ 2 required: true, 3 type: 'object', 4 properties: { 5 hello: {type: 'string', required: true} 6 }, 7 additionalProperties: false 8}) 9 10var doc = {hello: 'world', notInSchema: true} 11console.log(filter(doc)) // {hello: 'world'}
When the verbose
options is set to true
, is-my-json-valid
also outputs:
value
: The data value that caused the errorschemaPath
: an array of keys indicating which sub-schema failed1var schema = { 2 required: true, 3 type: 'object', 4 properties: { 5 hello: { 6 required: true, 7 type: 'string' 8 } 9 } 10} 11var validate = validator(schema, { 12 verbose: true 13}) 14 15validate({hello: 100}); 16console.log(validate.errors) 17// [ { field: 'data.hello', 18// message: 'is the wrong type', 19// value: 100, 20// type: 'string', 21// schemaPath: [ 'properties', 'hello' ] } ]
Many popular libraries make it easy to retrieve the failing rule with the schemaPath
:
1var schemaPath = validate.errors[0].schemaPath 2var R = require('ramda') 3 4console.log( 'All evaluate to the same thing: ', R.equals( 5 schema.properties.hello, 6 { required: true, type: 'string' }, 7 R.path(schemaPath, schema), 8 require('lodash').get(schema, schemaPath), 9 require('jsonpointer').get(schema, [""].concat(schemaPath)) 10)) 11// All evaluate to the same thing: true
By default is-my-json-valid bails on first validation error but when greedy is set to true it tries to validate as much as possible:
1var validate = validator({ 2 type: 'object', 3 properties: { 4 x: { 5 type: 'number' 6 } 7 }, 8 required: ['x', 'y'] 9}, { 10 greedy: true 11}); 12 13validate({x: 'string'}); 14console.log(validate.errors) // [{field: 'data.y', message: 'is required'}, 15 // {field: 'data.x', message: 'is the wrong type'}]
Here is a list of possible message
values for errors:
is required
is the wrong type
has additional items
must be FORMAT format
(FORMAT is the format
property from the schema)must be unique
must be an enum value
dependencies not set
has additional properties
referenced schema does not match
negative schema matches
pattern mismatch
no schemas match
no (or more than one) schemas match
has a remainder
has more properties than allowed
has less properties than allowed
has more items than allowed
has less items than allowed
has longer length than allowed
has less length than allowed
is less than minimum
is more than maximum
is-my-json-valid uses code generation to turn your JSON schema into basic javascript code that is easily optimizeable by v8.
At the time of writing, is-my-json-valid is the fastest validator when running
If you know any other relevant benchmarks open a PR and I'll add them.
This library ships with TypeScript typings. They are still early on and not perfect at the moment, but should hopefully handle the most common cases. If you find anything that doesn't work, please open an issue and we'll try to solve it.
The typings are using unknown
and thus require TypeScript 3.0 or later.
Here is a quick sample of usage together with express:
1import createError = require('http-errors') 2import createValidator = require('is-my-json-valid') 3import { Request, Response, NextFunction } from 'express' 4 5const personValidator = createValidator({ 6 type: 'object', 7 properties: { 8 name: { type: 'string' }, 9 age: { type: 'number' }, 10 }, 11 required: [ 12 'name' 13 ] 14}) 15 16export function post (req: Request, res: Response, next: NextFunction) { 17 // Here req.body is typed as: any 18 19 if (!personValidator(req.body)) { 20 throw createError(400, { errors: personValidator.errors }) 21 } 22 23 // Here req.body is typed as: { name: string, age: number | undefined } 24}
As you can see, the typings for is-my-json-valid will contruct an interface from the schema passed in. This allows you to work with your incoming json body in a type safe way.
MIT
7.5/10
Summary
Regular Expression Denial of Service in is-my-json-valid
Affected Versions
< 2.17.2
Patched Versions
2.17.2
5.3/10
Summary
Regular expression deinal of service (ReDoS) in is-my-json-valid
Affected Versions
< 1.4.1
Patched Versions
1.4.1
5.3/10
Summary
Regular expression deinal of service (ReDoS) in is-my-json-valid
Affected Versions
>= 2.0.0, < 2.17.2
Patched Versions
2.17.2
0/10
Summary
Moderate severity vulnerability that affects is-my-json-valid
Affected Versions
<= 2.12.3
Patched Versions
2.12.4
0/10
Summary
Regular Expression Denial of Service in is-my-json-valid
Affected Versions
< 1.4.1
Patched Versions
1.4.1
0/10
Summary
Regular Expression Denial of Service in is-my-json-valid
Affected Versions
>= 2.0.0, < 2.17.2
Patched Versions
2.17.2
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More