Skip to content

class-validator vs Zod

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.

class-validator

Open source · MIT

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

Zod

Open source · MIT

TypeScript-first library for validating data at runtime and inferring static types from the same schema.

The short answer

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

Choose Zod if you need…

  • TypeScript developers who want runtime checks that match their static types
  • Teams validating API request or response data in TypeScript projects
  • Developers who want one schema definition instead of separate types and validators

Side-by-side

class-validatorZod
TypeLibraryLibrary
PriceFree and open sourceFree and open source
LicenceMITMIT
GitHub stars11.9k44.2k
Setup difficultyBeginnerBeginner
How you get itInstall with a package managerInstall with a package manager
Main languageTypeScriptTypeScript
Last code update8 months ago6 days ago

What each one is

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.

Zod

Zod is a TypeScript library for defining schemas that check whether data has the expected shape, such as form input or API responses. You describe the shape once, and Zod both validates values at runtime and gives you a TypeScript type that matches it. The README provided is truncated and only points to the package, so details beyond that are limited here.

Best for

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

Zod

  • TypeScript developers who want runtime checks that match their static types
  • Teams validating API request or response data in TypeScript projects
  • Developers who want one schema definition instead of separate types and validators

Strengths

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

Zod

  • Schemas produce both runtime validation and static type inference
  • MIT licensed and widely starred (about 44k stars at time of metadata)
  • Actively maintained, with a recent release (v4.6.7) and a recent push

Not ideal for

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

Zod

  • Projects not using TypeScript, since the library is described as TypeScript-first and the README excerpt gives no JavaScript-only guidance
  • Readers needing a no-code tool, because this is a code library that requires writing schemas

Trade-offs

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

Zod

  • The README excerpt was truncated, so API details and setup specifics could not be verified here
  • Being TypeScript-first means its main value depends on using TypeScript

Getting started

class-validator

Install the package

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

Zod

Clone the repository

git clone https://github.com/colinhacks/zod
Step-by-step guide: how to use Zod

Frequently asked questions

What is the difference between class-validator and Zod?

class-validator: Validates class properties using TypeScript decorators, for browser and Node.js. Zod: TypeScript-first library for validating data at runtime and inferring static types from the same schema. class-validator suits typescript developers validating request bodies or form data in node.js or the browser, while Zod suits typescript developers who want runtime checks that match their static types.

Are class-validator and Zod free?

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

Is class-validator or Zod more popular?

Zod has more GitHub stars (44.2k) 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 class-validator and Zod?

Other options people compare them with include Yup, Ajv, validator.js, VeeValidate, ts-pattern.

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.