Joi: How to validate a nested object?

Created on 12 Dec 2014  路  1Comment  路  Source: sideway/joi

I want to validate that my notes are either null or an array and if it's an array I want to validate the object attributes in the array.

Here's an example of what I want to validate

"notes" : [ 
  {
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed bibendum egestas erat facilisis convallis. Aliquam a libero erat. Quisque aliquet."
  }
]

How can I validate that if notes is an array, validate that notes.description is a string. Otherwise if notes is null, do not validate notes.description?

Here's what I have started, but i think it's way off:

notes: [Joi.array(), Joi.allow(null)]
notes.description: ???
support

Most helpful comment

notes: Joi.array().includes(Joi.object({ description: Joi.string().required() })).allow(null)

>All comments

notes: Joi.array().includes(Joi.object({ description: Joi.string().required() })).allow(null)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manjeettahkur picture manjeettahkur  路  19Comments

kanongil picture kanongil  路  17Comments

ivan-kleshnin picture ivan-kleshnin  路  48Comments

gebrits picture gebrits  路  22Comments

Marsup picture Marsup  路  135Comments