Validation Joi Array

trying to verify that an array has zero or more rows in one case and that it has zero or more objects in another while fighting Joi docs :(

validate: { headers: Joi.object({ 'content-type': "application/vnd.api+json", accept: "application/vnd.api+json" }).options({ allowUnknown: true }), payload : Joi.object().keys({ data : Joi.object().keys({ type: Joi.any().allow('BY_TEMPLATE').required(), attributes: Joi.object({ to : Joi.string().email().required(), templateId : Joi.string().required(), categories : Joi.array().items( //trying to validate here that each element is a string), variables : Joi.array({ //also trying to validate here that each element is an Object with one key and value }) }) }).required() }) } 
+34
source share
4 answers

Joi.array().items() accepts another Joi scheme for use with array elements. Thus, an array of strings is easy:

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

The same goes for an array of objects; just pass the object diagram to items() :

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

You can try this:

 function(data){ const Schema = { categories: Joi.array().items(Joi.string()), variables: Joi.array().items(Joi.object().keys().min(1)) } return Joi.validate(data, Schema) } 
+1
source

Joi.array().items(Joi.string().required(), Joi.number().required()); found it here here

-1
source
 validate: { headers: Joi.object({ 'content-type': "application/vnd.api+json", accept: "application/vnd.api+json" }).options({ allowUnknown: true }), payload : Joi.object().keys({ data : Joi.object().keys({ type: Joi.any().allow('BY_TEMPLATE').required(), attributes: Joi.object({ to : Joi.string().email().required(), templateId : Joi.string().required(), categories : Joi.array().items(Joi.string()), variables : Joi.array().items(Joi.object().keys().max(1)) }) }).required() }) } 
-2
source

Source: https://habr.com/ru/post/1265146/


All Articles