如何在 Aurelia Validation 中为给定的 customRule 创建自定义方法

How to create a custom method for a given customRule in Aurelia Validation

我正在使用 aurelia-validation,并且我创建了一个 customRule。

规则验证逻辑:

export function validateCompare(value: any, obj: any, otherPropertyName: string) {
    return value === null ||
        value === undefined ||
        value === "" ||
        obj[otherPropertyName] === null ||
        obj[otherPropertyName] === undefined ||
        obj[otherPropertyName] === "" ||
        value === obj[otherPropertyName];
}

配置:

import { ValidationRules, validationMessages } from "aurelia-validation";
import { validateCompare } from "./compareValidation";

export function configureValidation() {
    validationMessages["required"] = "${$displayName} é obrigatório";
    validationMessages["email"] = "${$displayName} em formato inválido";

    ValidationRules.customRule("compare", validateCompare, "${$displayName} não confere com ${$getDisplayName($config.otherPropertyName)}", otherPropertyName => ({ otherPropertyName }));
}

使用自定义规则:

ValidationRules
    .ensure((m: ClienteEdicaoViewModel) => m.Login).required().satisfiesRule("login")
    .ensure((m: ClienteEdicaoViewModel) => m.Senha).satisfiesRule("requiredIf", "ConfirmacaoSenha").satisfiesRule("senha")
    .ensure((m: ClienteEdicaoViewModel) => m.ConfirmacaoSenha).displayName("Confirmação de Senha").satisfiesRule("requiredIf", "Senha").satisfiesRule("compare", "Senha")
    .on(ClienteEdicaoViewModel);

问题:

我正在使用打字稿,我想创建一个包装使用 satisfiesRule 的方法,我想以这种方式应用规则:

ValidationRules
    .ensure((m: ClienteEdicaoViewModel) => m.Login).required().login()
    .ensure((m: ClienteEdicaoViewModel) => m.Senha).requiredIf("ConfirmacaoSenha").senha()
    .ensure((m: ClienteEdicaoViewModel) => m.ConfirmacaoSenha).displayName("Confirmação de Senha").requiredIf("Senha").compare("Senha")
    .on(ClienteEdicaoViewModel);

如何创建 requiredIfcompare 方法并在 FluentRule 中使用它们?

C# 具有它可以执行的扩展方法,但我在打字稿中尝试了一些方法但没有成功。

您需要扩充验证模块并为原型提供实现。 这就是您的配置。

import { ValidationRules, validationMessages, FluentRuleCustomizer, FluentRules } from "aurelia-validation";
import { validateCompare } from "./compareValidation";

export function configureValidation() {
    validationMessages["required"] = "${$displayName} é obrigatório";
    validationMessages["email"] = "${$displayName} em formato inválido";

    ValidationRules.customRule("compare", validateCompare, "${$displayName} não confere com ${$getDisplayName($config.otherPropertyName)}", otherPropertyName => ({ otherPropertyName }));
}

declare module "aurelia-validation/dist/commonjs/implementation/validation-rules" {
    interface FluentRules<TObject, TValue> {
        compare(value: string): FluentRuleCustomizer<TObject, TValue>;
    }

    interface FluentRuleCustomizer<TObject, TValue> {
        compare(value: string): FluentRuleCustomizer<TObject, TValue>;
    }
}

FluentRules.prototype.compare = function (value: string) {
    return this.satisfiesRule("compare", value);
};

FluentRuleCustomizer.prototype.compare = function (value: string) {
    return this.satisfiesRule("compare", value);
};