dto-classes
dto-classes copied to clipboard
Comparison to Zod?
I would love to see something written up in the README comparing and contrasting this with Zod. I took a quick look at the docs and couldn't quickly determine what the benefit of using this would be over something like Zod or similar libraries?
Yea, that's a great idea!.
I'll add a more detailed official section later, but here's the differentiation summary:
- A complete solution tailored especially for data transfer objects:
- a single construct for serialization and deserialization. zod and class-validator only handle parsing.
- syntax and style closer to JSON Schema and OpenAPI for aligned mental models. zod uses chained methods, class-validator uses decorators.
- straightforward way to integrate the HTTP context when parsing. Validation often depends on the requesting user.
- Static types by default:
- to get static types with zod, you must make an extra
infercall. - to get static types with class-validator, you must make a redundant type declaration. For a string field, you must use the
@IsString()decorator and declare thestringtype.
- to get static types with zod, you must make an extra
- Simple customization
- Just add your own method to a DTObject class for custom parsing or validation. class-validator makes you create a custom decorator any time you need to do this. You can do this in zod, using a
refinechained method, but I think class methods are easier to read & maintain.
- Just add your own method to a DTObject class for custom parsing or validation. class-validator makes you create a custom decorator any time you need to do this. You can do this in zod, using a