【问题标题】:Joi error validation throws error in node jsJoi错误验证在节点js中抛出错误
【发布时间】:2021-01-31 21:00:12
【问题描述】:

我正在使用 JOI 来验证请求对象,如下所示 (JOI 版本使用“joi”:“^17.2.1”)

const Joi=require('joi');
app.post('/api/saveMovieList',(req,res)=>{
  var schema = Joi.object().keys({
    name: Joi.string().min(5).max(10).required()
  });
  
  constr result=Joi.validate(req.body, schema);
  console.log(result);
});

因为 Joi.validate 不是函数而出现错误

如果我使用

constr result=schema.validate(req.body, schema);
console.log(result);

将错误消息作为无效的消息选项

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您是否真的尝试过阅读文档?甚至是第一个例子?是schema.validate( req.body) 假设req.body 已经是一个合适的json 对象
  • 我已阅读文档,这是我的第一个示例。 const schema = { name: Joi.string().alphanum().min(3).max(30).required() }; console.log(schema.validate(req.body, schema));
  • 明白了,谢谢 const schema = Joi.object({ name: Joi.string().min(6).required() });常量验证 = schema.validate(req.body);

标签: node.js npm joi


【解决方案1】:

以下代码解决了我的问题

const schema = Joi.object({
        name: Joi.string().min(6).required()
    });
        
    const validation = schema.validate(req.body);

【讨论】:

    猜你喜欢
    • 2020-06-14
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多