skip to Main Content

i am trying to validate my req.body from my api using Joi Property does not exist on type

here is my code

const idScheme = Joi.string().required();
const updatedBioScheme = Joi.object({
  bio: Joi.string()
      .min(3)
      .required(),
});

export async function
updatedBio(req: Request, res: Response,) {
  try {
    const uid = req.query.uid;
    const {err, val} = updatedBioScheme.validate(req.body);
    const {error, value} = idScheme.validate(uid);
    if (error) {
      res.status(400).json({error: "Invalid uid parameter"});
      return;
    } else if (err) {
      return res.status(400).json(err);
    }
    const userId = value;
    const {bio}= val;
    const inject = myContainer.get<ProfileServicesRepository>(TYPES.ProfileServices);
    const data = await inject.updateBio(userId, bio);
    if (data == undefined) {
      return res.status(400).json({error: "User not available yet!!"});
    }
    return res.status(200).json({data, message: "Sucessful"});
  } catch (error) {
    return res.status(400).send({error, message: "error"});
  }
}

i am getting the error from this const {err, val} = updatedBioScheme.validate(req.body);

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    i figure out the error, so i just to a value

     const {error: err, value: val} = updatedBioScheme.validate(req.body);
    

  2. According to Joi doc you have value, error. Instead you try to extract err and val which are non-existent on the object and TypeScript knows it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search