Knockout-Validation
Knockout-Validation copied to clipboard
Pattern validation fails when the message is an empty string
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 {
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.
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.