question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validating array body

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

6reactions
btavcommented, May 14, 2019

Here is a quick example of how to fix this issue by setting contextRequest to true.

const Joi = require('joi');

const exampleSchema = Joi.array()
  .items(
    Joi.object().keys({
      example: Joi.number().required(),
    })
  )
  .required();

module.exports = {
  example: {
    body: exampleSchema,
    options: {
      contextRequest: true,
    },
  },
}
3reactions
mytharchercommented, Jan 28, 2018

I am using v1.0.2 and still can’t use array for body. Any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

NestJs validate array in request body - Stack Overflow
To validate the array, create a dedicated class which contains a property that wraps the array, or use the ParseArrayPipe .
Read more >
Validate array in Body decorator · Issue #612 - GitHub
I am having a similar issue. However, I am trying to setup @Body to accept a json object from the request, but reject...
Read more >
Validating arrays and nested values in Laravel - LogRocket Blog
Validating arrays and nested values allows you to more effectively deal with data transmitted from your frontend.
Read more >
array — Understanding JSON Schema 2020-12 documentation
There are two ways in which arrays are generally used in JSON: List validation: a sequence of arbitrary length where each item matches...
Read more >
Laravel JSON Array Validation - Example & Important Tips
Laravel Validate Array of ObjectsHow to validate JSON array in Laravel as well as some important things to take note of because JSON...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found