ajv vs joi vs jsonschema vs z-schema
データバリデーションライブラリ
ajvjoijsonschemaz-schema類似パッケージ:
データバリデーションライブラリ

データバリデーションライブラリは、入力データが特定のスキーマに従っているかどうかを検証するためのツールです。これにより、アプリケーションのデータの整合性を保ち、エラーを未然に防ぐことができます。これらのライブラリは、JSONデータのバリデーションを行うために広く使用されており、APIの入力検証や設定ファイルの検証など、さまざまなシナリオで利用されます。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
ajv176,210,71114,4981.03 MB3051年前MIT
joi14,732,06421,187557 kB18617日前BSD-3-Clause
jsonschema4,696,3631,86383.5 kB681年前MIT
z-schema2,693,4483412.07 MB391年前MIT
機能比較: ajv vs joi vs jsonschema vs z-schema

パフォーマンス

  • ajv:

    AJVは、非常に高速なバリデーションを提供します。スキーマをコンパイルしてキャッシュする機能により、大量のデータを処理する際のパフォーマンスが向上します。

  • joi:

    Joiは、パフォーマンスも良好ですが、AJVほどの速度はありません。特に複雑なバリデーションを行う場合に、柔軟性と可読性を重視しています。

  • jsonschema:

    jsonschemaは、標準的なバリデーションを提供しますが、パフォーマンスはAJVに比べて劣ります。大規模なデータセットを扱う際には注意が必要です。

  • z-schema:

    z-schemaは、拡張機能を持ちながらも、パフォーマンスは良好です。特にカスタムバリデーションを多く使用する場合に適しています。

APIの使いやすさ

  • ajv:

    AJVは、シンプルなAPIを提供し、スキーマの定義が直感的です。特にJSONスキーマに慣れている開発者にとって使いやすいです。

  • joi:

    Joiは、非常に直感的で、オブジェクトのバリデーションを簡単に行えるAPIを提供します。特に、ネストされたオブジェクトのバリデーションが容易です。

  • jsonschema:

    jsonschemaは、標準的なJSONスキーマに基づいているため、他のツールとの互換性が高いですが、APIはやや冗長に感じることがあります。

  • z-schema:

    z-schemaは、拡張機能を持つため、APIはやや複雑ですが、柔軟性があります。特にカスタムバリデーションを多く使用する場合に便利です。

エラーメッセージのカスタマイズ

  • ajv:

    AJVは、エラーメッセージをカスタマイズするためのオプションを提供しています。特定のバリデーションエラーに対して、独自のメッセージを設定できます。

  • joi:

    Joiは、エラーメッセージのカスタマイズが非常に簡単で、バリデーションルールごとにメッセージを定義できます。特にユーザーに対するフィードバックを重視する場合に適しています。

  • jsonschema:

    jsonschemaは、エラーメッセージのカスタマイズ機能はありますが、AJVやJoiほど柔軟ではありません。標準的なメッセージが多く使用されます。

  • z-schema:

    z-schemaは、エラーメッセージのカスタマイズが可能ですが、他のライブラリに比べて設定が複雑になることがあります。

拡張性

  • ajv:

    AJVは、プラグインを使用して機能を拡張することが可能です。独自のバリデーションルールを追加することができ、特定のニーズに応じたカスタマイズが行えます。

  • joi:

    Joiは、拡張性が高く、独自のバリデーションメソッドを追加することができます。特に、複雑なビジネスロジックに基づくバリデーションに適しています。

  • jsonschema:

    jsonschemaは、標準に準拠したバリデーションを提供しますが、拡張性は制限されています。特に、カスタム機能を追加することは難しいです。

  • z-schema:

    z-schemaは、拡張性が高く、独自のバリデーション機能を追加することができます。特に、特定の要件に応じたカスタマイズが可能です。

コミュニティとサポート

  • ajv:

    AJVは、活発なコミュニティがあり、多くのドキュメントやサポートが提供されています。特にパフォーマンスに関する情報が豊富です。

  • joi:

    Joiも広く使用されており、コミュニティが活発です。多くのリソースやサポートが利用可能で、特にNode.jsエコシステムでの使用が一般的です。

  • jsonschema:

    jsonschemaは、標準に基づいているため、広く認知されていますが、他のライブラリに比べてコミュニティは小さめです。

  • z-schema:

    z-schemaは、比較的新しいライブラリですが、拡張機能が豊富で、特定のニーズに応じたサポートが得られることがあります。

選び方: ajv vs joi vs jsonschema vs z-schema
  • ajv:

    AJVは、高速なJSONスキーマバリデーションを必要とする場合に選択してください。特にパフォーマンスが重要なアプリケーションで、スキーマのコンパイルとキャッシュ機能を活用したい場合に適しています。

  • joi:

    Joiは、シンプルで直感的なAPIを持つため、複雑なオブジェクトのバリデーションを行う際に選択してください。特にNode.js環境での使用に適しており、柔軟なバリデーションルールを定義できます。

  • jsonschema:

    jsonschemaは、JSONスキーマに基づく標準的なバリデーションを行いたい場合に選択してください。特に、JSONスキーマの仕様に準拠したバリデーションを重視する場合に適しています。

  • z-schema:

    z-schemaは、JSONスキーマの拡張機能を必要とする場合に選択してください。特に、スキーマのバリデーションにおいてカスタム機能や追加のバリデーションルールを使用したい場合に便利です。

ajv の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