Reputation: 3105
I'm looking for a way to validate that an array contains a required value using joi.
Found these issues online - #1, #2, but none of them has a definitive answer.
I tried several stuff but they doesn't seem to work, like:
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'm trying to achieve:
Accepted:
['required-string'], ['required-string', 'other'], ['other','required-string'], ['other',...,'required-string',....,'more-other']
Denied:
[], ['other'], [null], etc..
Upvotes: 6
Views: 4847
Reputation: 878
You can use array.items
by listing all allowed types. If a given type is .required()
then there must be a matching item in the array:
joi API reference
joi.array().items(joi.string().valid('required-string').required(), joi.string())
Upvotes: 3