Convert Figma logo to code with AI

ajv-validator logoajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)

13,713
872
13,713
266

Top Related Projects

13,715

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)

JSON Schema validation

1,169

Tiny Validator for JSON Schema v4

20,860

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

  1. 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
  1. 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
  1. 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:

  1. Install Ajv:
npm install ajv
  1. Import and create an Ajv instance:
import Ajv from "ajv"
const ajv = new Ajv()
  1. Define a schema and compile it:
const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "number" }
  },
  required: ["name"]
}

const validate = ajv.compile(schema)
  1. Use the validator function:
const data = { name: "John", age: 30 }
if (validate(data)) {
  console.log("Valid!")
} else {
  console.log("Invalid:", validate.errors)
}

Competitor Comparisons

13,715

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.

1,169

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
20,860

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Ajv logo

 

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.

build npm npm downloads Coverage Status SimpleX Gitter GitHub Sponsors

Ajv sponsors

Mozilla

Microsoft

RetoolTideliftSimpleX

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:

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:

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:

Performance of different validators by json-schema-benchmark:

performance

Features

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

Version 7.0.0

Version 6.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

MIT

NPM DownloadsLast 30 Days