gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:42:55 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com