如何解决类验证器:使用类成员作为装饰器参数
IsPostalCode
上的 zip
需要知道 countryCode/locale,这是其他类成员之一。
是否可以使用类成员作为装饰器参数?
import {
IsEmail,IsISO31661Alpha2,IsPostalCode,Isstring
} from "class-validator"
export class SignupDto {
@IsEmail()
email: string
@Isstring()
password: string
@IsISO31661Alpha2()
countryCode: string
// Something like this
@IsPostalCode(this.countryCode)
zip: string
}
解决方法
由于装饰器在打字稿中的工作方式,装饰器不能固有地使用它们内部的类属性。您可以创建一个 custom validation decorator 来读取类的其他属性并正确验证 zip 属性,但这可能需要一些工作才能开始。
所以回答你的问题:
是否可以使用类成员作为装饰器参数?
不,不是。
,您可以创建一个自定义验证器,如下所示:
import {
ValidationOptions,registerDecorator,ValidationArguments,buildMessage,} from 'class-validator';
/**
* Install validator package from npm. class-validator uses validator under the
* hood
*/
import {isISO31661Alpha2,isPostalCode} from 'validator';
export function IsPostalCodeOf(
property: string,validationOptions?: ValidationOptions,) {
// eslint-disable-next-line @typescript-eslint/ban-types
return function(object: Object,propertyName: string) {
registerDecorator({
name: 'isPostalCodeOf',target: object.constructor,propertyName: propertyName,constraints: [property],options: validationOptions,validator: {
validate(value: any,args: ValidationArguments) {
// Getting the country code field from the argument.
// countryCode field from SignupDto
const [countryCodeField] = args.constraints;
// Getting the value of the countryCode Field
const countryCode = (args.object as any)[countryCodeField];
// Checking if the country code is valid even though it is checked
// at class level
if (!isISO31661Alpha2(countryCode)) {
// Invalid county code
return false;
}
// Checks if the value (zip) belongs in the extracted countryCode
// field
return isPostalCode(value,countryCode);
},// Specifiy your error message here.
defaultMessage: buildMessage(
eachPrefix =>
`${eachPrefix} $property must be a valid postal
code in the specified country `,validationOptions,),},});
};
}
用法:
export class SignupDto {
@IsEmail()
email: string
@IsString()
password: string
@IsISO31661Alpha2()
countryCode: string
@IsPostalCodeOf('countryCode')
zip: string
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。