1

I want to validate an array that contains several objects using Joi. Although I cannot find a way to mark one of the objects in the array list as "required = false".

Is there a way to accomplish this with Joi?

Khalil Najjar
  • 115
  • 3
  • 9

1 Answers1

1

Try this way

Joi.array().items(Joi.object({
   // Object schema
}))

Ref link 1

Ref link 2

Sachin Shah
  • 4,503
  • 3
  • 23
  • 50
  • what if one of the objects matches, I want to ignore the other schema and move to the next data from an array? https://stackoverflow.com/questions/74658952/joi-validations-if-object-matches-the-schema-validate-against-it-from-multiple – Bravo Dec 02 '22 at 17:57
  • 1
    @Bravo I don't think so that can be possible till I know. – Sachin Shah Dec 03 '22 at 19:04
  • Thanks @Sachin Is there any other way I can achieve that functionality ? – Bravo Dec 05 '22 at 01:05
  • 1
    Yes of course @Bravo you can implement through your business logic. i.e you can loop with your JSON data and make joi payload, once any object matches you can skip rest of the data in loop and go ahead. – Sachin Shah Dec 05 '22 at 08:24
  • Thanks. If you have any examples can you please share with me as I am totally new to this. – Bravo Dec 05 '22 at 13:57