Skip to content

validator.extend() doesn't work #17

Description

@skorykh

I used same code example as described in documentation to test ability to add custom validation rules using validator.extend() method and It looks like validator.extend() method doesn't work correctly.

Let's look on source codes. warnIf() function:

function warnIf(condition, message) {
  if (condition) {
    if (!process.env.JEST_WORKER_ID) {
      window.console.warn(warnPrefix + message);
    }
    return true;
  }
  return false;
}

And extend() method:

extend(ruleName, func, message) {
    let validApi =
        warnIf(!ruleName, 'Please specify a rule name as first argument for the validator extend() method')
        && warnIf(!func, 'Please specify a function as second argument for the validator extend() method')
        && warnIf(!message, 'Please specify a message as third argument for the validator extend() method')
        && warnIf(typeof ruleName !== 'string', 'The first argument must be a string in the validator extend() method')
        && warnIf(typeof func !== 'function', 'The second argument must be a function in the validator extend() method')
        && warnIf(typeof message !== 'string', 'The third argument must be a string in the validator extend() method')
        && warnIf( definedRules.hasOwnProperty(ruleName), 'The rule already exists!');

    if(!validApi)
        return;

    definedRules[ruleName] = func;
    this.availableRules.push(ruleName);
    this.messages[ruleName] = message;
}

As I see, if all parameters are correct, warnIf() method skips warnings and returns false. As result, validApi variable has got false value and custom validation rule will never be stored in definedRules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions