2014-10-17 90 views
0

我需要接受一个手机号码作为网络服务的输入,但我在面对以下问题的同时使用Joi框架进行验证。错误:模式必须是RegExp

淳佳说:

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正则表达式,它似乎为atleast一些投入正常工作。我不明白为什么joi会抛出这个错误。

+0

也许你需要添加'/'上的图案的两端:'/^[0-9] * $ /' – 2014-10-17 22:19:29

回答

4

其实,记录here (link)穰框架希望这里不是一个正则表达式模式,而是实际的正则表达式。即:,你应该使用:的

Joi.string().required().regex(/^[0-9]*$/) [...] 

...相反:

Joi.string().required().regex('^[0-9]*$') [...] 
+0

谢谢..这样一个愚蠢的错误。 – guptakvgaurav 2014-10-17 22:25:48

相关问题