[英]How to set custom error message IsEnum of class-validator in nestjs
I'm using nestjs and I'm using the @IsEnum(Enum)
keyword.我正在使用nestjs,并且正在使用
@IsEnum(Enum)
关键字。
If the values that can be entered into the enum are A, B, C
, and if B1
is entered, an error occurs.如果枚举中可以输入的值为
A, B, C
,如果输入了B1
则会出错。
response: {
statusCode: 400,
message: [ 'each value in tag must be a valid enum value' ],
error: 'Bad Request'
},
status: 400
It simply displays the above log.它只是显示上面的日志。 I want to know what value is wrong.
我想知道什么值是错误的。
If I entered A,B1,C
, I would like to receive an error message saying 'The value of B1 is incorrect'.如果我输入
A,B1,C
,我想收到一条错误消息,提示“B1 的值不正确”。
@IsEnum(EnumName, { each: true })
enumValues: EnumName[] = [];
import { difference } from 'lodash';
@IsEnum(EnumName, {
message: (args: ValidationArguments) => {
const { value, constraints } = args;
const correctValues = Object.values(constraints[0]);
const incorrectValues = difference(value, correctValues);
return `The values of ${incorrectValues} are incorrect`;
},
each: true,
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.