Joi: Joi.array() does not allow arguments when trying to validate an array of objects.

Created on 30 Apr 2018  路  8Comments  路  Source: sideway/joi

Context

When trying to validate a json response of this type

{
          this_is_an_array : [
                           {
                                 id : 1234,
                                 name : abcd
                           },
                           {
                                 id : 1243
                                 name : bacd
                            }
                                        ]
}

The corresponding schema fails for version13.1.2 but passes for 9.0.0.

module.exports={
schema : joi.object().keys({
   this_is_an_array : joi.array([{
         id : joi.number().required(),
         name : joi.string().required()
                                           }])
                                       })
}
  • node version: 8.6.0
  • joi version: 13.1.2
  • environment (node, browser): Using with Chakram API Framework 1.5.0
  • used with (hapi, standalone, ...): No
  • any other relevant information: None

What are you trying to achieve or the steps to reproduce ?

  • Define the schema.

  • Match API response against the defined schema.

Describe your issue here, include schemas and inputs you are validating if needed.

The schema defined above (and below) fails for version 13.1.2 but successfully validates for 9.0.0. Is there any change regarding how the validation of an array of objects is done?

Which result you had ?

Joi.array() does not allows arguments

What did you expect ?

The schema should successfully validate against the given response.

non issue

Most helpful comment

Use Joi.array().items(), found here.

All 8 comments

It wasn't checked before but was completely ignored. I'm not sure if your issue is a feature request or a bug report.

More like a bug @Marsup . It would have been a feature request if it would have been completely removed. But I see that between two versions, there is a dis-similarity between how things are accomplished.

Well no. You were wrong passing an argument to that function in the 1st place, it didn't accomplish anything, all the new joi does is prevent that.

So, now I cannot pass any arguments to the joi.array() method? If so, am I wrong in using the schema that worked with version 9.0.0 ? We have a couple of other projects using the same thing - having schema structure like that (I actually referred their code).

It never worked, it was strictly equivalent to just having joi.array(), now is not different. You have to provide it to items() for it to work, in 9 and 13 equally.

Ah. Thanks for clearing up. How do I create a valid schema for this json then?

Use Joi.array().items(), found here.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohamadresaaa picture mohamadresaaa  路  3Comments

alekbarszczewski picture alekbarszczewski  路  3Comments

REBELinBLUE picture REBELinBLUE  路  3Comments

Dreamystify picture Dreamystify  路  4Comments

sergibondarenko picture sergibondarenko  路  3Comments