我遇到了同样的问题,所以我创建了自己的ValidateNested
装饰器。
import {
ValidationOptions,
registerDecorator,
ValidationArguments,
validateSync,
} from 'class-validator';
import { plainToClass } from 'class-transformer';
/**
* @decorator
* @description A custom decorator to validate a validation-schema within a validation schema upload N levels
* @param schema The validation Class
*/
export function ValidateNested(
schema: new () => any,
validationOptions?: ValidationOptions
) {
return function (object: Object, propertyName: string) {
registerDecorator({
name: 'ValidateNested',
target: object.constructor,
propertyName: propertyName,
constraints: [],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
args.value;
if (Array.isArray(value)) {
for (let i = 0; i < (<Array<any>>value).length; i++) {
if (validateSync(plainToClass(schema, value[i])).length) {
return false;
}
}
return true;
} else
return validateSync(plainToClass(schema, value)).length
? false
: true;
},
defaultMessage(args) {
if (Array.isArray(args.value)) {
for (let i = 0; i < (<Array<any>>args.value).length; i++) {
return (
`${args.property}::index${i} -> ` +
validateSync(plainToClass(schema, args.value[i]))
.map((e) => e.constraints)
.reduce((acc, next) => acc.concat(Object.values(next)), [])
).toString();
}
} else
return (
`${args.property}: ` +
validateSync(plainToClass(schema, args.value))
.map((e) => e.constraints)
.reduce((acc, next) => acc.concat(Object.values(next)), [])
).toString();
},
},
});
};
}
然后你可以像使用它 -
class Schema2 {
@IsNotEmpty()
@IsString()
prop1: string;
@IsNotEmpty()
@IsString()
prop2: string;
}
class Schema1 {
@IsNotEmpty()
@IsString()
prop3: string;
@ValidateNested(Schema2)
nested_prop: Schema2;
}
适用于非原始数组和 javascript 对象。