I am currently utilizing aurelia-validation and have developed a custom rule.
Logic for Rule Validation:
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} is required";
validationMessages["email"] = "${$displayName} is in an invalid format";
ValidationRules.customRule("compare", validateCompare, "${$displayName} does not match ${$getDisplayName($config.otherPropertyName)}", otherPropertyName => ({ otherPropertyName }));
}
Usage of the Custom Rule:
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("Confirmation Password").satisfiesRule("requiredIf", "Senha").satisfiesRule("compare", "Senha")
.on(ClienteEdicaoViewModel);
Inquiry:
I am working with typescript and would like to create a method that simplifies the use of the satisfiesRule
. I want to apply rules as follows:
ValidationRules
.ensure((m: ClienteEdicaoViewModel) => m.Login).required().login()
.ensure((m: ClienteEdicaoViewModel) => m.Senha).requiredIf("ConfirmationPassword").password()
.ensure((m: ClienteEdicaoViewModel) => m.ConfirmationPassword).displayName("Confirmation Password").requiredIf("Password").compare("Password")
.on(ClienteEdicaoViewModel);
Is there a way to create the requiredIf
and compare
methods and incorporate them into the FluentRule?
C# has extension methods that could achieve this, but my attempts in TypeScript have been unsuccessful so far.