https://github.com/typestack/class-validator/issues/1473
fix: ValidatedNested with transform not validating · Issue #1473 · typestack/class-validator
Versions: class-transformer: 0.4.0 class-validator: 0.13.2 Description Using Transform from class-transformer and ValidateNested of class-validator seems to skip validation of nested fields. Minima...
github.com
FormData로 데이터를 전달 받기 때문에 string형식으로 전달받은 object들을 JSON.parse()하기 위해서 @Transform을 사용하게 되었는데 그런경우에는 @Type()을 사용해서 @ValidateNested가 제대로 동작하지 않는 것을 발견함
위의 이슈를 통해서 방법을 알게됨
바로 JSON.parse()시 plainToInstance()를 사용하는 것!
아래 링크는 문제를 해결하게 도와준 위의 이슈의 댓글!
https://github.com/typestack/class-validator/issues/1473#issuecomment-1260948193
fix: ValidatedNested with transform not validating · Issue #1473 · typestack/class-validator
Versions: class-transformer: 0.4.0 class-validator: 0.13.2 Description Using Transform from class-transformer and ValidateNested of class-validator seems to skip validation of nested fields. Minima...
github.com
그래서 따로 해당되는 decorator를 만들게됨
export function TransformStringObject<T>(className?: ClassConstructor<T>) {
return applyDecorators(
Transform(({ value }) => {
if (value === '') {
return undefined;
}
return typeof value === 'string'
? className
? plainToInstance(className, JSON.parse(value))
: JSON.parse(value)
: value;
}),
);
}
'TIL' 카테고리의 다른 글
[Jest] 한 함수를 여러번 쓸경우, 여러 return 값이 필요시 (0) | 2023.05.24 |
---|---|
rxjs retry change parameter / rxjs retry 사용시 값 갱신 안됨 (0) | 2023.05.16 |
eslint : vscode formatOnSave (0) | 2023.02.16 |
2023.02.11 letsencrypt certbot 자동 갱신 오류 (0) | 2023.02.11 |
prisma relation deleteMany (개인 기록용) (0) | 2023.01.29 |