[英]Joi error: ValidationError: "value" must be of type object
I was wondering why JOI is returning both an error and a value:我想知道为什么 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);
};
You need to pass req.body
to validateStuff, if you want no error.如果你不想出错,你需要将
req.body
传递给 validateStuff。
Your code passes the string from the name
property right away您的代码立即传递
name
属性中的字符串
Or you could change your Joi schema to const Joi.string().min(3).required()
或者您可以将您的 Joi 架构更改为
const Joi.string().min(3).required()
Your question states you wonder why schema.validate returns both an error and a value.您的问题表明您想知道为什么 schema.validate 会同时返回错误和值。
When destructuring like you did const { error, value} = schema.validate()
you can do the following, because you could have an error at runtime or none.当像
const { error, value} = schema.validate()
您可以执行以下操作,因为您可能在运行时出现错误或没有错误。
if (error) {
// handle error
// passed value might be needed
} else {
// validation successful
}
Ran into this issue, but for a different reason than the other answer above.遇到这个问题,但原因与上面的其他答案不同。 My error message:
我的错误信息:
{"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.