nest icon indicating copy to clipboard operation
nest copied to clipboard

Compatibility with standard-schema

Open BrunnerLivio opened this issue 9 months ago • 13 comments
trafficstars

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 ValidationPipe that is compatible with standard-schema
  • Update the docs how the ValidationPipe can 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

BrunnerLivio avatar Jan 30 '25 11:01 BrunnerLivio

Notably, there is an issue open on class-validator side around this topic as well: https://github.com/typestack/class-validator/issues/2577

BrunnerLivio avatar Jan 30 '25 11:01 BrunnerLivio

I like the ideia but I think this should start as a 3rd-party lib to see community's adoption

micalevisk avatar Jan 30 '25 11:01 micalevisk

I guess there's nothing we can do until class-validator supports the standard schema too (to avoid breaking changes)

kamilmysliwiec avatar Jan 30 '25 12:01 kamilmysliwiec

Unfortunately, the class-validator isn't updated much anymore。https://github.com/typestack/class-validator/issues/1775

0x0bit avatar Feb 21 '25 03:02 0x0bit

@0x0bit well

Image

micalevisk avatar Feb 21 '25 20:02 micalevisk

@0x0bit出色地

Image

But he didn't spend any more time in the repository; the latest update was only a month ago.

0x0bit avatar Feb 24 '25 04:02 0x0bit

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

niemyjski avatar Mar 11 '25 21:03 niemyjski

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".

GregoireBellon avatar Mar 12 '25 08:03 GregoireBellon

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.

niemyjski avatar Mar 12 '25 11:03 niemyjski

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!!!

0x0bit avatar Mar 13 '25 10:03 0x0bit

Nestjs needs to evolve. It's getting behind for not adopting Standard Schema in its core.

Christopher96u avatar May 02 '25 14:05 Christopher96u

@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?

samchon avatar May 08 '25 07:05 samchon

Hoping to see standard schema support get adopted!

drusellers avatar Jun 13 '25 02:06 drusellers

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.

fabian-hiller avatar Sep 28 '25 00:09 fabian-hiller

I quickly looked at the "Standard Schema" documentation and two things caught my eye as... strange things:

  1. Any ideas why it's written like this?
if (result instanceof Promise) result = await result;
  1. This statement in itself is quite strange in the context of the JavaScript stack:

Do I need to add @standard-schema/spec as a dependency? No. The @standard-schema/spec package 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/spec as a dependency and consume it with import type.

KostyaTretyak avatar Sep 28 '25 06:09 KostyaTretyak

  1. Using await once is enough. Validate can be sync or async, but just write await.
  2. It's ok, use dep or just copy paste, version 1.0.0 will always be 1.0.0

arch-fan avatar Oct 01 '25 05:10 arch-fan