Skip to content

Need to override twice the key name for error messages #1364

@jmgarciaromero

Description

@jmgarciaromero

Context

  • node version: 6.11.5
  • joi version: 13.0.1
  • environment: browser
  • used with: webpack 3 and joi-browser 13.0.1

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

I'm using custom labels for error messages using the following schema

schema = Joi.object().keys({
    a: Joi.boolean(),
    b: Joi.when('a', {
      is: true,
      then: Joi.string().empty('').allow(null),
    }).label('Label b'),
    c: Joi.when('a', {
      is: true,
      then: Joi.string().empty('').allow(null),
    }).label('Label c'),
    d: Joi.when('a', {
      is: true,
      then: Joi.string().empty('').allow(null),
    }).label('Label d'),
  })
  .or('b', 'c', 'd');

schema.validate({a: true, b: 1})

Which result you had ?

"child "Label b" fails because ["b" must be a string]"

What did you expect ?

"child "Label b" fails because ["Label b" must be a string]"

If I want the label to be correct either in the Joi.string() validation or in the .or() validation I need to override twice the key name for each child.

schema = Joi.object().keys({
    a: Joi.boolean(),
    b: Joi.when('a', {
      is: true,
      then: Joi.string().empty('').allow(null).label('Label b'),
    }).label('Label b'),
    c: Joi.when('a', {
      is: true,
      then: Joi.string().empty('').allow(null).label('Label c'),
    }).label('Label c'),
    d: Joi.when('a', {
      is: true,
      then: Joi.string().empty('').allow(null).label('Label d'),
    }).label('Label d'),
  })
  .or('b', 'c', 'd');

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changesChange that can breaking existing codebugBug or defect

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions