我需要接受手机号码作为 Web 服务的输入,但是在使用 Joi 框架进行验证时遇到了以下问题。
乔伊 说:
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)
验证:
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
}
};
}();
我检查了 freeformatter 上的正则表达式,它似乎对至少一些输入工作正常。我不明白为什么 joi 会抛出这个错误。