Reputation: 4691
I need to accept a mobile number as input of web-service, but i am facing following issue while validating with Joi framework.
Joi says:
Error: pattern must be a RegExp
at Object.exports.assert (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/ragchews/node_modules/joi/node_modules/hoek/lib/index.js:524:11)
at internals.String.regex (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/ragchews/node_modules/joi/lib/string.js:107:10)
at /home/gaurav/Gaurav-Drive/code/nodejsWorkspace/ragchews/src/validators/userValidator.js:10:40
at Object.<anonymous> (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/ragchews/src/validators/userValidator.js:13:2)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
for validation:
var userProfileValidation = function(){
return {
payload : {
uid: Joi.string().required().alphanum().length(userConfigs.UID_LENGTH),
mobile_num: Joi.string().required().regex('^[0-9]*$').length(userConfigs.RMN_LENGTH) //for this guy
}
};
}();
I checked the regex on freeformatter and it seems to work fine for atleast some inputs. I don't understand why joi is throwing this error.
Upvotes: 2
Views: 7688
Reputation: 9618
Actually, as documented here (link) the Joi framework expects here not a regex pattern, but an actual regex. i.e.: you should use:
Joi.string().required().regex(/^[0-9]*$/) [...]
... instead of:
Joi.string().required().regex('^[0-9]*$') [...]
Upvotes: 12