2017-08-31 6 views
4

aurelia-validationを使用していて、customRuleを作成しました。Aurelia検証で特定の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]; 
} 

は、Configuration: customRuleを使用して

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#には拡張メソッドがありますが、成功することなくタイプスクリプトでいくつかの方法を試しました。

答えて

4

検証モジュールを拡張し、プロトタイプに実装を提供する必要があります。 これはあなたの設定がどのように見えるかです。

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

そして、質問のおかげでところで、それはより良い、あまりにも私のコードを作った:) –

+0

感謝そんなに、私は、 ''宣言モジュール「オーレリア・検証」をしようとし、それが働いた完全な場所を使用して、うまく行われていました! !!! –

+0

fantastic answer –

関連する問題