ajv vs json-schema vs joi vs yup vs jsonschema vs superstruct vs z-schema vs tv4
Data Validation Libraries Comparison
3 Years
ajvjson-schemajoiyupjsonschemasuperstructz-schematv4Similar Packages:
What's Data Validation Libraries?

Data validation libraries in JavaScript provide tools to ensure that data (such as user input, API responses, or configuration files) adheres to specific rules or schemas before it is processed or stored. These libraries help catch errors early, improve data integrity, and enhance application security by preventing invalid or malicious data from causing issues. They offer various features like type checking, custom validation, asynchronous validation, and integration with frameworks like React, making them versatile for both frontend and backend development.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
ajv150,584,274
14,3231.03 MB299a year agoMIT
json-schema25,570,079
526-304 years ago(AFL-2.1 OR BSD-3-Clause)
joi12,217,589
21,143558 kB1853 days agoBSD-3-Clause
yup7,753,559
23,502270 kB2425 days agoMIT
jsonschema3,935,368
1,86083.5 kB667 months agoMIT
superstruct2,892,881
7,129182 kB98a year agoMIT
z-schema2,405,663
3402.07 MB39a year agoMIT
tv42,252,403
1,169-1168 years ago-
Feature Comparison: ajv vs json-schema vs joi vs yup vs jsonschema vs superstruct vs z-schema vs tv4

Validation Approach

  • ajv:

    ajv (Another JSON Validator) is a JSON Schema validator that focuses on performance and compliance with JSON Schema standards. It validates data against schemas defined in JSON Schema format, supporting both synchronous and asynchronous validation. ajv is known for its speed and efficiency, making it suitable for applications that require high-performance validation.

  • json-schema:

    json-schema is a library for working with JSON Schema, a standard for describing the structure of JSON data. This library provides tools for validating JSON data against schemas, generating schemas from JavaScript objects, and serializing/deserializing data according to schema definitions. It is useful for ensuring that JSON data adheres to a specified format, making it valuable for API validation and data interchange.

  • joi:

    joi is a powerful schema description language and data validator for JavaScript. It allows developers to define complex validation rules using a fluent, chainable API. joi is particularly well-suited for validating objects, arrays, and primitive types, and it provides detailed error messages when validation fails. It is widely used in Node.js applications, especially for validating request payloads in APIs.

  • yup:

    yup is a JavaScript schema builder for value parsing and validation. It is particularly popular in React applications for form validation. yup allows developers to define schemas for objects, arrays, and primitive values, and it supports asynchronous validation, custom error messages, and nested structures. It is designed to be easy to use and integrates well with form libraries like Formik.

  • jsonschema:

    jsonschema is a simple and lightweight library for validating JSON data against JSON Schema definitions. It provides an easy-to-use API for validating objects, arrays, and other JSON data types. The library supports custom validation functions and error handling, making it flexible for various use cases. It is particularly useful for validating data in web applications, APIs, and any JavaScript environment where JSON data needs to be validated.

  • superstruct:

    superstruct is a tiny and fast library for creating simple, composable validators for JavaScript data. It allows developers to define data structures and validation rules using a straightforward API. superstruct is designed to be lightweight and easy to use, making it a great choice for projects that need quick and efficient data validation without the overhead of larger libraries.

  • z-schema:

    z-schema is a fast and extensible JSON Schema validator that supports draft-04, draft-06, and draft-07 specifications. It is designed for high performance and allows for custom validation logic, making it suitable for complex validation scenarios. z-schema also provides detailed error reporting and supports asynchronous validation, making it a versatile choice for applications that require robust JSON data validation.

  • tv4:

    tv4 is a fast and lightweight validator for JSON Schema (Draft 4). It provides a simple API for validating JSON data against schemas defined in the JSON Schema format. tv4 is known for its speed and simplicity, making it a good choice for projects that need quick validation without a lot of complexity. It also supports asynchronous validation and custom error messages.

Error Handling

  • ajv:

    ajv provides detailed error reporting that includes the path to the invalid data, the expected schema, and the actual value that failed validation. This information is useful for debugging and for providing clear feedback to users or developers.

  • json-schema:

    json-schema provides basic error handling for validation failures. When data does not conform to the defined schema, the library generates error messages that indicate the nature of the violation. However, the error handling capabilities are relatively simple compared to more feature-rich libraries.

  • joi:

    joi generates rich error messages that are easy to understand. When validation fails, joi returns an error object that contains information about the failed validation, including the path to the invalid value, the type of validation that failed, and a customizable error message. This makes it easy to identify and handle validation errors in a user-friendly way.

  • yup:

    yup offers comprehensive error handling with support for asynchronous validation and nested structures. When validation fails, yup provides detailed error messages that can be easily accessed and displayed to users. The library also supports custom error messages and validation rules, allowing for greater flexibility in how errors are handled and presented.

  • jsonschema:

    jsonschema provides clear error messages when validation fails. The library returns an error object that includes information about the validation failures, such as the path to the invalid data and the reasons for the failure. This makes it easy to understand what went wrong and how to address it.

  • superstruct:

    superstruct provides simple error handling for validation failures. When data does not conform to the defined structure, superstruct throws an error that includes information about the validation failure. The error message includes the path to the invalid value and a description of the validation rule that was not met, making it easy to identify the issue.

  • z-schema:

    z-schema provides detailed error messages for validation failures, including information about the schema that was violated and the path to the invalid data. The error reporting is designed to be clear and informative, making it easier to debug validation issues.

  • tv4:

    tv4 provides error messages for validation failures, including the path to the invalid data and a description of the validation error. The error reporting is straightforward and helps developers understand why the validation failed.

Asynchronous Validation

  • ajv:

    ajv supports asynchronous validation out of the box, allowing for validation of data that may involve asynchronous operations, such as checking values against a database or an external API. This feature is particularly useful for validating data that requires external checks or for implementing custom asynchronous validation logic.

  • json-schema:

    json-schema does not natively support asynchronous validation, as it is primarily focused on synchronous validation of JSON data against defined schemas. However, developers can implement custom asynchronous validation logic outside of the library if needed.

  • joi:

    joi supports asynchronous validation, making it suitable for scenarios where validation requires external data or operations. Developers can define asynchronous validation rules using promises or async/await, allowing for more complex validation logic that goes beyond simple synchronous checks.

  • yup:

    yup fully supports asynchronous validation, making it a great choice for scenarios where validation may involve external data or operations. It allows for the use of promises and async/await in validation rules, enabling more complex and dynamic validation logic.

  • jsonschema:

    jsonschema is primarily designed for synchronous validation of JSON data against schemas. It does not provide built-in support for asynchronous validation, but developers can extend the library or implement asynchronous validation logic separately if required.

  • superstruct:

    superstruct does not support asynchronous validation out of the box, as it is designed for simple synchronous validation of data structures. However, developers can create custom asynchronous validation functions and integrate them into their validation logic if needed.

  • z-schema:

    z-schema primarily focuses on synchronous validation of JSON data against schemas. It does not provide built-in support for asynchronous validation, but developers can implement custom asynchronous validation logic if needed.

  • tv4:

    tv4 is designed for synchronous validation of JSON Schema (Draft 4) and does not support asynchronous validation. It focuses on providing fast and efficient validation for JSON data against schemas without the complexity of asynchronous operations.

Code Examples

  • ajv:

    ajv Example

    const Ajv = require('ajv');
    const ajv = new Ajv();
    
    const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] };
    const validate = ajv.compile(schema);
    
    const data = { name: 'John' };
    const valid = validate(data);
    if (!valid) console.log(validate.errors);
    
  • json-schema:

    json-schema Example

    const { Validator } = require('json-schema');
    const validator = new Validator();
    
    const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] };
    const data = { name: 'John' };
    const valid = validator.validate(data, schema);
    if (!valid) console.log(validator.errors);
    
  • joi:

    joi Example

    const Joi = require('joi');
    
    const schema = Joi.object({ name: Joi.string().required() });
    const { error, value } = schema.validate({ name: 'John' });
    if (error) console.log(error.details);
    
  • yup:

    yup Example

    const yup = require('yup');
    
    const schema = yup.object({ name: yup.string().required() });
    const data = { name: 'John' };
    const isValid = await schema.isValid(data);
    if (!isValid) console.log('Invalid data');
    
  • jsonschema:

    jsonschema Example

    const { Validator } = require('jsonschema');
    const validator = new Validator();
    
    const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] };
    const data = { name: 'John' };
    const validationResult = validator.validate(data, schema);
    if (!validationResult.valid) {
      console.log(validationResult.errors);
    }
    
  • superstruct:

    superstruct Example

    const { struct } = require('superstruct');
    
    const User = struct({ name: 'string' });
    const user = { name: 'John' };
    const isValid = User(user);
    if (!isValid) console.log('Invalid user');
    
  • z-schema:

    z-schema Example

    const ZSchema = require('z-schema');
    const validator = new ZSchema();
    
    const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] };
    const data = { name: 'John' };
    const valid = validator.validate(data, schema);
    if (!valid) console.log(validator.getLastErrors());
    
  • tv4:

    tv4 Example

    const tv4 = require('tv4');
    const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] };
    const data = { name: 'John' };
    const valid = tv4.validate(data, schema);
    if (!valid) console.log(tv4.error);
    
How to Choose: ajv vs json-schema vs joi vs yup vs jsonschema vs superstruct vs z-schema vs tv4
  • ajv:

    Choose ajv if you need a fast, standards-compliant JSON Schema validator that supports asynchronous validation and custom keywords. It is ideal for projects that require high performance and adherence to JSON Schema specifications.

  • json-schema:

    Use json-schema if you need a lightweight library for working with JSON Schema, including validation, serialization, and deserialization. It is a good choice for projects that require basic JSON Schema functionality without additional overhead.

  • joi:

    Select joi if you prefer a declarative, schema-based approach to validation with rich error messages and built-in support for complex data types. It is well-suited for Node.js applications and integrates seamlessly with Express.

  • yup:

    Use yup if you need a schema builder for runtime value parsing and validation, especially in React applications. It supports asynchronous validation, custom messages, and integrates well with form libraries like Formik.

  • jsonschema:

    Opt for jsonschema if you want a simple and easy-to-use validator for JSON Schema. It is particularly useful for projects that need a straightforward implementation without a lot of configuration or complexity.

  • superstruct:

    Choose superstruct if you value a minimalistic, TypeScript-friendly library that allows you to define validations using simple functions. It is great for projects that prioritize type safety and want to keep dependencies lightweight.

  • z-schema:

    Choose z-schema if you require a fast and extensible JSON Schema validator with support for draft-04, draft-06, and draft-07. It is ideal for projects that need a flexible validator with good performance and customization options.

  • tv4:

    Select tv4 if you need a small, fast, and easy-to-use validator for JSON Schema (Draft v4). It is suitable for projects that require a no-frills solution with a focus on simplicity and performance.

README for ajv
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