Knockout-Validation icon indicating copy to clipboard operation
Knockout-Validation copied to clipboard

Pattern validation fails when the message is an empty string

Open jdevalk1 opened this issue 10 years ago • 2 comments
trafficstars

When specifying the pattern binding, and the message is empty:

    this.value.extend({
        pattern: { params: Constants.NAME_REGEX, message: "" }
    });

Then in addExtenders call the wrong addRule method will be invoked, and upon processing of the pattern validation, it will not use the regular expression, but the actual params object, which will not work so well.

            if (params && (params.message || params.onlyIf)) { //if it has a message or condition object, then its an object literal to use
                return kv.addRule(observable, {
                    rule: ruleName,
                    message: params.message,
                    params: utils.isEmptyVal(params.params) ? true : params.params,
                    condition: params.onlyIf
                });
            } else {

Suggested fix:

            if (params && (params.message !== undefined || params.onlyIf !== undefined)) { //if it has a message or condition object, then its an object literal to use
                return kv.addRule(observable, {
                    rule: ruleName,
                    message: params.message,
                    params: utils.isEmptyVal(params.params) ? true : params.params,
                    condition: params.onlyIf
                });
            } else {

jdevalk1 avatar Jul 31 '15 19:07 jdevalk1

I marked this issue as wontfix because I see it as an invalid usage. I will leave this issue open for a while though just in case others want to comment on it.

crissdev avatar Dec 08 '15 22:12 crissdev

It only was an issue for us as we hooked up out validation messages to a translation library and during unit testing the library was returning nulls for translated strings.

However in our unit test we did validate the object, resulting in unexplained behaviour.

I'd say it's not valid usage as well, but sometimes what is put in the message is beyond the developers direct control (or at least - not directly obvious)

The fact that it takes a different codepath depending on the message is kind of tricky and made it hard to debug as well.

Date: Tue, 8 Dec 2015 14:13:41 -0800 From: [email protected] To: [email protected] CC: [email protected] Subject: Re: [Knockout-Validation] Pattern validation fails when the message is an empty string (#577)

I marked this issue as wontfix because I see it as an invalid usage. I will leave this issue open for a while though just in case others want to comment on it.

— Reply to this email directly or view it on GitHub.

jdevalk1 avatar Dec 10 '15 15:12 jdevalk1