2

I am using bellow code:

const coll = [
              { id: 1, name: 'John' },  
              { id: 2, name: 'Jemmy' },  
              { id: 3, name: 'Jenny' }  
            ];

const schema = Joi.object().keys({ 
        name: Joi.string().min(3).required()
      }); 
return schema.validate(coll);   

when my coll array is valid, then also when checking the schema, it shows the following, always going to the error section.

schema validator
{
  value: [
    { id: 1, name: 'Action' },
    { id: 2, name: 'Horror' },
    { id: 3, name: 'Comedy' }
  ],
  error: [Error [ValidationError]: "value" must be of type object] {
    _original: [ [Object], [Object], [Object] ],
    details: [ [Object] ]
  }
}
Heartbit
  • 1,698
  • 2
  • 14
  • 26
Veronica
  • 21
  • 2

1 Answers1

0

If you want to validate an array containing objects, you could use

const schema = Joi.array().items(Joi.object().keys({
    name: Joi.string().min(3).required()
}))
PederF
  • 33
  • 5