Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 591
Alberto Monteiro Avatar asked Aug 31 '17 16:08

Alberto Monteiro


1 Answers

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);
};
like image 124
Maxim Balaganskiy Avatar answered Nov 15 '22 05:11

Maxim Balaganskiy