dto-classes icon indicating copy to clipboard operation
dto-classes copied to clipboard

Comparison to Zod?

Open wilmoore opened this issue 2 years ago • 1 comments

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?

wilmoore avatar Jun 08 '23 04:06 wilmoore

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 infer call.
    • 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 the string type.
  • 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 refine chained method, but I think class methods are easier to read & maintain.

rsinger86 avatar Jun 11 '23 20:06 rsinger86