Knockout-Contrib / Knockout-Contrib/Knockout-Validation

Pattern validation fails when the message is an empty string

Open
#577 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

wontfix
Dominant language
JavaScript
Stars
1k
Forks
366
PR merge metrics
No merged PRs in 30d

Description

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 {

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at addExtenders and inspect the branch that handles pattern parameters with an empty message. Verify the empty message is treated as a defined option while the regular expression remains the rule parameters, then confirm pattern validation works with message: "".

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.