title: How to validate certain fields of a nested json object using JOI validation
body: <p>I need help regarding how to validate certain fields of a nested json object using JOI validation. In my example I have an object which contains two sub objects i.e. <code>clientObj</code> and <code>agentObj</code>. I'm only interested in validating the <code>username</code> field which is required but I don't want to validate the remaining fields. If I only mention that field, by deleting all other fields, in my schema and <code>joi.validate()</code> function I get 422 error. Code is given below:</p>
  
  <pre><code>exports.callAuthentication = function (req, res, next) {
  
      let connectSchema = {
          clientObj: joi.object().keys({
              name: joi.string().min(3).max(38),
              email: joi.string().min(3).max(38),
              language: joi.string().min(3).max(38),
              username: joi.string().min(3).max(38).required(),
              mobile_no: joi.string().min(3).max(38),
              time_zone: joi.string().min(3).max(38),
              system_phone: joi.string().optional().allow('').min(3).max(38),
              phone_no_info: joi.any().optional().allow(''),
              voicemail_pin: joi.string().min(3).max(38),
              display_picture: joi.string().min(3).max(38),
              external_extension: joi.string().min(3).max(38)
  
          }),
          agentObj: joi.object().keys({
              userId: joi.number(),
              username: joi.string().min(3).max(38).required(),
              name: joi.string().min(3).max(38),
              email: joi.string().min(3).max(38),
              status: joi.string().min(3).max(38),
              role: joi.string().min(3).max(38)
          })
      };
  
      const data = req.body;
  
      joi.validate(data, connectSchema, (err) =&gt; {
          if (err) {
              // send a 422 error response if validation fails
              res.status(422).json({
                  status: 'error',
                  message: err.details[0].message
              });
          } else {
              req.body = data;
              next();
          }
      });
  }
  </code></pre>