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

I am using aurelia-validation and I created customRule.

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 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 use typescript, and I would like to create a method that wraps the use satisfiesRule, I would like to apply the rules as follows:

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 to create the methods requiredIfand compareand use them FluentRule?

C # has extension methods that it could do, but I tried several methods in typescript without success.

+4
source share
1 answer

. .

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

Source: https://habr.com/ru/post/1684837/


All Articles