Skip to content

question: can we perform a oneOf validation? #616

Closed
@manan

Description

@manan

Description

How to perform a oneOf validation of a nested object i.e., child object is either of TypeA or TypeB? This seems like a very common use case, is there an out-of-the-box solution for this? I understand that this problem is concerning class-transformer more than it is class-validator..

Is there an example of a generic @OneOf()decorator? Any ideas on how to work with class-transformer and class-validator together to make this happen?

Reproduction

class TypeA {

    @IsString()
    public foo;
}

class TypeB {

    @IsString()
    public bar;
}

class Parent {

    @IsObject()
    @ValidateNested()
    @Type((): Function => TypeA)  // ???
    public child: TypeA | TypeB;
}

Environment

  • nodejs: 8.11.2
  • browser
  • framework/library

class-validator version: 0.12.2

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