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.
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:
And extend() method:
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.