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

is it possible to have nested .whens()? #335

Closed
TiagoFuelber opened this issue Oct 8, 2018 · 8 comments
Closed

is it possible to have nested .whens()? #335

TiagoFuelber opened this issue Oct 8, 2018 · 8 comments

Comments

@TiagoFuelber
Copy link

TiagoFuelber commented Oct 8, 2018

I'm trying to achieve nested .when() calls, but with no success.

I'm trying something similiar to the following example:

const schema = Yup.object().shape({
  day: Yup.number(),
  month: Yup.number(),
  year: Yup.number().when("day", {
    is: 1,
    then: Yup.number.when("month", {
      is: 1,
      then: Yup.number().required("Its january, please select the year"),
      otherwise: Yup.number().required(
        "Its not january, please select the year"
      )
    }),
    otherwise: Yup.number()
  })
});

Does anyone have an idea of how can I achieve that?

@TiagoFuelber TiagoFuelber changed the title is it possible to have nested .whens()? is it possible to have nested .whens()? Oct 8, 2018
@jquense
Copy link
Owner

jquense commented Oct 8, 2018

Nested whens don't work unfortunately, but you can flatten them by doing a when for multiple keys at once

when(['day', 'month'], { is: (day, month) => true, ... })

@ugeng
Copy link

ugeng commented Oct 11, 2018

Hi, @jquense. Is it possible at all? And how it could be achieved?

I mean, nestingwhens, not combining them, which is not good in a matter of reusabilty and testabilty of switchers like below.

 passport: yup.string().whenLawful(
    schema => schema.passport().whenServicesSelected(
      passportSchema => passportSchema.required('Passport is required'),
      passportSchema => passportSchema.notRequired(),
    ),
    schema => schema.notRequired(),
  ),

@jquense
Copy link
Owner

jquense commented Oct 11, 2018

@ugeng at the moment no there isn't any support for it. There isn't a strong reason why, but it's complicated enough that it's not super simple to add. I'm happy to evaluate a PR tho if someone is up for it.

@ugeng
Copy link

ugeng commented Oct 11, 2018

Well, I suppose, strong reasons are purity, readability and testability of code...
I wish I have more free time for that...
At the moment, conditional logic in my schemes is not way too complicated (and I really can "flatten" predicates seamlessly), but when it does I would gladly (or sadly) dive deeper in this feature..
Thanks.

@vinicius5581
Copy link

vinicius5581 commented Jan 9, 2019

While this doesn’t exist yet, have anyone found a workaround or would have a suggestion on how do deal with a validation like the following?

const schematic = Yup.object().shape({
    aBoolean: Yup.boolean(),
    aThing: Yup.object().when("aBoolean", {
           is: true,
           then: Yup.object().shape({
                 aString: Yup.string(),
                 anotherString: Yup.string().when("aString", {
                        is: "theRightThing",
                        then: Yup.string().required()
                  })
           }).required()        
    })
})

@sibelius
Copy link

@jquense any plans to support nested whens?

@darrensw777
Copy link

darrensw777 commented Apr 25, 2020

As of quarantine 2020, this now works as you think it should:

lastDancingDay: Yup.string().when('myDance', {
    is: true,
    then: Yup.string().when('doYouTango', {
        is: 'Yes',
        then: Yup.string().required('Dancing shoes required'),
    }),
}),

@wasssim1
Copy link

I think the .nullable() is still not handled in nested "whens"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants