简体   繁体   中英

Joi error: ValidationError: "value" must be of type object

I was wondering why JOI is returning both an error and a value:

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.

Your code passes the string from the name property right away

Or you could change your Joi schema to const Joi.string().min(3).required()

Your question states you wonder why schema.validate returns both an error and a value.

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.

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"}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM