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

Using multiple when conditions #1376

Closed
monsieurBoutte opened this issue Dec 12, 2017 · 6 comments
Closed

Using multiple when conditions #1376

monsieurBoutte opened this issue Dec 12, 2017 · 6 comments
Assignees
Labels
support Questions, discussions, and general support

Comments

@monsieurBoutte
Copy link

Context

  • node version: 6.11.3
  • joi version: 10.0.6
  • environment (node, browser): browser
  • used with (hapi, standalone, ...): standalone
  • any other relevant information: if I comment out one condition at a time, both work on their own.

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

I've got a radio button group that needs a two part validation schema with both parts conditionally required based off of each other's values. If a user selects the last option 'other', an input field will appear for them to manually enter another number

The first validation should, require there be a selection of the first three if the fourth option is not selected.

The second validation should be, if the selection value is 'other', ignore the input mask and require the input be filled with a 10 digit phone number.

screen shot 2017-12-11 at 8 58 19 pm

const cardContactSchema = {
  selectedCardNumber: Joi.string()
    .label('Phone Number')
    .when('otherPreferredCardNumber', {
      is: Joi.string().empty(''),
      then: Joi.string().required(),
    }),
  otherPreferredCardNumber: Joi.string()
    .label('Phone Number')
    .when('selectedCardNumber', {
      is: 'other',
      then: Joi.string()
        .replace(/\D/g, '')
        .length(10)
        .required()
        .options({
          language: {
            string: {
              length: 'length must be {{limit}} digits',
            },
          },
        }),
      otherwise: Joi.string().optional(),
    }),
};

Which result you had ?

Error: item added into group otherPreferredCardNumber created a dependencies error(otherPreferredCardNumber)

What did you expect ?

The first validation should, require there be a selection of the first three if the fourth option is not selected.

The second validation should be, if the selection value is 'other', ignore the input mask and require the input be filled with a 10 digit phone number.

@Marsup Marsup self-assigned this Dec 13, 2017
@Marsup Marsup added the support Questions, discussions, and general support label Dec 13, 2017
@Marsup
Copy link
Collaborator

Marsup commented Dec 13, 2017

Well it's a circular dependency so obviously it won't work, but I don't think you need it to be circular since selectedCardNumber is always required, your 1st when is useless.

@monsieurBoutte
Copy link
Author

monsieurBoutte commented Dec 13, 2017

sorry let me clarify,

The first validation should require there be a selection of the four options available. If and only if the fourth option "other" is selected, should it trigger the second validation on otherPreferredCardNumber

If I remove the first when this schema will require both of them. Theoretically, the otherPreferredCardNumber should be run when the selection is equal to "other" but for some reason, it's running regardless once I remove the first when

@Marsup
Copy link
Collaborator

Marsup commented Dec 13, 2017

Still doesn't make a difference to me, selectedCardNumber will have a value, be it a card number or 'other'.

@Marsup
Copy link
Collaborator

Marsup commented Aug 3, 2018

Inactive, reopen if it's still an issue.

@Marsup Marsup closed this as completed Aug 3, 2018
@mi-mazouz
Copy link

mi-mazouz commented Aug 20, 2019

Hi everyone,

I have the same issue when I'm trying to validate dates depending on each others, here my code:

endDate: Joi.date()
    .greater(
      moment()
      .startOf('day')
      .toDate()
    )
    .when('startDate', {
      is: Joi.exist(),
      then: Joi.date().greater(Joi.ref('startDate'))
    })
    .optional(),
startDate: Joi.date()
    .greater(
      moment()
      .startOf('day')
      .toDate()
    )
    .when('endDate', {
      is: Joi.exist(),
      then: Joi.date().less(Joi.ref('endDate'))
    })
    .optional()

error: item added into group startDate created a dependencies error

Do you guys have any ideas to solve this problem?

@hueniverse
Copy link
Contributor

See release notes for v16.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

4 participants