typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.94k stars 789 forks source link

feat: run asynchronous decorators in order when synchronous decorators are present on the same property #1926

Open riley-pikus opened 1 year ago

riley-pikus commented 1 year ago

Description

This is a clone of issue 1688 as it should not be closed. Per my comment:

It is expected behavior in the typescript ecosystem that decorators run bottom to top. Here it is in the official Microsoft docs:

https://www.typescriptlang.org/docs/handbook/decorators.html#decorator-composition

Is there a reason that class-validator wouldn't conform to these standards? Per the author of this issue, this should be labeled a bug

Let me know if you'd like me to copy/paste their description here rather than just linking to follow the normal Issue reporting pattern

braaar commented 1 year ago

Interesting. I haven't given this much thought previously. I guess it's up to @NoNameProvided to comment on this. Perhaps this is a matter of dev resources, and if community contributors are willing to do the work it will be accepted.

braaar commented 1 year ago

After looking into this a bit more, synchronous validators do run in bottom to top. Are you suggesting that async validators should run as though they were synchronous validators if there are synchronous validators on the same property?

riley-pikus commented 1 year ago

Correct,

For example, If I want to validate that what the user sent me in their input is a "Valid ID in my other system", I'd like to decorate that property like so:

@IsValidIDInOtherSystem()
@IsUUID()

but you'll notice that in practice, non valid UUIDS make it to the decorator when I expected they shouldn't have

braaar commented 1 year ago

Could you rename this issue to feat: run asynchronous decorators in order when synchronous decorators are present on the same property?