EagleT
EagleT

Reputation: 84

Create custom FluentRule for aurelia validation

Until Aurelia-Validation 1.3.0 it was possible to add a custom FluentRule for the aurelia validation as mentioned in this thread. However, I'm not able to get this to work with a newer version, e.g. the latest (1.5.0).

My Code:

import { FluentRuleCustomizer, FluentRules, validationMessages, ValidationRules, FluentEnsure } from 'aurelia-validation';

export function validateEmailPattern(value: any, obj: any, pattern: RegExp) {
    return value === null || value === undefined || pattern.test(value);
}
export function configureValidation() {
    ValidationRules.customRule(
        'validEmailPattern',
        validateEmailPattern,
        `\${$displayName} must be a valid email format`);
}

declare module 'aurelia-validation/dist/aurelia-validation' {
    interface FluentRules<TObject, TValue> {
        validEmailPattern(value: RegExp): FluentRuleCustomizer<TObject, TValue>;
    }

    interface FluentRuleCustomizer<TObject, TValue> {
        validEmailPattern(value: RegExp): FluentRuleCustomizer<TObject, TValue>;
    }
}

FluentRules.prototype.validEmailPattern = function(value: RegExp) {
    return this.satisfiesRule('validEmailPattern', value);
};

FluentRuleCustomizer.prototype.validEmailPattern = function(value: RegExp) {
    return this.satisfiesRule('validEmailPattern', value);
};

The error message:

Unhandled promise rejection: reason=TypeError:
aurelia_validation_1.ValidationRules.ensure(...).required(...).satisfiesRule(...).ensure(...).required(...).ensure(...).required(...).satisfiesRule(...).when(...)
.ensure(...).required(...).when(...).ensure(...).required(...).when(...).then(...).validEmailPattern is not a function

Any idea or hint how this can be done with aurelia-validation 1.5.0 and typescript? I know that there is a match rule, which can be used with a pattern, but this is only an example, there are more rules that I want to define this way.

Thanks in advance for any help.

Upvotes: 1

Views: 101

Answers (1)

Maxim Balaganskiy
Maxim Balaganskiy

Reputation: 1574

Check that you haven't got a duplicate aurelia-validation folder sitting in local package dependencies. What might happen is that you patch FluentRules from a duplicate package

Upvotes: 2

Related Questions