Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jmgarciaromero opened this issue Nov 22, 2017 · 1 comment
Closed

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

jmgarciaromero opened this issue Nov 22, 2017 · 1 comment
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Milestone

Comments

@jmgarciaromero
Copy link

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');
@Marsup Marsup added the bug Bug or defect label Nov 22, 2017
@Marsup Marsup self-assigned this Aug 6, 2018
@Marsup Marsup added the breaking changes Change that can breaking existing code label Aug 6, 2018
@Marsup Marsup added this to the 14.0.0 milestone Aug 6, 2018
@Marsup Marsup closed this as completed in 7e0fea8 Oct 14, 2018
@lock
Copy link

lock bot commented Jan 9, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants