【发布时间】: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);