-
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
Support for union types in schemas. #197
Comments
An alternate and cheaper to implement solution might be to provide a hook for manual validation, e.g. var people = Joi.array().includes(person.validator(function(value) {
return !(Joi.validate(value, Joi.number().integer()) &&
Joi.validate(value, person));
})); Of course, this design is much bulkier and less transparent. |
I am not following what you are asking for. All I see above is a person schema that the same as: var id = Joi.number().integer();
var item = { id: id, name: Joi.string() };
var person = [id, item]; |
In the case of an array that works, but in the case of a single item it breaks down. array() was perhaps a bad choice there because If we were adding that support anyway, it would be appealing to have a method like here's an example of the issue in the node repl: |
|
Apologies for the lack of clarity. I was hoping giving the example from the node session would help clear things up. What I meant to say was: var person = [id, item];
Joi.array().includes(person); Works great and is precisely the functionality that I want. However, Sometimes I want a 1:1 relationship var class = {
id: Joi.number().integer(),
title: Joi.string(),
people: Joi.array().includes(person), // works great
instructor: person // does not work
}; Hopefully that helps? |
Why doesn't instructor work? It will match an id or item. |
Perhaps I've made an error in the repl example I tried? Doing: Joi.validate(7, [id, item]); // does not work
Joi.validate({id: 7, name: 'John'}, [id, item]); // also does not work. Is there something I missed here? Apologies if I'm being dense. |
Works in 2.8. Try master. |
Works great in 2.8, sorry for the trouble. Thanks! |
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. |
Hey folks, we really appreciate the work you've done with Joi. We use Hapi for our web server and are now looking to centralize all validation using a single set of Joi schemas for our mongo documents.
One of the few missing features that would be very useful is support for union types in schemas. This is useful in many cases -- all variants of providing an enumeration of valid types for a field. To give on example, it would be nice to implement a transparent reference function that can accept a value matching the schema or a value matching an identifying field in the schema, in the pattern:
Which would return a type that would either accept a person value or a possible person id.
I'm hoping to graft Joi onto an ODM so we can use the same validation across the stack.
If there's anything I've missed that would let me already implement something similar please let me know. Otherwise, I'd be happy to do whatever I can to get some momentum behind this.
Cheers,
Josh
The text was updated successfully, but these errors were encountered: