config icon indicating copy to clipboard operation
config copied to clipboard

Add Standard Schema validation support for configuration schema

Open iamnivekx opened this issue 5 months ago • 14 comments

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

iamnivekx avatar Jul 23 '25 01:07 iamnivekx

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:

micalevisk avatar Jul 27 '25 18:07 micalevisk

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.

iamnivekx avatar Jul 28 '25 02:07 iamnivekx

@nstandif

Thanks for the suggestion!

  1. Your dependency injection approach makes sense, but createValidator is only called once during module init, making the type detection overhead negligible.
  2. 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.
  3. 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.

iamnivekx avatar Aug 24 '25 14:08 iamnivekx

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.

iamnivekx avatar Sep 01 '25 10:09 iamnivekx

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.

iamnivekx avatar Sep 01 '25 10:09 iamnivekx

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!

iamnivekx avatar Sep 01 '25 10:09 iamnivekx

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!

nstandif avatar Sep 01 '25 14:09 nstandif

Got it — I’m waiting for the merge. @micalevisk do you think there’s a chance this could be merged?

iamnivekx avatar Sep 02 '25 01:09 iamnivekx

I hope so

micalevisk avatar Sep 02 '25 01:09 micalevisk

joi also supports Standard Schema. https://github.com/hapijs/joi/pull/3080

iamnivekx avatar Sep 02 '25 04:09 iamnivekx

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.

nstandif avatar Sep 03 '25 05:09 nstandif

Yes, just pointing it out here — I don’t want to introduce major changes either, but thought it might be helpful to mention. Thanks!

iamnivekx avatar Sep 03 '25 05:09 iamnivekx

Same here — would really love to see this merged. 🙏

iamnivekx avatar Dec 02 '25 14:12 iamnivekx

Hi @kamilmysliwiec, any chance this could be merged? 🙏

iamnivekx avatar Dec 06 '25 10:12 iamnivekx