Joi validation of array

后端 未结 6 930
梦如初夏
梦如初夏 2021-02-02 05:29

trying to validate that an array has zero or more strings in one case and that it has zero or more objects in another , struggling with Joi docs :(

validate: {
          


        
6条回答
  •  盖世英雄少女心
    2021-02-02 05:45

    Joi.array().items() accepts another Joi schema to use against the array elements. So an array of strings is this easy:

    Joi.array().items(Joi.string())
    

    Same for an array of objects; just pass an object schema to items():

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

提交回复
热议问题