nest
nest copied to clipboard
Compatibility with standard-schema
Is there an existing issue that is already proposing this?
- [x] I have searched the existing issues
Is your feature request related to a problem? Please describe it
There are numerous schema validators out there. This has spawned an ecosystem around NestJS with various integrations such as nestjs-valibot and many others.
Describe the solution you'd like
NestJS could support stanadard-schema which recently hit 1.0.0:
Standard Schema is a common interface designed to be implemented by JavaScript and TypeScript schema libraries.
With that a commonly shared ValidationPipe could be offered directly from the framework instead of copy/pasting the integrations from the docs site.
Teachability, documentation, adoption, migration strategy
Guide on how to accept Standard Schema: https://standardschema.dev/#how-do-i-accept-standard-schemas-in-my-library
- Offer a general
ValidationPipethat is compatible with standard-schema - Update the docs how the
ValidationPipecan be utilised with different schema libraries
What is the motivation / use case for changing the behavior?
I think it makes NestJS more future-proof with any future schema/validation library that might come
Notably, there is an issue open on class-validator side around this topic as well: https://github.com/typestack/class-validator/issues/2577
I like the ideia but I think this should start as a 3rd-party lib to see community's adoption
I guess there's nothing we can do until class-validator supports the standard schema too (to avoid breaking changes)
Unfortunately, the class-validator isn't updated much anymore。https://github.com/typestack/class-validator/issues/1775
@0x0bit well
@0x0bit出色地
But he didn't spend any more time in the repository; the latest update was only a month ago.
Would be nice to get standard-schema across the finish line and some resources added to class-validator. PR: https://github.com/typestack/class-validator/pull/2588
I initiated a PR for class-validator to support the Standard Schema, and it led me to think that NestJs should not wait for it to be merged before adding support for standard-schema.
Standard Schema assumes that the validator is closely tied to the schema it uses to validate a DTO, and that a DTO can be a standard JavaScript object.
Class-validator's validator stores all validation constraints in a singleton context called "ValidationMetadata" and it relies on the DTO's constructor to identify the validation constraints related to the DTO. This means a DTO must be an instance of a class.
This difference in philosophy means that class-validator will need additional APIs to support Standard Schema's approach.
I think that building a new Validation Pipe, or adapting the current pipe to support standard-schema would be easier to maintain and faster to implement (especially since class-validator's repository does not seem not very active).
The nest-zod package basically contains all the logic we need; we just need to replace "zod" with "standard schema".
Thanks for your pr! I think it would be extremely beneficial to be apart of the core library. I think new maintainers is key and simplifying the library possibly for easier maintenance and scope.
I think that building a new Validation Pipe, or adapting the current pipe to support standard-schema would be easier to maintain and faster to implement (especially since class-validator's repository does not seem not very active).
Absolutely!!!
Nestjs needs to evolve. It's getting behind for not adopting Standard Schema in its core.
@kamilmysliwiec @micalevisk I'll investigate this issue on my 3rd party library nestia.
typia starts supporting the standard-schema, and nestia is utilizing such typia.
@BrunnerLivio How I should I do at now to support standard-schema in NestJS?
Hoping to see standard schema support get adopted!
Hey 👋 I am a co-creator of Standard Schema. Feel free to ping me if you have any specific question that our README don't answer.
I quickly looked at the "Standard Schema" documentation and two things caught my eye as... strange things:
- Any ideas why it's written like this?
if (result instanceof Promise) result = await result;
- This statement in itself is quite strange in the context of the JavaScript stack:
Do I need to add
@standard-schema/specas a dependency? No. The@standard-schema/specpackage is completely optional. You can just copy and paste the types into your project. We guarantee no breaking changes without a major version bump.If you don't mind additional dependencies, you can add
@standard-schema/specas a dependency and consume it with import type.
- Using await once is enough. Validate can be sync or async, but just write await.
- It's ok, use dep or just copy paste, version 1.0.0 will always be 1.0.0