Skip to content

Ajv vs class-validator

Which should you use? A side-by-side look at what each one does, who it suits, what it costs, how hard it is to set up, and where each falls short.

Ajv

Open source · MIT

A fast library that checks whether JSON data matches a schema, for Node.js and browsers.

class-validator

Open source · MIT

Validates class properties using TypeScript decorators, for browser and Node.js.

The short answer

Choose Ajv if you need…

  • Node.js developers validating API request or response bodies
  • Front-end developers validating form or config data in the browser
  • Teams already writing JSON Schema who need a standards-based checker
  • Projects that need draft-04 support (via the separate ajv-draft-04 package)

Choose class-validator if you need…

  • TypeScript developers validating request bodies or form data in Node.js or the browser
  • Projects that already model data as classes and want validation rules next to the fields
  • Teams building APIs who want to return structured validation errors to clients

Side-by-side

Ajvclass-validator
TypeLibraryLibrary
PriceFree and open sourceFree and open source
LicenceMITMIT
GitHub stars14.9k11.9k
Setup difficultyIntermediateBeginner
How you get itInstall with a package managerInstall with a package manager
Main languageTypeScriptTypeScript
Last code update2 months ago8 months ago

What each one is

Ajv

Ajv is a JavaScript/TypeScript library that checks JSON data against a schema, such as confirming a form submission or API request has the required fields and types. It supports JSON Schema drafts 04, 06, 07, 2019-09 and 2020-12, plus JSON Type Definition (RFC 8927). The README describes it as fast, and it generates validation code from schemas to achieve this.

class-validator

class-validator lets you attach validation rules to the properties of a class using decorators, such as checking that a string is an email or a number is within a range. You then call validate on an object and get back a list of errors for anything that fails. It also supports non-decorator schemas, and it uses validator.js internally to run the checks.

Best for

Ajv

  • Node.js developers validating API request or response bodies
  • Front-end developers validating form or config data in the browser
  • Teams already writing JSON Schema who need a standards-based checker
  • Projects that need draft-04 support (via the separate ajv-draft-04 package)

class-validator

  • TypeScript developers validating request bodies or form data in Node.js or the browser
  • Projects that already model data as classes and want validation rules next to the fields
  • Teams building APIs who want to return structured validation errors to clients

Strengths

Ajv

  • The README claims it is the fastest JSON schema validator and cites third-party benchmarks
  • Supports multiple JSON Schema drafts and JSON Type Definition
  • MIT licensed with a large community (over 100 contributors per the README)

class-validator

  • Rules are declared next to the fields they check, which keeps models readable
  • Returns structured ValidationError objects with property names and constraint messages
  • Works in both browser and Node.js environments according to the README

Not ideal for

Ajv

  • People who are new to programming and just want a simple form library, since it requires writing schemas
  • Projects that need a non-JavaScript runtime, since the README only mentions Node.js and browsers
  • Users who only need to check a few values by hand, where a simple function would be enough

class-validator

  • Projects that do not use TypeScript or decorators, since the main API depends on them (the README also offers a non-decorator option, which is less covered in the excerpt)
  • Users who need a full schema language with type inference from one definition, as the README does not compare against such tools

Trade-offs

Ajv

  • Draft-04 support requires an extra package, so setup is not one step for older schemas
  • The README is short and points to an external website for most documentation
  • Performance claims come from the project's own README citing older benchmarks

class-validator

  • The README says forbidUnknownValues defaults to true and advises keeping it; turning it off lets unknown objects pass validation
  • Requires npm 6 or later so the dependency tree is flattened correctly
  • The README excerpt is truncated, so details of many decorators and advanced features were not reviewed

Getting started

Ajv

Install the package

npm install ajv
Step-by-step guide: how to use Ajv

class-validator

Install the package

npm install class-validator --save
Step-by-step guide: how to use class-validator

Frequently asked questions

What is the difference between Ajv and class-validator?

Ajv: A fast library that checks whether JSON data matches a schema, for Node.js and browsers. class-validator: Validates class properties using TypeScript decorators, for browser and Node.js. Ajv suits node.js developers validating api request or response bodies, while class-validator suits typescript developers validating request bodies or form data in node.js or the browser.

Are Ajv and class-validator free?

Ajv: Free and open source. class-validator: Free and open source.

Which is easier to set up, Ajv or class-validator?

class-validator is easier to get started with (rated beginner); Ajv is rated intermediate and usually needs more setup.

Is Ajv or class-validator more popular?

Ajv has more GitHub stars (14.9k) than class-validator (11.9k). Popularity isn't the same as fit — check which one matches your use case above.

What are other alternatives to Ajv and class-validator?

Other options people compare them with include Zod, Formily, Pydantic, SQLModel, Yup.

Keep comparing

Built from our catalogue entries for both products: open-source details come from each project's README (AI-assisted, refreshed weekly) and GitHub data; AI-tool details are editorial. Spotted something wrong? Let us know from either product's page.