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

标签 typescript aurelia

我正在使用 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);

问题:

我正在使用 typescript ,我想创建一个方法来包装 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# 有它可以执行的扩展方法,但我在 typescript 中尝试了一些方法但没有成功。

最佳答案

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

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);
};

关于typescript - 如何在 Aurelia Validation 中为给定的 customRule 创建自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45986196/

相关文章:

dom - Aurelia - 从生成的 DOM 元素中访问 ViewModel 函数/绑定(bind)

javascript - 不要滚动元素调整大小

Javascript如何使用filter()内部的forEach()过滤数组

javascript - knockout 可观察订阅 : maximum call stack exceeded

javascript - Aurelia 中的 Angular 2 EventEmitter

javascript - 如何: Extend the Aurelia Logger with additional methods

typescript - 在 typescript 库项目中组织 NPM 模块导出的最佳实践?

angular - 增强第三方库 Typescript

javascript - firebase js 版本 > 3.6.3 破坏了与 aurelia-cli 的捆绑

javascript - 在 Aurelia 中聚焦一个元素