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
joi.valid with object or array #1348
Comments
Typically this can just be built out explicitly using For your array of objects example above, you should be able to apply the same idea to const obj1Schema = Joi.object().keys({
a: Joi.number().valid(1).required(),
b: Joi.number().valid(2).required()
});
const obj2Schema = Joi.object().keys({
a: Joi.number().valid(3).required(),
b: Joi.number().valid(4).required()
});
const arraySchema = Joi.array().items([obj1Schema, obj2Schema]).unique().length(2);
arraySchema.validate([{a: 1, b: 2}]); // ValidationError: "value" must contain 2 items
arraySchema.validate([{a: 1, b: 2}, {a: 3, b: 5}]); // ValidationError: "value" at position 1 does not match any of the allowed types
arraySchema.validate([{a: 1, b: 2}, {a: 1, b: 2}]); // ValidationError: "value" position 1 contains a duplicate value
arraySchema.validate([{a: 1, b: 2}, {a: 3, b: 4}]); // error: null In other words, I think usually if the problem can be solved with schema composition then we do not need to add new complexity to the Joi core api. I believe this falls into that category. :) |
Thanks, this gets me started! I would like to suggest that deepEqual functionality would be very useful in core, because when wanting to assert that something exactly equals something else, the above solution is verbose. It would be great to see at a glance that something needs to equal exactly something, with the same keys and the same order and uniqueness and everything. If anything differs at all then the deepEqual check fails. The following is a very concise way of asserting what is needed, and is very readable and understandable. schema = joi.equals([
{ a: 1, b: 2 },
{ a: 3, b: 4 },
{ a: 5, b: 6 },
]),
}) While the following works but is less readable. And this is a fairly simple case. In my use cases I'll have larger and more complex objects I need to assert, which would lead to a lot of verbosity in the code. schema = joi.array().items([
joi.object().keys({
a: joi.number().valid(1).required(),
b: joi.number().valid(2).required(),
}),
joi.object().keys({
a: joi.number().valid(3).required(),
b: joi.number().valid(4).required(),
}),
joi.object().keys({
a: joi.number().valid(5).required(),
b: joi.number().valid(6).required(),
}),
]).unique().length(2) |
Are you using it like assertion framework in your tests? Why not use tools made for that? |
First, you can use literal values. A schema of |
@hueniverse Where can you use that? I can't do so in @Marsup It's not for tests, it's for ensuring that certain values returned from APIs exactly match various values, since some keys are fixed (based on the inputs), and the others change. However, if deepEqual functionality were available then an additional use case for joi could also for test assertions. |
It's a shorthand for the
i just didn't use that notation in my original example because I didn't want to introduce another notation into the mix :P A short version of the example above would be schema = joi.array().items([
joi.object().keys({
a: 1,
b: 2,
}),
joi.object().keys({
a: 3,
b: 4,
}),
joi.object().keys({
a: 5,
b: 6,
}),
]).unique().length(2).options({ presence: 'required' }) |
https://github.com/hapijs/joi/blob/master/API.md#compileschema contains a comprehensive example of all the cases supported. |
This issue seems old enough that it's dead or solved by now, closing. Reopen if you still want to discuss it. |
Hi, I'd like to reopen the issue but can't do it since I wasn't the one who closed it. I still think having some kind of deepEqual functionality would be very useful. I'm using joi to assert most things, and then I have to separately assert that for example an array exactly equals something else, which isn't a clean solution. It would be nice to have joi handle all the assertions I want to make. |
How about you implement it in an extension? |
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 want to validate that a value is exactly equal to something. But if I do
it doesn't match that the object exactly equals the array of objects. With arrays, doing
joi.valid([1, 2, 3 ])
checks that the values can be either 1, 2, or 3, rather than verifying that it's exactly equal to[1, 2, 3]
. How can I make this work?Could some kind of
joi.deepEqual
be added? It's useful for cases where let's say you're receiving something back from a JSON API and need to make sure that some of the keys meet some criteria, but other keys need to exactly equal something.The text was updated successfully, but these errors were encountered: