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

Suggested solution for cyclic dependencies #195

Closed
latviancoder opened this issue Apr 5, 2018 · 2 comments
Closed

Suggested solution for cyclic dependencies #195

latviancoder opened this issue Apr 5, 2018 · 2 comments

Comments

@latviancoder
Copy link

latviancoder commented Apr 5, 2018

Lets imagine we have an object with two child objects, but both of them optional.

{
  one: { ... },
  two: { ... }
}

My problem is I have to check for required fields inside of these objects, but only if these objects themselves exist. So I naively tried the following:

    two: object()
        .when('two', {
            is: (two) => typeof two !== 'undefined',
            then: TwoSchema
        })

...and of course got cyclic dependency error.

What I ended up doing was passing 2 additional variables to context: $isOneDefined/$isTwoDefined.
Is there a better approach?

@jquense
Copy link
Owner

jquense commented Apr 5, 2018

two: yup.lazy(value => value !== undefined ? TwoSchema : object())

?

@latviancoder
Copy link
Author

Incredible stuff, thanks for fast answer.

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

2 participants