Skip to content

It is not possible to automatically validate an empty object with forbidUnknownValues #1503

Closed

Description

@machendos

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

According to the recommendation from the documentation(https://github.com/typestack/class-validator#passing-options), we use the setting forbidUnknownValues: true directly in the call to the validate method

But if in many months, our application developer decides to describe the following class for input:

export class EmptyObjectDto {}

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

Can we get around this somehow?

Activity

  1. NoNameProvided commented on Nov 16, 2022

    @NoNameProvided
    Member

    Yes, this use case should be supported.

    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()
    export class EmptyObjectDto {}

    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.

  2. locked and limited conversation to collaborators on Nov 16, 2022
  3. converted this issue into a discussion #1782 on Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions