问题
I am using aurelia-validation, and I created a customRule.
Rule validation logic:
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];
}
Configuration:
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 }));
}
Using the customRule:
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);
Question:
I am using typescript, and I would like to create a method that wraps the use of the satisfiesRule
, I would like to apply rules in this way:
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);
How can I create those requiredIf
and compare
methods and use it in the FluentRule?
C# has extensions methods that it would be able to do, but I tried some ways in typescript without no success.
回答1:
You need to augment the validation module and provide the implementation to the prototype. This is what your configuration should look like.
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);
};
来源:https://stackoverflow.com/questions/45986196/how-to-create-a-custom-method-for-a-given-customrule-in-aurelia-validation