class-validator vs. joi
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
- 33.6M
- Stars
- 21.2K
- Gzip Size
- 55.0 kB
- License
- BSD-3-Clause
- Last Updated
- 1y ago
- Open Issues
- 217
- Forks
- 1.5K
- Unpacked Size
- 1.9 MB
- Dependencies
- 1
class-validator vs joi downloads · last 12 months
Criteria · class-validator vs joi
- Schema Reuse
- class-validatorReusable rules are associated with decorated classesjoi ✓Schemas can be handled as explicit validation values
- Learning Curve
- class-validatorRequires understanding decorators and instance validationjoiRequires learning the schema-oriented API
- Rule Placement
- class-validatorConstraints sit on class propertiesjoiConstraints live in schema declarations
- Bundle Footprint
- class-validatorListed at 105.8 kB gzipjoi ✓Listed at 55.0 kB gzip
- Migration Effort
- class-validatorRequires classes and decorated propertiesjoiRequires schema definitions and schema-based calls
- Validation Model
- class-validatorValidates class instances using property decoratorsjoiValidates objects against explicit schemas
- Unpacked Footprint
- class-validatorListed at 5.3 MB unpackedjoi ✓Listed at 1.9 MB unpacked
- TypeScript Integration
- class-validator ✓Uses TypeScript class properties and decoratorsjoiDescribes object validation through schemas
- Constraint Organization
- class-validatorRules are colocated with declared propertiesjoiRules are grouped in object schemas
- Data Representation Fit
- class-validatorBest aligned with class instancesjoiBest aligned with object-shaped data
- Plain Object Validation
- class-validatorRequires a class-instance-oriented flowjoi ✓Directly fits plain object inputs
- Class-Oriented Codebases
- class-validator ✓Naturally aligns validation with class definitionsjoiSchema remains separate from class declarations
- Operational Track Record
- class-validator16.9M weekly downloads and 329 open issuesjoi ✓33.9M weekly downloads and 210 open issues
- Validation Boundary Clarity
- class-validatorValidation is attached to class structurejoi ✓Validation is visibly invoked with a schema
| Criteria | class-validator | joi |
|---|---|---|
| Schema Reuse | Reusable rules are associated with decorated classes | ✓ Schemas can be handled as explicit validation values |
| Learning Curve | Requires understanding decorators and instance validation | Requires learning the schema-oriented API |
| Rule Placement | Constraints sit on class properties | Constraints live in schema declarations |
| Bundle Footprint | Listed at 105.8 kB gzip | ✓ Listed at 55.0 kB gzip |
| Migration Effort | Requires classes and decorated properties | Requires schema definitions and schema-based calls |
| Validation Model | Validates class instances using property decorators | Validates objects against explicit schemas |
| Unpacked Footprint | Listed at 5.3 MB unpacked | ✓ Listed at 1.9 MB unpacked |
| TypeScript Integration | ✓ Uses TypeScript class properties and decorators | Describes object validation through schemas |
| Constraint Organization | Rules are colocated with declared properties | Rules are grouped in object schemas |
| Data Representation Fit | Best aligned with class instances | Best aligned with object-shaped data |
| Plain Object Validation | Requires a class-instance-oriented flow | ✓ Directly fits plain object inputs |
| Class-Oriented Codebases | ✓ Naturally aligns validation with class definitions | Schema remains separate from class declarations |
| Operational Track Record | 16.9M weekly downloads and 329 open issues | ✓ 33.9M weekly downloads and 210 open issues |
| Validation Boundary Clarity | Validation is attached to class structure | ✓ Validation is visibly invoked with a schema |
class-validator is designed for developers who model application data as TypeScript classes and want validation rules declared on class properties. Its decorator-based approach keeps constraints close to the fields they govern, which can suit class-oriented codebases where those classes already represent request or domain objects. It is less natural when the input is an arbitrary object and there is no class instance to validate.
joi is built around describing and validating object shapes with schemas. That makes it a direct fit for data arriving as plain objects, such as parsed request payloads or configuration values, without requiring the data to be expressed as decorated classes first. Its schema-first philosophy is particularly useful when validation needs to be explicit and separated from the shape’s runtime representation.
The central architectural difference is where the rules live and how data reaches them. With class-validator, a developer adds decorators to class properties and validates an instance against that metadata; with joi, a developer defines a schema and passes an object through schema validation. The choice affects design: class-validator ties validation to class definitions, while joi makes the schema an explicit object that can be selected and applied independently.
Their extension approaches follow those different foundations. class-validator’s rules are expressed as property decorators, so reusable constraints are associated with class fields and the metadata collected for those classes. joi’s validation is expressed through schemas, allowing a developer to compose the description of an object and its constraints as part of the validation flow. Consider whether the team prefers annotations embedded in class declarations or schemas that can be handled as separate values; neither model is simply a drop-in form of the other.
For developer experience, class-validator can be approachable when TypeScript classes are already the project’s primary way to define data: rules appear beside the properties they protect. That convenience also means developers need to understand the decorator-and-instance model, and validation depends on class structure rather than only on an object’s shape. joi requires learning its schema-oriented API, but its explicit schema makes the validation boundary easy to identify when inspecting code that handles plain objects.
The supplied bundle figures show a meaningful footprint difference: joi is listed at 55.0 kB gzip, compared with 105.8 kB for class-validator. The unpacked figures point in the same direction, at 1.9 MB for joi and 5.3 MB for class-validator. If shipped JavaScript size is an important constraint, joi has the advantage in these figures; bundle size alone, however, does not determine whether its schema model matches the application’s architecture.
Choose class-validator when the application already represents validated data as classes and the team wants constraints declared on properties, for example when class instances are the natural boundary for incoming or internal data. Choose joi when the input is primarily plain objects and a standalone schema is the clearer contract, such as validating payloads or configuration before application code consumes them. Before adopting either, test the validation flow against the actual data shape: converting plain objects into class instances or maintaining schemas beside classes can add a design layer that the other approach avoids.
The migration path is architectural rather than a mechanical change of syntax. Moving from class-validator to joi means translating property decorators into object schemas and changing validation calls to operate on those schemas; moving in the other direction means establishing classes and applying decorators to their properties. Teams should account for where constraints are currently defined and whether consumers expect class instances, because those expectations can be more consequential than rewriting individual rules.
A useful boundary case is a codebase with both class-based domain objects and untrusted, plain input data. class-validator is a closer fit where validation belongs to instances; joi is a closer fit where the schema itself should describe and check an incoming object. The provided package data also shows joi with more weekly downloads and stars, fewer open issues, and a later listed update, but those comparisons do not replace the architectural decision: pick the API that matches the data representation your validation boundary actually receives.
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