class-validator vs. zod
Side-by-side comparison · 9 metrics · 14 criteria
- Weekly Downloads
- 18.0M
- Stars
- 11.9K
- Gzip Size
- 105.8 kB
- License
- MIT
- Last Updated
- 9mo ago
- Open Issues
- 329
- Forks
- 848
- Unpacked Size
- 5.3 MB
- Dependencies
- N/A
- Weekly Downloads
- 462.8M
- Stars
- 44.4K
- Gzip Size
- 88.9 kB
- License
- MIT
- Last Updated
- 10mo ago
- Open Issues
- 132
- Forks
- 2.4K
- Unpacked Size
- 6.1 MB
- Dependencies
- 0
class-validator vs zod downloads · last 12 months
Criteria · class-validator vs zod
- Schema Reuse
- class-validatorRules remain associated with decorated classeszod ✓Schemas can be composed and reused independently
- Learning Curve
- class-validatorStraightforward for teams already using decoratorszodRequires learning schema declarations and parsing
- Adoption Signal
- class-validator4.6M weekly downloads and 11.8K starszod ✓144.6M weekly downloads and 44.2K stars
- API Explicitness
- class-validatorValidation metadata is attached to propertieszodValidation is initiated through a declared schema
- Bundle Footprint
- class-validator105.8 kB gzip; larger compressed bundlezod ✓88.9 kB gzip; smaller compressed bundle
- Open Issue Count
- class-validator320 open issues in the supplied datazod ✓94 open issues in the supplied data
- Validation Model
- class-validatorAttaches validation rules to class properties through decoratorszodDeclares standalone schemas to validate values
- Class Instance Fit
- class-validator ✓Direct fit for class-oriented data modelszodValidates values without requiring class instances
- Migration Approach
- class-validatorRequires class models and property decoratorszodRequires translating rules into schemas and value parsing
- Modeling Flexibility
- class-validatorBest aligned with class-shaped modelszod ✓Schema-first, without a class-instance requirement
- Input Boundary Validation
- class-validatorWorks around validation of class instanceszod ✓Schema explicitly validates incoming values
- TypeScript Type Inference
- class-validatorDecorator-based validation for class fieldszod ✓Infers static types directly from schemas
- Installed Package Contents
- class-validator ✓5.3 MB unpacked; smaller package contentszod6.1 MB unpacked; larger package contents
- Validation Rule Organization
- class-validatorKeeps constraints near class propertieszodKeeps constraints in schema definitions
| Criteria | class-validator | zod |
|---|---|---|
| Schema Reuse | Rules remain associated with decorated classes | ✓ Schemas can be composed and reused independently |
| Learning Curve | Straightforward for teams already using decorators | Requires learning schema declarations and parsing |
| Adoption Signal | 4.6M weekly downloads and 11.8K stars | ✓ 144.6M weekly downloads and 44.2K stars |
| API Explicitness | Validation metadata is attached to properties | Validation is initiated through a declared schema |
| Bundle Footprint | 105.8 kB gzip; larger compressed bundle | ✓ 88.9 kB gzip; smaller compressed bundle |
| Open Issue Count | 320 open issues in the supplied data | ✓ 94 open issues in the supplied data |
| Validation Model | Attaches validation rules to class properties through decorators | Declares standalone schemas to validate values |
| Class Instance Fit | ✓ Direct fit for class-oriented data models | Validates values without requiring class instances |
| Migration Approach | Requires class models and property decorators | Requires translating rules into schemas and value parsing |
| Modeling Flexibility | Best aligned with class-shaped models | ✓ Schema-first, without a class-instance requirement |
| Input Boundary Validation | Works around validation of class instances | ✓ Schema explicitly validates incoming values |
| TypeScript Type Inference | Decorator-based validation for class fields | ✓ Infers static types directly from schemas |
| Installed Package Contents | ✓ 5.3 MB unpacked; smaller package contents | 6.1 MB unpacked; larger package contents |
| Validation Rule Organization | Keeps constraints near class properties | Keeps constraints in schema definitions |
class-validator is best suited to TypeScript applications whose data is already represented as class instances and whose validation rules belong alongside class properties. Its decorator-based approach lets teams express constraints on model fields and validate those objects without replacing a class-oriented design. It is a focused choice when the application’s existing architecture relies on classes and developers prefer validation metadata attached to those classes.
zod is designed around declaring schemas that validate values and infer corresponding static TypeScript types. That makes it a natural fit when developers want a schema to describe the accepted shape of input and serve as a source for both runtime checks and compile-time types. Its broad adoption, reflected in 144.6 million weekly downloads and 44.2K GitHub stars, also makes it a familiar option for teams choosing a TypeScript validation approach.
The central architectural difference is where the rules live and what developers pass through validation. class-validator puts decorators on class properties, so validation is organized around class definitions and instances; zod puts rules in schema values, which are explicitly used to parse or validate data. The former keeps constraints near object fields, while the latter makes the schema itself a distinct artifact that can be composed and reused independently of a class instance.
That difference shapes extension and reuse. With class-validator, extending validation commonly means adding or adjusting decorators on a class, keeping behavior tied to that class’s property structure. With zod, developers build and combine schema declarations, making the schema a direct unit of reuse for different input boundaries; this suits flows where data arrives as plain values and should be checked before being treated as a domain object. The provided package information does not establish a separate plugin system for either library, so the meaningful contrast is decorators versus schemas rather than a claimed difference in plugin availability.
For developer experience, class-validator is approachable when a team already understands decorators and uses class definitions as its main data model, but its approach depends on representing the validated data through those classes. zod asks developers to learn schema declaration, then returns static type inference from those declarations, reducing the need to maintain a separate TypeScript shape by hand. Its API can feel more explicit because validation begins with a schema, while class-validator can feel more integrated when the class is already the central source of structure.
The published bundle figures favor zod: its gzip bundle is 88.9 kB compared with 105.8 kB for class-validator, a meaningful consideration when client-side validation contributes to shipped JavaScript. The unpacked package figures point the other way, with class-validator at 5.3 MB and zod at 6.1 MB; that measure describes installed package contents, not the same thing as the compressed bundle delivered to an application. Teams should therefore weigh bundle impact against their actual deployment path rather than treating unpacked size as a proxy for browser cost.
Choose class-validator when the application already models inputs as classes, decorators match the team’s style, and property-level constraints are the desired way to maintain validation. Choose zod when schemas should define input boundaries, values should be checked independently of class instances, or inferred TypeScript types are a central requirement. For a new TypeScript project without a strong class-based convention, zod is the more direct fit for schema-first validation; for an established class-centric codebase, class-validator may preserve a more consistent model.
Migration is not merely a syntax rewrite: moving from class-validator means translating property decorators and class-instance validation into schema declarations and explicit value parsing, while moving in the other direction means introducing classes and attaching constraints to their fields. zod has fewer open issues in the supplied data, 94 versus 320 for class-validator, but issue counts alone do not establish resolution speed or maintenance quality. The adoption figures substantially favor zod, yet the practical long-term choice should still follow the application’s data model and whether schemas or decorated classes are the more stable contract.
A useful edge-case distinction is the point at which untrusted data enters the application. When input is naturally converted into class instances before checks run, class-validator aligns with that flow; when data should be validated as a value before any class construction, zod’s schema-centered API aligns more directly. Both are MIT-licensed validation libraries, but their contrasting abstractions make them poor drop-in substitutes: choose based on whether validation should be metadata on classes or an explicit schema that also supplies static types.
CORRECTIONS
Spot wrong data here?Spot wrong data on this page?
A short note helps us fix it.A short note helps us fix it. We read every one; confirmed fixes ship in the next nightly build.
Anonymous · No account · No email back