ajv
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Top Related Projects
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
JSON Schema validation
Tiny Validator for JSON Schema v4
The most powerful data validation library for JS
JSON Schema Based Editor
Quick Overview
Ajv is a popular JSON schema validator for JavaScript and TypeScript. It provides fast and efficient validation of JSON data against schemas, supporting JSON Schema draft-04/06/07 and JSON Type Definition (RFC 8927).
Pros
- High performance due to code generation
- Supports multiple JSON Schema versions and JSON Type Definition
- Extensive validation features, including custom keywords and formats
- TypeScript support with type inference
Cons
- Learning curve for advanced features and custom keywords
- Large bundle size when using all features
- Some users report occasional inconsistencies with certain edge cases
- Documentation can be overwhelming for beginners
Code Examples
- Basic schema validation:
import Ajv from "ajv"
const ajv = new Ajv()
const schema = {
type: "object",
properties: {
foo: { type: "integer" },
bar: { type: "string" }
},
required: ["foo"],
additionalProperties: false
}
const validate = ajv.compile(schema)
console.log(validate({ foo: 1, bar: "abc" })) // true
console.log(validate({ foo: "1" })) // false
- Using custom formats:
import Ajv from "ajv"
import addFormats from "ajv-formats"
const ajv = new Ajv()
addFormats(ajv)
const schema = {
type: "object",
properties: {
email: { type: "string", format: "email" },
date: { type: "string", format: "date" }
}
}
const validate = ajv.compile(schema)
console.log(validate({ email: "user@example.com", date: "2023-05-01" })) // true
console.log(validate({ email: "invalid", date: "not-a-date" })) // false
- Using JSON Type Definition:
import Ajv from "ajv/dist/jtd"
const ajv = new Ajv()
const schema = {
properties: {
name: { type: "string" },
age: { type: "uint8" }
}
}
const validate = ajv.compile(schema)
console.log(validate({ name: "John", age: 30 })) // true
console.log(validate({ name: "Jane", age: "25" })) // false
Getting Started
To use Ajv in your project, follow these steps:
- Install Ajv:
npm install ajv
- Import and create an Ajv instance:
import Ajv from "ajv"
const ajv = new Ajv()
- Define a schema and compile it:
const schema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" }
},
required: ["name"]
}
const validate = ajv.compile(schema)
- Use the validator function:
const data = { name: "John", age: 30 }
if (validate(data)) {
console.log("Valid!")
} else {
console.log("Invalid:", validate.errors)
}
Competitor Comparisons
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Pros of ajv
- More comprehensive and feature-rich JSON schema validator
- Supports draft-07 and later versions of JSON Schema
- Extensive documentation and community support
Cons of ajv
- Larger bundle size due to more features
- Steeper learning curve for advanced features
- May be overkill for simple validation needs
Code Comparison
ajv:
const Ajv = require("ajv")
const ajv = new Ajv()
const schema = {
type: "object",
properties: {
foo: { type: "integer" },
bar: { type: "string" }
},
required: ["foo"],
additionalProperties: false
}
const validate = ajv.compile(schema)
console.log(validate({foo: 1, bar: "abc"})) // true
console.log(validate({foo: "1", bar: "abc"})) // false
ajv:
// The code for ajv would be identical to the above example,
// as they are the same project. The comparison is not applicable
// in this case since we're comparing the same repository.
Note: The comparison between ajv-validator/ajv and ajv-validator/ajv is not meaningful as they refer to the same repository. The pros, cons, and code example provided are for ajv itself, not in comparison to another project.
JSON Schema validation
Pros of jsonschema
- Simpler API and easier to get started for basic use cases
- Lighter weight with fewer dependencies
- Better support for older Node.js versions
Cons of jsonschema
- Less performant for large schemas or high-volume validation
- Fewer advanced features and customization options
- Less active development and community support
Code Comparison
jsonschema:
var Validator = require('jsonschema').Validator;
var v = new Validator();
var instance = 4;
var schema = {"type": "number"};
console.log(v.validate(instance, schema));
Ajv:
const Ajv = require("ajv")
const ajv = new Ajv()
const schema = {type: "number"}
const validate = ajv.compile(schema)
console.log(validate(4))
Both libraries provide JSON Schema validation, but Ajv offers more advanced features, better performance, and wider adoption in the JavaScript ecosystem. jsonschema is simpler and may be sufficient for basic use cases, but Ajv is generally recommended for more complex or performance-critical applications. Ajv also has better TypeScript support and more frequent updates. However, jsonschema might be preferred in scenarios where a lighter-weight solution or compatibility with older Node.js versions is required.
Tiny Validator for JSON Schema v4
Pros of tv4
- Lightweight and simple to use
- Supports older versions of JSON Schema (draft-04)
- Easier to integrate into existing projects due to its simplicity
Cons of tv4
- Less actively maintained compared to Ajv
- Limited support for newer JSON Schema features
- May have performance limitations for large-scale validation tasks
Code Comparison
tv4:
var valid = tv4.validate(data, schema);
if (!valid) {
console.log(tv4.error);
}
Ajv:
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(data);
if (!valid) console.log(validate.errors);
Key Differences
- Ajv is more actively maintained and updated
- Ajv supports newer JSON Schema drafts and features
- Ajv offers better performance for large-scale validation
- tv4 has a simpler API and is easier to get started with
- Ajv provides more advanced features like custom keywords and formats
Use Cases
- Choose tv4 for simpler projects or when working with older JSON Schema versions
- Opt for Ajv in larger projects, when performance is crucial, or when using newer JSON Schema features
The most powerful data validation library for JS
Pros of Joi
- More intuitive API with chainable methods for schema definition
- Built-in support for custom error messages and localization
- Extensive validation options for common use cases (e.g., credit cards, emails)
Cons of Joi
- Slower performance compared to Ajv, especially for large datasets
- Larger bundle size, which may impact client-side applications
- Less support for JSON Schema standards and formats
Code Comparison
Joi schema definition:
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
email: Joi.string().email().required()
});
Ajv schema definition:
const schema = {
type: "object",
properties: {
username: { type: "string", pattern: "^[a-zA-Z0-9]{3,30}$" },
email: { type: "string", format: "email" }
},
required: ["username", "email"]
};
Both Joi and Ajv are popular JavaScript libraries for data validation, but they have different strengths and use cases. Joi offers a more user-friendly API and extensive validation options, making it suitable for complex validation scenarios. Ajv, on the other hand, focuses on performance and JSON Schema compliance, making it a better choice for high-performance applications or when working with standardized schemas.
JSON Schema Based Editor
Pros of json-editor
- Provides a complete UI for editing JSON schemas, including form generation
- Offers a more user-friendly interface for non-technical users
- Includes built-in validation and error reporting in the UI
Cons of json-editor
- Less focused on pure JSON Schema validation
- Not as actively maintained (last update in 2019)
- More limited in terms of customization and extensibility
Code Comparison
json-editor:
var editor = new JSONEditor(document.getElementById('editor_holder'), {
schema: {
type: "object",
properties: {
name: { type: "string" }
}
}
});
Ajv:
const Ajv = require("ajv")
const ajv = new Ajv()
const validate = ajv.compile({
type: "object",
properties: {
name: { type: "string" }
}
})
Summary
While json-editor provides a complete UI solution for editing JSON schemas, Ajv focuses on high-performance JSON Schema validation. json-editor is better suited for projects requiring a user-friendly interface for JSON editing, while Ajv is ideal for backend validation and more complex schema requirements. Ajv is more actively maintained and offers greater flexibility, but requires more setup for UI integration.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Ajv JSON schema validator
The fastest JSON validator for Node.js and browser.
Supports JSON Schema draft-04/06/07/2019-09/2020-12 (draft-04 support requires ajv-draft-04 package) and JSON Type Definition RFC8927.
Ajv sponsors
Contributing
More than 100 people contributed to Ajv, and we would love to have you join the development. We welcome implementing new features that will benefit many users and ideas to improve our documentation.
Please review Contributing guidelines and Code components.
Documentation
All documentation is available on the Ajv website.
Some useful site links:
- Getting started
- JSON Schema vs JSON Type Definition
- API reference
- Strict mode
- Standalone validation code
- Security considerations
- Command line interface
- Frequently Asked Questions
Please sponsor Ajv development
Since I asked to support Ajv development 40 people and 6 organizations contributed via GitHub and OpenCollective - this support helped receiving the MOSS grant!
Your continuing support is very important - the funds will be used to develop and maintain Ajv once the next major version is released.
Please sponsor Ajv via:
- GitHub sponsors page (GitHub will match it)
- Ajv Open Collective
Thank you.
Open Collective sponsors
Performance
Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.
Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks:
- json-schema-benchmark - 50% faster than the second place
- jsck benchmark - 20-190% faster
- z-schema benchmark
- themis benchmark
Performance of different validators by json-schema-benchmark:
Features
- Ajv implements JSON Schema draft-06/07/2019-09/2020-12 standards (draft-04 is supported in v6):
- all validation keywords (see JSON Schema validation keywords)
- OpenAPI extensions:
- NEW: keyword discriminator.
- keyword nullable.
- full support of remote references (remote schemas have to be added with
addSchema
or compiled to be available) - support of recursive references between schemas
- correct string lengths for strings with unicode pairs
- JSON Schema formats (with ajv-formats plugin).
- validates schemas against meta-schema
- NEW: supports JSON Type Definition:
- all keywords (see JSON Type Definition schema forms)
- meta-schema for JTD schemas
- "union" keyword and user-defined keywords (can be used inside "metadata" member of the schema)
- supports browsers and Node.js 10.x - current
- asynchronous loading of referenced schemas during compilation
- "All errors" validation mode with option allErrors
- error messages with parameters describing error reasons to allow error message generation
- i18n error messages support with ajv-i18n package
- removing-additional-properties
- assigning defaults to missing properties and items
- coercing data to the types specified in
type
keywords - user-defined keywords
- additional extension keywords with ajv-keywords package
- $data reference to use values from the validated data as values for the schema keywords
- asynchronous validation of user-defined formats and keywords
Install
To install version 8:
npm install ajv
Getting started
Try it in the Node.js REPL: https://runkit.com/npm/ajv
In JavaScript:
// or ESM/TypeScript import
import Ajv from "ajv"
// Node.js require:
const Ajv = require("ajv")
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
const schema = {
type: "object",
properties: {
foo: {type: "integer"},
bar: {type: "string"},
},
required: ["foo"],
additionalProperties: false,
}
const data = {
foo: 1,
bar: "abc",
}
const validate = ajv.compile(schema)
const valid = validate(data)
if (!valid) console.log(validate.errors)
Learn how to use Ajv and see more examples in the Guide: getting started
Changes history
See https://github.com/ajv-validator/ajv/releases
Please note: Changes in version 8.0.0
Code of conduct
Please review and follow the Code of conduct.
Please report any unacceptable behaviour to ajv.validator@gmail.com - it will be reviewed by the project team.
Security contact
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerabilities via GitHub issues.
Open-source software support
Ajv is a part of Tidelift subscription - it provides a centralised support to open-source software users, in addition to the support provided by software maintainers.
License
Top Related Projects
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
JSON Schema validation
Tiny Validator for JSON Schema v4
The most powerful data validation library for JS
JSON Schema Based Editor
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot