1

How to validate nested object using JOI in nodejs ("joi": "^17.3.0")

const Joi = require("joi");

const Validation = (data) => {
  const schema = Joi.object({
    details: {
      firstname: Joi.string().required(),
      lastname: Joi.string().required(),
    },
  });
  return schema.validate(data);
};

module.exports = {
  Validation,
};

req.body sample

{
    "details": {
        "firstname": "Fname",
        "lastname": "Lname"
    }
}

Validation(req.body)

Error message I'm getting ""details.firstname" is required if i'm not sending firstname property. how to get proper message like "firstname required"

Arun M
  • 133
  • 1
  • 11

2 Answers2

1

At a quick look I found out that you are missing .keys before the nested object so it should be like this.

const Joi = require("joi");

const Validation = (data) => {
  const schema = Joi.object().keys({
    details: {
      firstname: Joi.string().required(),
      lastname: Joi.string().required(),
    },
  });
  return schema.validate(data);
};

module.exports = {
  Validation,
};

Also this validation will require firstname in the body as it is required as per your validation and if you want a custom message to show on error then it would be like this.

firstname: Joi.string().required().error(() => {
    return {
        message: 'Your custom message',
    };
})
Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
  • After adding .keys still throwing same error message ("details.firstname" is required) – Arun M Jan 09 '21 at 16:33
  • If your data is same as sample then it should not return this error message. If you are not passing the `firstname` in the body then this error message will be there as per your validation i-e `firstname: Joi.string().required()`. if `firstname` is not required you can change the validation to `firstname: Joi.string()` – Hamza Anis Jan 09 '21 at 16:44
  • Joi is displaying error messages properly when sending data as object { firstname: Joi.string().required(), lastname: Joi.string().required(), } But nested objects not giving proper error messages – Arun M Jan 09 '21 at 16:54
  • I don't want to write any custom messages because JOI is already giving the default messages. – Arun M Jan 09 '21 at 17:24
  • If you don't want any validation errors make sure that your body looks like this `{"details":{"firstname":"Fname","lastname":"Lname"}}` – Hamza Anis Jan 09 '21 at 17:43
  • I need error messages, currently i'm getting error messages like this "details.firstname" is required because of the nested object schema. I want to get the error message like this "firstname" is required by following nested object schema. I don't know whether it's possible with JOI. writing custom message for every validation will be time consuming instead I can create my own validation mechanism. – Arun M Jan 09 '21 at 17:50
0

To have the desired output,install joi:version 14 (npm i joi@14), don't install latest version,because what you want available in version 14, Then you can do like the following code

const Joi = require("joi");

const Validation = (data) => {
  const schema = Joi.object({
    details: {
      firstname: Joi.string().required(),
      lastname: Joi.string().required(),
    },
  });
  let {error} = schema.validate(data)
  const { details } = error;
  const message = details.map((i) => i.message).join(",");
  console.log("error", message);
  return message;
};

module.exports = {
  Validation,
};
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39
  • Still I'm getting message as "details.firstname" is required. to get "firstname" is required I modified my code like this error.details[0].message.split('.')[1]. I know this is not the best solution but I don't have any other answer right now. – Arun M Jan 13 '21 at 05:22
  • I edited my answer because it's correct in version 14 of joi – Mohammad Yaser Ahmadi Jan 13 '21 at 06:46