Skip to content

fix: Inheriting Validation decorators are not working properly #1201

@userchem123

Description

@userchem123

Description

Inheriting Validation decorators are not working properly.
I have a property name in base class which is optional and derived class has same property name which is required.

When I assign name as null/ doesn't define name and call validateOrReject(model); It doesn't throw exception/error. It applies on base model which it thinks as optional and doesn't think derived model has it as required property.

Minimal code-snippet showcasing the problem
I have a base class say

    export class Emp {
         @IsNotEmpty()
         @IsString()   
         id: string;
        
         @IsOptional()
         @IsString()   
         name: string;
   }

I have derived class which extends Emp

    export class Emp1 extends Emp {
        
         @IsNotEmpty()
         @IsString()   
         name: string;
}

Expected behavior

Validation should apply from derived model say name is required in this case and should throw error.

Actual behavior

Validation is being applied only from base model say name is optional in this case and doesn't throw error.

Docs say ,
When you define a subclass which extends from another one, the subclass will automatically inherit the parent's decorators. If a property is redefined in the descendant class decorators will be applied on it both from that and the base class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions