Add Standard Schema validation support for configuration schema
PR Checklist
Please check if your PR fulfills the following requirements:
- [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
- [x] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
PR Type
What kind of change does this PR introduce?
- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, local variables)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Other... Please describe:
What is the current behavior?
Issue Number: https://github.com/nestjs/config/issues/1164
What is the new behavior?
Does this PR introduce a breaking change?
- [ ] Yes
- [x] No
Other information
I like the ideia of supporting zod specially now with a lot of AI-related stuff using it. Can you elaborate a bit on why we can't use zod prior to this PR? I thought that we're able to use zod alreay in @nestjs/config v4.0.2 :thinking:
You're absolutely right — technically, Zod can be used with @nestjs/[email protected] via a custom validate function.
The main goals of this PR are:
- To provide first-class support for Zod, allowing developers to pass Zod schemas directly into the config module, just like with
Joi; - To improve developer experience (DX) by removing the need for custom integration logic or adapters.
@nstandif
Thanks for the suggestion!
- Your dependency injection approach makes sense, but
createValidatoris only called once during module init, making the type detection overhead negligible. - Regarding Zod not being added to peerDependencies: I followed the same pattern as Joi - both are optional validation libraries that users can choose to install based on their needs. This keeps the core package lightweight and avoids forcing users to install validation libraries they don't use.
- I've also added comprehensive test coverage for all three Zod versions (v3, v4, and v4 mini) to ensure the implementation works correctly across different versions.
Maybe we could do a StandardSchemaV1 validator instead of just zod? I don't see anything that is implementation specific to zod that we'd need to support. WDYT?
Good point — I agree, there doesn’t seem to be anything strictly tied to Zod here. Supporting StandardSchemaV1 sounds like a better approach.
I still think "zod": "^3.25.0 || ^4.0.0" should be added as a peer dependency, so that npm can manage this. Also, please consider `"joi": "^17.13.0 || ^18.0.0" added as a peer dep., granted that you have tested 'joi v18' and we don't have node 12 compatibility to maintain for this library.
I see your point, but since these dependencies are optional, I think it’s clearer and simpler to keep them out of peerDependencies. This way we avoid forcing consumers to install packages they may not need.
Maybe we could do a StandardSchemaV1 validator instead of just zod? I don't see anything that is implementation specific to zod that we'd need to support. WDYT?
Good point — I agree, there doesn’t seem to be anything strictly tied to Zod here. Supporting StandardSchemaV1 sounds like a better approach.
@nstandif DONE!
Once you get this merged in, please make a PR to nestjs/docs.nestjs.com and standard-schema/standard-schema so that others know that standard schema is supported!
Got it — I’m waiting for the merge. @micalevisk do you think there’s a chance this could be merged?
I hope so
joi also supports Standard Schema. https://github.com/hapijs/joi/pull/3080
joi also supports Standard Schema. hapijs/joi#3080
Yes, but it is only supported as of v18.0.0+. Removing the joi dependency would be breaking.
Yes, just pointing it out here — I don’t want to introduce major changes either, but thought it might be helpful to mention. Thanks!
Same here — would really love to see this merged. 🙏
Hi @kamilmysliwiec, any chance this could be merged? 🙏