[英]Joi error: ValidationError: "value" must be of type object
我想知道为什么 JOI 同时返回一个错误和一个值:
app.post("/api/courses", (req, res) => {
const { error, value } = validateStuff(req.body.name);
console.log(`error: ${error}
value: ${value}`);
validateStuff = (course) => {
const schema = Joi.object({
name: Joi.string().min(3).required(),
});
return schema.validate(course);
};
如果你不想出错,你需要将req.body
传递给 validateStuff。
您的代码立即传递name
属性中的字符串
或者您可以将您的 Joi 架构更改为const Joi.string().min(3).required()
您的问题表明您想知道为什么 schema.validate 会同时返回错误和值。
当像const { error, value} = schema.validate()
您可以执行以下操作,因为您可能在运行时出现错误或没有错误。
if (error) {
// handle error
// passed value might be needed
} else {
// validation successful
}
遇到这个问题,但原因与上面的其他答案不同。 我的错误信息:
{"statusCode":400,"error":"Bad Request","message":"Error validating request payload","propertyErrors":{"value":"\"value\" must be of type object"}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.