Typescript 扩展现有验证器并仅设置一些选项

Typescript 扩展现有验证器并仅设置一些选项,typescript,typescript-decorator,class-validator,Typescript,Typescript Decorator,Class Validator,“我的数据库”列的类型为双精度(来自) 双精度8字节可变精度,不精确15位小数精度 我想使用类验证器进行精度检查 @IsNumber() /* precision check */ public myValue: number; IsDecimaldecorator可能在这里有所帮助,因此@IsDecimal({decimal\u digits:'15'})可能会起作用。我将不得不对多个字段使用此装饰器,是否有方法扩展现有装饰器并只传入decimal\u digits选项?我认为重新发明轮子是

“我的数据库”列的类型为双精度(来自)

双精度8字节可变精度,不精确15位小数精度

我想使用类验证器进行精度检查

@IsNumber()
/* precision check */
public myValue: number;
IsDecimal
decorator可能在这里有所帮助,因此
@IsDecimal({decimal\u digits:'15'})
可能会起作用。我将不得不对多个字段使用此装饰器,是否有方法扩展现有装饰器并只传入
decimal\u digits
选项?我认为重新发明轮子是没有意义的。如果我可以继承验证,但将精度设置为小于或等于15,那就太好了

目前我创建了自己的装饰器

@ValidatorConstraint()
class IsDoublePrecisionConstraint implements ValidatorConstraintInterface {
    public validate(value: any): boolean {
        if (typeof value === 'number') {
            if (value % 1 === 0) {
                return true;
            }

            const valueText: string = value.toString();
            const valueSegments: string[] = valueText.split('.');
            const decimalDigits: string = valueSegments[1];

            return decimalDigits.length <= 15;
        }

        return false;
    }

    public defaultMessage(args: ValidationArguments): string {
        return `${args.property} must have less than or equal to 15 decimal digits.`;
    }
}

export function IsDoublePrecision() {
    return (object: Record<string, any>, propertyName: string) => {
        registerDecorator({
            target: object.constructor,
            propertyName,
            validator: IsDoublePrecisionConstraint,
        });
    };
}
@ValidatorConstraint()
类IsDoublePrecisionConstraint实现ValidatorConstraintInterface{
公共验证(值:任意):布尔值{
如果(值的类型=='number'){
如果(值%1==0){
返回true;
}
常量valueText:string=value.toString();
常量valueSegments:string[]=valueText.split('.');
常量小数位数:string=valueSegments[1];
返回小数位数{
寄存器检测器({
目标:object.constructor,
propertyName,
验证程序:IsDoublePrecisionConstraint,
});
};
}
但我不确定这一个是否能处理每一个案件


提前感谢

我没有发现任何关于扩展
类验证程序
的现有修饰符的示例,但是
IsDecimal
只是一个普通的属性修饰符,然后我们可以将其用作属性修饰符

我的想法是创建一个“普通”属性装饰器,并在此装饰器中使用
decimal\u digits
选项调用
IsDecimal

// function as a const
export const IsDoublePrecision = () => { // use decorator factory way
  return (target: object, key: string) => { // return a property decorator function
    IsDecimal({ decimal_digits: '15' })(target, key); // call IsDecimal decorator
  }
}
用法:

@IsNumber()
/* precision check */
@IsDoublePrecision()
public myValue: number;

非常感谢,您介意在这里解释一下速记语法吗?
IsDecimal({decimal_digits:'15'})(目标,键)第二,我通过传入各种值来测试它,比如
48.4445
48.44454684564587884754567458948
48.12345678912345
,但每次我收到这个错误消息时:(“值不是有效的十进制数。”很抱歉打扰您,我还创建了自己的装饰器并更新了我的question@Question3r太多人认为需要解释我的代码,但在排序中,
IsDecimal
是一个decorator工厂,当您调用
IsDecimal()时,它将返回一个decorator
。属性装饰器将为类创建一些
元数据
,元数据存储在中,用于关于属性名称和验证类型…它将用于
类验证器的
验证
功能。调用
(目标,键)
正在为使用
IsDoublePrecision
的类应用
IsDecimal
的逻辑。我不知道
IsDecimal
的具体规格,但使用
@IsDecimal({decimal\u digits:'15')时,您将得到相同的错误
而不是
IsDoublePrecision
使用相同的测试用例。当
myValue
是一个相似的数字字符串时,它将通过。