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

kailashyogeshwar85 picture kailashyogeshwar85  路  4Comments

PaunPrashant picture PaunPrashant  路  3Comments

kevbook picture kevbook  路  4Comments

a-c-m picture a-c-m  路  3Comments

leore picture leore  路  4Comments