I am currently exploring how to effectively utilize my validation pipe in combination with class-validator on an API call.
My DTO is decorated with class-validator decorators and is performing as anticipated. However, I am interested in utilizing the 'skipMissingProperties' feature to bypass validation for non-existent properties (such as 'name' in the provided screenshots).
My goal is to create a straightforward DTO that utilizes various decorators and skips validation for properties that are not included.
Regrettably, it appears that my implementation of skipMissingProperties is incorrect because even when this option is set, validation errors still occur within the DTO.
How can I effectively use the skipMissingProperties option of the validation-pipe alongside class-validator decorators for properties that are present?
In the code snippet below, if I send an update request with parameters excluding 'name', the class validator generates errors at the DTO level.
Screenshot of Validation Pipe on Controller
Screenshot of UpdateViewDTO's decorators
API Controller Endpoint:
@Put(':viewId')
public async updateView(
@Req() request: RequestExtended,
@Param('viewId') viewId: string,
@Body(new ValidationPipe({ skipMissingProperties: true })) updateView: UpdateViewDto)
: Promise<View> {
// Perform API operations
}
UpdateViewDTO:
export class UpdateViewDto {
@IsString()
@MinLength(1, {
message: LanguageElements.VIEW_NAME_REQUIRED_ERROR_MSG,
})
@MaxLength(50, {
message: LanguageElements.VIEW_NAME_TOO_LONG_ERROR_MSG,
})
public readonly name?: string;
// Other properties
}