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

Alternatives respect subsequent "when"s if first has only "otherwise" #1161

Merged
merged 2 commits into from
May 13, 2017

Conversation

WesTyler
Copy link
Contributor

@WesTyler WesTyler commented Apr 14, 2017

Addresses #1151 .

When a .when() has only an otherwise, subsequent when()s should now be checked until either a then is found or the last condition is reached.

Previous Buggy Behavior

const schema = Joi.object({
    a: Joi.number(),
    b: Joi.number(),
    c: Joi.number()
        .when('a', { is: 1, otherwise: Joi.number().min(1) })
        .when('b', { is: 1, then: Joi.number().min(1) })
});

schema.validate({
    a: 1,
    b: 1,
    c: 0
}); // { error: null, value: { a: 1, b: 1, c: 0 } }

New behavior

const schema = Joi.object({
    a: Joi.number(),
    b: Joi.number(),
    c: Joi.number()
        .when('a', { is: 1, otherwise: Joi.number().min(1) })
        .when('b', { is: 1, then: Joi.number().min(1) })
});

schema.validate({
    a: 1,
    b: 1,
    c: 0
}); // child "c" fails because ["c" must be larger than or equal to 1]

@@ -43,6 +43,9 @@ internals.Alternatives = class extends Any {
return baseType._validate(value, state, options);
}
}
else if (item.then === undefined && i < (il - 1)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a more important change. baseType should probably only ever be used as last resort, so removing it from the failed branch seem to make sense. I haven't tested it but my guts tell me we can simplify this code by only dealing with baseType once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see what I can do 👍

@Marsup Marsup added the bug Bug or defect label May 1, 2017
@WesTyler
Copy link
Contributor Author

WesTyler commented May 5, 2017

@Marsup I cleaned up the logic around baseType. Is that what you had in mind?

@Marsup Marsup merged commit cd01c65 into hapijs:master May 13, 2017
Marsup added a commit that referenced this pull request May 13, 2017
@Marsup
Copy link
Collaborator

Marsup commented May 13, 2017

Thanks !

@Marsup Marsup added this to the 10.4.2 milestone May 13, 2017
@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
bug Bug or defect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants