What is is-my-json-valid?
The is-my-json-valid npm package is a JSON schema validator that is fast and simple to use. It allows you to validate JSON data against a schema, ensuring that the data conforms to the expected structure and types.
What are is-my-json-valid's main functionalities?
Basic JSON Validation
This feature allows you to validate a simple JSON object against a schema. In this example, the schema expects an object with a string 'name' and a number 'age'. The provided JSON object meets these criteria, so the validation returns true.
const validator = require('is-my-json-valid');
const validate = validator({ type: 'object', properties: { name: { type: 'string' }, age: { type: 'number' } } });
const valid = validate({ name: 'John', age: 30 });
console.log(valid); // true
Custom Error Messages
This feature provides detailed error messages when validation fails. In this example, the 'age' property is a string instead of a number, so the validation fails and the errors are logged.
const validator = require('is-my-json-valid');
const validate = validator({ type: 'object', properties: { name: { type: 'string' }, age: { type: 'number' } } }, { verbose: true });
const valid = validate({ name: 'John', age: 'thirty' });
if (!valid) console.log(validate.errors);
Formats and Custom Formats
This feature allows you to use predefined formats like 'email' to validate specific types of strings. In this example, the 'email' property must be a valid email address.
const validator = require('is-my-json-valid');
const validate = validator({ type: 'object', properties: { email: { type: 'string', format: 'email' } } });
const valid = validate({ email: 'test@example.com' });
console.log(valid); // true
Other packages similar to is-my-json-valid
ajv
AJV (Another JSON Schema Validator) is a highly efficient and feature-rich JSON schema validator. It supports JSON Schema draft-07 and provides advanced features like asynchronous validation, custom keywords, and more. Compared to is-my-json-valid, AJV offers more extensive support for JSON Schema specifications and additional features.
jsonschema
The jsonschema package is a simple and easy-to-use JSON schema validator. It supports JSON Schema draft-04 and is suitable for basic validation needs. While it may not be as fast as is-my-json-valid, it provides a straightforward API for validating JSON data.
joi
Joi is a powerful schema description language and data validator for JavaScript objects. It allows you to define schemas using a fluent API and provides extensive validation capabilities. Unlike is-my-json-valid, Joi is not limited to JSON Schema and offers more flexibility in defining and validating data structures.
is-my-json-valid
A JSONSchema validator that uses code generation
to be extremely fast
npm install is-my-json-valid
It passes the entire JSONSchema v4 test suite except for remoteRefs
and maxLength
/minLength
when using unicode surrogate pairs.
Usage
Simply pass a schema to compile it
var validator = require('is-my-json-valid')
var validate = validator({
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
})
console.log('should be valid', validate({hello: 'world'}))
console.log('should not be valid', validate({}))
console.log(validate.errors)
You can also pass the schema as a string
var validate = validate('{"type": ... }')
Optionally you can use the require submodule to load a schema from __dirname
var validator = require('is-my-json-valid/require')
var validate = validator('my-schema.json')
Custom formats
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
var validate = validator({
type: 'string',
required: true,
format: 'only-a'
}, {
formats: {
'only-a': /^a+$/
}
})
console.log(validate('aa'))
console.log(validate('ab'))
External schemas
You can pass in external schemas that you reference using the $ref
attribute as the schemas
option
var ext = {
required: true,
type: 'string'
}
var schema = {
$ref: '#ext'
}
var validate = validate(schema, {schemas: {ext: ext}})
validate('hello')
validate(42)
Filtering away additional properties
is-my-json-valid supports filtering away properties not in the schema
var filter = validator.filter({
required: true,
type: 'object',
properties: {
hello: {type: 'string', required: true}
},
additionalProperties: false
})
var doc = {hello: 'world', notInSchema: true}
console.log(filter(doc))
Verbose mode outputs the value on errors
is-my-json-valid outputs the value causing an error when verbose is set to true
var validate = validator({
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
}, {
verbose: true
})
validate({hello: 100});
console.log(validate.errors)
Greedy mode tries to validate as much as possible
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:
var validate = validator({
type: 'object',
properties: {
x: {
type: 'number'
}
},
required: ['x', 'y']
}, {
greedy: true
});
validate({x: 'string'});
console.log(validate.errors)
Performance
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.
License
MIT