2014-10-17 87 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

相關問題