Skip to content

joi errors not overriding for nested objects #1528

@overflowz

Description

@overflowz

Context

  • node version: 8.x
  • joi version: latest
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone

What are you trying to achieve or the steps to reproduce ?

const userSchema = Joi.object().keys({
  city: Joi.object().keys({
    id: Joi.number().required().error(new Error('invalid city id')),
  }).required().error(new Error('city is required')),
});

Which result you had ?

it returns 'city is required' unless all the nested objects are valid.

What did you expect ?

display error for each part.

while this works without .error(), I want to be able to send custom error messages for each part.

For example:

const userSchema = Joi.object().keys({
  city: Joi.object().keys({
    id: Joi.number().required().error(new Error('invalid city id')),
  }).required().error(new Error('city is required')),
});

userSchema.validate({ city: { id: 'asfasf' } }); // returns 'city is required', while I'm expecting it to return 'invalid city id'

Metadata

Metadata

Assignees

Labels

supportQuestions, discussions, and general support

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions