I'm having trouble on validating nested object when I try to update a specific prop of a nested object e.g.:
import { Type } from 'class-transformer';
import { IsOptional, IsString, ValidateNested } from 'class-validator';
class Nested {
@IsString()
@IsOptional()
readonly propA: string;
@IsString()
@IsOptional()
readonly propB: string;
}
export class UpdateDto {
@IsOptional()
@Type(() => Nested)
@ValidateNested()
readonly nested: Nested;
}
If I try to pass a dotted key like { "nested.propA": "test" } I get the following error
{
"statusCode": 400,
"message": [
"property nested.propA should not exist"
],
"error": "Bad Request"
}
A simplest solution can be adding in the parent object the dotted keys eg:
class Nested {
@IsString()
@IsOptional()
readonly propA: string;
@IsString()
@IsOptional()
readonly propB: string;
}
export class UpdateDto {
@IsOptional()
@Type(() => Nested)
@ValidateNested()
readonly nested: Nested;
@IsString()
@IsOptional()
readonly 'nested.propA': string; <==== THIS
@IsString()
@IsOptional()
readonly 'nested.propB': string; <==== THIS
}
With that approach I can pass both the entire object and a single prop, but it seems really redundant to me.
Any other way to validate using a "dot" syntax? The db is mongo where the dot syntax can be used to updating a specific prop of a nested object.
Thanks.