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

Complex conditional validation #1917

Closed
dnttam opened this issue Jun 21, 2019 · 2 comments
Closed

Complex conditional validation #1917

dnttam opened this issue Jun 21, 2019 · 2 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@dnttam
Copy link

dnttam commented Jun 21, 2019

Describe the problem you are trying to fix (provide as much context as possible)

So I have the following (simplified) schema:

{
   from: 2019-01-01,
   to: 2019-02-01,
   isAnnual: true,
}

The validation I need is this if "isAnnual' is true - then make sure that the duration between "to" and "from" is no more than 364 days.

I tried to do something like this:

const from = Joi.date().max(Joi.ref('to')).format('YYYY-MM-DD').raw().required();
const to = Joi.date().format('YYYY-MM-DD').raw().required();
const isAnnual = Joi.boolean();

const getDaysInSpan = (from, to) => {
	const days =  moment(to).diff(moment(from), 'days');
	return days;
};

const event = Joi.object().keys({
	from,
	to,
	isAnnual,
}).when('isAnnual', { is: true, then: getDaysInSpan(Joi.ref('to'), Joi.ref('from')) < 365 });

Bit it doesn't work - the actual date object doesn't get to the getDaysInSpan function - the to,from are joi refs.

Can this be done?

Which API (or modification of the current API) do you suggest to solve that problem ?

Don't know

Are you ready to work on a pull request if your suggestion is accepted ?

Not at the moment.

@dnttam dnttam closed this as completed Jun 21, 2019
@dnttam dnttam reopened this Jun 23, 2019
@hueniverse
Copy link
Contributor

Not right now. You cannot perform calculations during validation and references are only resolved at runtime. This may or may not be supported in the future.

@hueniverse hueniverse self-assigned this Jun 23, 2019
@hueniverse hueniverse added the support Questions, discussions, and general support label Jun 23, 2019
@hueniverse hueniverse changed the title complex conditional validation - is it possible? Complex conditional validation Jun 25, 2019
@hueniverse hueniverse added request and removed support Questions, discussions, and general support labels Jun 25, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone Jun 25, 2019
@hueniverse hueniverse reopened this Jun 25, 2019
@hueniverse
Copy link
Contributor

            const schema = Joi.object({
                annual: Joi.boolean().required(),
                from: Joi.date().required(),
                to: Joi.date().required()
                    .when('annual', { is: true, then: Joi.date().max(Joi.var('{number(from) + 364 * day}')) })
            });

@hueniverse hueniverse added the v16 label Aug 11, 2019
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

2 participants