我是 hapi 的新手,我从简单的表单提交开始,需要验证我的表单数据。为此,我通过使用模块“joi”获得了功能。但是对于 joi 模型,我如何通过对用户名和密码等具有预先指定格式的字符串进行正则表达式验证来验证我的数据。
最佳答案
你可以这样使用
var schema = Joi.object().keys({
username: Joi.string().regex(/[a-zA-Z0-9]{3,30}/).min(3).max(30).required(),
password: Joi.string().regex(/[a-zA-Z0-9]{3,30}/),
confirmation: Joi.ref('password')
})
.with('password', 'confirmation');
// will fail because `foo` isn't in the schema at all
Joi.validate({foo: 1}, schema, console.log);
// will fail because `confirmation` is missing
Joi.validate({username: 'alex', password: 'qwerty'}, schema, console.log);
// will pass
Joi.validate({
username: 'alex', password: 'qwerty', confirmation: 'qwerty'
}, schema, console.log);
关于node.js - 如何使用正则表达式验证 hapi 和 joi 中的请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47766856/