-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Required based on value on other key #166
Comments
Can you suggest an API for this feature? |
how about: withvalue(value,[fieldnames]) with something like: internals.Any.prototype.withvalue = function (testValue) {
var args = Array.prototype.slice.call(arguments);
var peers = Utils.flatten(args.slice(1));
for (var i = 0, li = peers.length; i < li; i++) {
Utils.assert(typeof peers[i] === 'string', 'peers must be a string');
}
this._test('withvalue', args, function (value, state, options) {
if(testValue !== value) {
return null;
}
if (!state.parent) {
return internals.Any.error('any.with.parent', null, state);
}
for (var i = 0, il = peers.length; i < il; ++i) {
if (!state.parent.hasOwnProperty(peers[i]) ||
state.parent[peers[i]] === undefined ||
state.parent[peers[i]] === null ||
state.parent[peers[i]] === '') {
return internals.Any.error('any.with.peer', { value: peers[i] }, state);
}
}
return null;
});
return this;
}; |
@HughePaul looks pretty limited for a very narrow use case. I'm looking for something more generic where you can express any condition based on this or other key values. |
perhaps a requiredif([conditions]) way of looking at it? title: Joi.string().required().valid('Mr','Mrs','Ms','Other'),
otherTitle: Joi.string().requiredif( {'title': Joi.string().valid('Other')} ) |
I am looking for something more along the lines of string().condition(some_condition, morerules()) but without needing to parse the condition. |
The first thing I tried was passing an object to {
names: Joi.array().with({ batch: true })
} That didn't work obviously. But to me that is the most obvious from an API usage perspective. I'm not familiar enough with the code itself to know wether or not that would be possible. |
Again, that's a very narrow way of doing conditions. |
I agree. I'm not sure I understand what you mean by not having to parse the condition though. Care to explain? |
Do you mean that the condition would be a function, like:
or (if, then) validation parameters
of .if().then() promise style:
or perhaps chains that can fail early, but that might be difficult to work out what is a condition fail and what is an error to report:
Obviously there could be complex validation and complex requirements specified in those sort of formats, but I think there is also still milage in a simple .withIfValue('value','requiredField'...) format as that may cover a lot of conditional requirements I've come across building APIs. |
I see the .if().then() format as the prettiest of these, and I could see it implemented as follows:
To validate these clauses the value would be validated against each 'if' chain, and if it didn't error then the value would also be validated against the 'then' chain. (with object() parents set up etc) does this make any sense? |
maybe if could take an optional field name to let you reference a value of another object property, so for the OP's issue the solution could be:
So the definitions of if() and then() could be:
|
Replaced by #194 |
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. |
I have the following JSON objects:
With the current implementation of Joi is it possible to make
names
required only ifbatch
istrue
and not allowed ifbatch
isfalse
? Reading the docs I think no? But I'm asking because I might be missing something! :)If not is it something that might be added to Joi or should I do that check "post validation"?
The text was updated successfully, but these errors were encountered: