You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
It is not possible to automatically validate an empty object with forbidUnknownValues #1503
Let's imagine that we are developing an application architecture on which application programmers will then layer business logic. We are currently creating a validation layer that will automatically validate incoming inputs from clients, simply by obtaining an input metatype that will be described by our future application programmer. Of course for these, we use class-validator library
But if in many months, our application developer decides to describe the following class for input:
exportclassEmptyObjectDto{}
He will be very surprised, because the library will take a seemingly valid object for an invalid one, since there will not be a full registered decorator in it and it will give an error
Accordingly, he will not have any opportunity to give an empty input to our validation layer, and it will be necessary to add extra if->else from the area of responsibility to which he does not belong
The technical problem here is that without decorators assigned class-validator doesn't know about this class so when it receives an instance of it it will think it's an unknown value.
We need to discuss how should we add support for this. My first (so probably not the best) idea would be to add a class decorator something like @ValidatedClass:
@ValidatedClass()exportclassEmptyObjectDto{}
This way class-validator will know about it and the validation will pass.
PS: I am moving this issue to discussions, so the community can brainstorm, iterate and propose an API that we can discuss.
Activity
NoNameProvided commentedon Nov 16, 2022
Yes, this use case should be supported.
The technical problem here is that without decorators assigned
class-validatordoesn't know about this class so when it receives an instance of it it will think it's an unknown value.We need to discuss how should we add support for this. My first (so probably not the best) idea would be to add a class decorator something like
@ValidatedClass:This way
class-validatorwill know about it and the validation will pass.PS: I am moving this issue to discussions, so the community can brainstorm, iterate and propose an API that we can discuss.