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

Multiple alternatives with default #763

Closed
2j2e opened this issue Nov 4, 2015 · 4 comments
Closed

Multiple alternatives with default #763

2j2e opened this issue Nov 4, 2015 · 4 comments
Assignees
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Milestone

Comments

@2j2e
Copy link

2j2e commented Nov 4, 2015

Imagine than we have a,b,c fields.

a: Joi.string().required(),
b: Joi.any()
    .when('a', {is: 'Other', then: Joi.any(), otherwise: Joi.string().required()}),
c: Joi.alternatives()
    .when('a', {is: 'Other', then: Joi.string().required()})
    .when('b', {is: 'Other', then: Joi.string().required()})

a - is always required.
b - required only if a != 'Other'
c - required only if a == 'Other' OR b == 'Other'

If none of c rules has been applied c should be optional()
How should I rebuild my schema to get last rule working?

@Marsup
Copy link
Collaborator

Marsup commented Nov 4, 2015

This is currently a bug I meant to tackle a while ago.

@Marsup Marsup added the bug Bug or defect label Nov 4, 2015
@Marsup Marsup self-assigned this Dec 17, 2015
@Marsup Marsup added the breaking changes Change that can breaking existing code label Dec 17, 2015
@Marsup Marsup added this to the 8.0.0 milestone Dec 17, 2015
@Marsup Marsup closed this as completed in 6584063 Dec 17, 2015
@Marsup
Copy link
Collaborator

Marsup commented Feb 14, 2016

Sorry for the delay, fix is finally published.
Final schema could look like :

const schema = Joi.object({
    a: Joi.string().required(),
    b: Joi.when('a', { is: 'Other', otherwise: Joi.string().required() }),
    c: Joi.string()
        .when('a', { is: 'Other', then: Joi.required() })
        .when('b', { is: 'Other', then: Joi.required() })
});

As you can see from b and c, type of the alternative can be inside or outside whether you want it to be a string anyway, your choice.

@oguzhannkayaa
Copy link

My project is almost same as yours. But in my project a is array. When I wrote a: Joi.array().required(),
other conditions does not work. But when I do a: Joi.string().required() it works perfectly. How can I convert it to array?

@Marsup
Copy link
Collaborator

Marsup commented Sep 20, 2018

Stop spamming dude and open your own issue.

@hapijs hapijs locked as resolved and limited conversation to collaborators Sep 20, 2018
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

3 participants