使用Joi进行密码验证

时间:2020-09-04 11:06:33

标签: node.js express validation joi

我正在尝试验证用户输入的密码。密码的最小长度为4,必须包含字符和数字。我正在使用Joi npm模块进行验证。

app.js代码:

const schema = Joi.object({
  email: Joi.string().email().required(),
  username: Joi.string().alphanum().min(3).required(),
  password: Joi.string().min(4).required(),
});
app.post('/register',async (req,res)=>{
  try{
       const value = await schema.validateAsync({
         email: req.body.email,
         username: req.body.username,
         password: req.body.password,
        });

  }catch(e){ 
  console.log(e)}
})

如何检查用户密码中是否同时包含字符和数字?我我不在这里使用正则表达式。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您可以使用switch (key) { case value: /* code executed here */ // break; default: /* more code executed here */ break; }

alphanum()

签出https://joi.dev/api/?v=17.2.1#stringalphanum