Skip to content

class-validator vs Yup

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.

Yup

Open source · MIT

A JavaScript/TypeScript library for defining schemas that validate and convert data at runtime.

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 Yup if you need…

  • Developers validating form or API input in JavaScript or TypeScript
  • Teams that want TypeScript types inferred from a validation schema
  • Projects that need async validation, such as checking against a server
  • Developers who want to extend schemas with their own custom methods

Side-by-side

class-validatorYup
TypeLibraryLibrary
PriceFree and open sourceFree and open source
LicenceMITMIT
GitHub stars11.9k23.7k
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.

Yup

Yup lets you describe the expected shape of a value, such as a user object with a name, age, and email, using chained method calls. You can then check incoming data against that schema, or convert ('cast') it into the correct types. It is commonly used to check form input or API data before using it.

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

Yup

  • Developers validating form or API input in JavaScript or TypeScript
  • Teams that want TypeScript types inferred from a validation schema
  • Projects that need async validation, such as checking against a server
  • Developers who want to extend schemas with their own custom methods

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

Yup

  • Concise chained syntax that the README describes as expressive enough for complex, interdependent validations
  • Strong TypeScript support, including inferring static types from schemas
  • Clear error details and support for custom error messages and localization

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

Yup

  • Projects that do not use JavaScript or TypeScript, since the library is written for that ecosystem
  • Users who need a non-developer tool with a visual interface
  • Projects on the pre-v1 API, since the README points to a separate pre-v1 branch for those docs

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

Yup

  • The README is truncated, so this summary covers only the first part of the API reference
  • Its full feature set and edge cases are documented elsewhere and were not visible here
  • Pre-v1 users must use a different documentation branch

Getting started

class-validator

Install the package

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

Yup

Install yup

git clone https://github.com/jquense/yup
Step-by-step guide: how to use Yup

Frequently asked questions

What is the difference between class-validator and Yup?

class-validator: Validates class properties using TypeScript decorators, for browser and Node.js. Yup: A JavaScript/TypeScript library for defining schemas that validate and convert data at runtime. class-validator suits typescript developers validating request bodies or form data in node.js or the browser, while Yup suits developers validating form or api input in javascript or typescript.

Are class-validator and Yup free?

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

Is class-validator or Yup more popular?

Yup has more GitHub stars (23.7k) 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 Yup?

Other options people compare them with include Zod, Ajv, validator.js, VeeValidate.

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.