Validating array body
See original GitHub issueHi,
Not sure if this is a bug or not, but let me explain what I want to do and the results I see.
I want to validate a that the body of a POST is a JSON array of objects. I have a schema like this:
{
body: Joi.array().items(Joi.object()).required()
}
If I send in body data that is an array, [{id:1}]
then I get this error:
Error: \"context\" must be an object\n
at Object.internals.checkOptions (/Users/23046666/Projects/so-domain/node_modules/joi/lib/any.js:38:15)\n
at [object Object].internals.Any.applyFunctionToChildren.internals.Any._validateWithOptions (/Users/23046666/Projects/so-domain/node_modules/joi/lib/any.js:628:19)\n
at [object Object].root.validate (/Users/23046666/Projects/so-domain/node_modules/joi/lib/index.js:104:23)\n
at validate (/Users/23046666/Projects/so-domain/node_modules/express-validation/lib/index.js:77:7)\n
at /Users/23046666/Projects/so-domain/node_modules/express-validation/lib/index.js:42:24\n
at Array.forEach (native)\n
at /Users/23046666/Projects/so-domain/node_modules/express-validation/lib/index.js:39:55
It seems that Joi requires the body to be an objext. And to verify, If I send body data that is an object, {id:1}, then I don’t see the error. (But instead the validation catches it, as it should.)
Looking at the code it seems that the body is set to the context
attribute in options sent to Joi, and Joi checks that it is a Joi.object(). This was not the case in Joi 8.1.0, but has changed somewhere to Joi 8.4.2.
Do we really need to set the context
attribute to the payload? Or is there any other way to keep it possible to validate arrays in the body?
Thanks!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:9
Top GitHub Comments
Here is a quick example of how to fix this issue by setting
contextRequest
totrue
.I am using v1.0.2 and still can’t use array for body. Any updates?