Joi: Joi validate ignores nested keys

Created on 18 Oct 2018  路  3Comments  路  Source: sideway/joi

  • node version: v9.2.0
  • joi version: 13.4.0
  • environment node:
  • used with standalone:
  • any other relevant information:

This is my Joi validation:

let schema = Joi.object().keys({
  personal_info: Joi.object().keys({
    first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').required().error(JoiCustomErrors),
    last_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').required().error(JoiCustomErrors),
    phone: Joi.string().min(10).max(10).regex(Regex.num, 'num').required().error(JoiCustomErrors),
    nickname: Joi.string().min(3).max(12).regex(Regex.alphanum, 'alphanum').required().error(JoiCustomErrors),
    birthday: Joi.date().max(`01-01-${new Date().getFullYear()-8}`).required().error(JoiCustomErrors),
    IDNumber: Joi.string().min(9).max(9).regex(Regex.num, 'num').required().error(JoiCustomErrors),
    address: Joi.object().keys({
      city: Joi.string().valid(Cities).required().error(JoiCustomErrors),
      street: Joi.string().min(2).max(15).regex(Regex.alphabeta, 'alphabeta').required().error(JoiCustomErrors),
      house_number: Joi.string().min(1).max(5).regex(Regex.alphanum, 'alphanum').error(JoiCustomErrors)
    })
  }),
  permission_level: Joi.number().min(1).max(9).required().error(JoiCustomErrors)
});
Joi.validate(req.body, schema, { abortEarly: false }, (err) => {
  if (err) return cast.joiError(err);
  return create_employee(result);
});

Explain:

All nested keys that are inside personal_info object are not getting checked. That means - If I take first_name and put it on parent (not under personal_info) it's getting checked by the schema validator - as should be.

What am I doing wrong?

support

Most helpful comment

I'm not sure what you expect without an example of failure. I'm going to assume you're missing the required on that key if you expect it to exist.

All 3 comments

I'm not sure what you expect without an example of failure. I'm going to assume you're missing the required on that key if you expect it to exist.

@Marsup Thanks alot that worked.

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JbIPS picture JbIPS  路  4Comments

PaunPrashant picture PaunPrashant  路  3Comments

builtbybrayne picture builtbybrayne  路  4Comments

jamesdixon picture jamesdixon  路  4Comments

kevbook picture kevbook  路  4Comments