[Question] How to "remove" inherit decorator ?
See original GitHub issueThere is an elegant way to āremoveā inherit decorator like @IsOptional in child class ?
Iām using class-validator with Nest to validate my input data with some DTO class.
I need to have a parent class with all fields optional and a child class with some mandatory fields.
So i have a CreateAccountDto wich inherit from AccountDto
The idea behind is to have different inputs DTO like CreateAccountDto and avoid repetition.
Actually i have :
export class AccountDto {
@IsUUID('4')
@IsOptional()
@ApiModelProperty()
readonly account_id: string;
@IsString()
@IsNotEmpty()
@MaxLength(200)
@IsEmail()
@IsOptional()
@ApiModelProperty()
readonly email: string;
@IsString()
@IsNotEmpty()
@MaxLength(100)
@IsOptional()
@ApiModelProperty()
readonly password: string;
}
and
export class CreateAccountDto extends AccountDto {
@Equals(undefined)
@RemoveApiModelProperty()
readonly account_id: string;
@RemoveIsOptional()
readonly email: string;
@RemoveIsOptional()
readonly password: string;
}
const removeValidationPropertyDecorator = (metakey: string): PropertyDecorator => {
return (target: object, propertyKey: string | symbol) => {
const validationMetadatasKey = 'validationMetadatas';
const validationMetadatas: ValidationMetadata[] = getFromContainer(MetadataStorage)[validationMetadatasKey];
_.remove(validationMetadatas, (validationMetadata: ValidationMetadata) => {
return validationMetadata.propertyName === propertyKey && validationMetadata.type === metakey;
});
};
};
export const RemoveIsOptional = (): PropertyDecorator => {
return removeValidationPropertyDecorator(ValidationTypes.CONDITIONAL_VALIDATION);
};
My actual solution is to have a custom @RemoveIsOptional decorator, but iām not sure is the best way to accomplish my goal !?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:13
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Over-riding methods remove decorators inherited from base ...
Not really. When you over-ride a method, you replace it completely. Of course, you can use super to access the original parent method,...
Read more >Decorator - Refactoring.Guru
Inheritance is static. You can't alter the behavior of an existing object at runtime. You can only replace the whole object with another...
Read more >Multiple Inheritance in Python - GeeksforGeeks
When a class is derived from more than one base class it is called multiple Inheritance. The derived class inherits all the features...
Read more >text-decoration - CSS: Cascading Style Sheets - MDN Web Docs
This means that if an element specifies a text decoration, then a child element can't remove the decoration. For example, in the markup...
Read more >The Composition Over Inheritance Principle
A crucial weakness of inheritance as a design strategy is that a class often needs ... it might seem a clear win for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

For NestJS users, have you seen this: https://trilon.io/blog/introducing-mapped-types-for-nestjs ?
I donāt know if this can satisfy all your needs, but this doesnāt exist when I created this issue 𤷠And I think that would have helped us a lot.
In the meantime we have implemented/used another solution on our side ;
Related PR : https://github.com/typestack/class-validator/pull/161
Digging deeper i found next solution for my use case, maybe it still would be useful for others. Insipred by https://github.com/typestack/class-validator/issues/164#issuecomment-369874196
setupOptionalValidators.ts
This one should be called after importing all DTOs. In my case iām using TsED v5, so i call it if
$afterRoutesInit.Now i can write my models in convenient way: (@Property and @Required are TsED v5 decorators used here for swagger schema)
End when i validating my requests - i just provide [ācreateā] or [āupdateā] groups for
validate();P.S. For validation inside TsED v5 i am overriding default
ValidationPipe:So now when calling
@BodyParams()inside ControllerValidationPipewill add [āupdateā] group, while calling@Required() @BodyParams()or@BodyParams({ required: true })will add [ācreateā] group.