The validation array contains a specific value using joi
I am looking for a way to check that an array contains the required value using joi .
Found these questions online - # 1 , # 2 , but none of them have a definitive answer.
I tried several things, but they don't seem to work, for example:joi.array().items(joi.string().allow('required-string').required())
joi.array().items(joi.string().label('required-string').required())
joi.array().items(joi.string().valid('required-string'))
This is what I am trying to achieve:
Received:
['required-string'], ['required-string', 'other'], ['other','required-string'], ['other',...,'required-string',....,'more-other']
Denied:
[], ['other'], [null], etc..
source to share
You can use array.items
by specifying all allowed types. If the specified type .required()
, then the array must contain the corresponding element:
API link joi
joi.array().items(joi.string().valid('required-string').required(), joi.string())
source to share