nestjs/config

Add support for Zod in schema validation

MatthewAry opened this issue · 1 comments

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Currently it seems that the validationSchema feature only uses Joi to validate, however Zod can do much of the same things that Joi can do for validation BUT it also lets you obtain an inferred TypeScript type from the defined schema, which is something that Joi can't do.

Describe the solution you'd like

Make Zod a first class citizen for schema validation.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Zod's own docs explain the justification pretty well.

Zod is designed to be as developer-friendly as possible. The goal is to eliminate duplicative type declarations. With Zod, you declare a validator once and Zod will automatically infer the static TypeScript type. It's easy to compose simpler types into complex data structures.

Currently it seems that the validationSchema feature only uses Joi to validate

It uses Joi because we need to use some lib. But you can provide any other validationSchema function that follows the API.

it also lets you obtain an inferred TypeScript type from the defined schema,

but for @nestjs/config that feature doesn't matter because ConfigService has its own way of retrieving the type def of some field.

I like Zod but I don't see how we could benefit on Zod's features in @nestjs/config without introducing new abstractions to it (= maintenance burden). Personally, I found quite better (& easy) to write my own config module instead.

What we could do is to improve validationSchema feature to allow us to supply any lib for object validation & parsing my bad, this is alread feasible with validate function