hapijs / hapijs/joi

How to validate all items in array have the same type

Open
#2,429 0 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

* *node version*: 12.14.1
* *module version*: 11.4.0
* *environment* node
* *used with* hapi application

My array can have multiple types:
`[Joi.string(), Joi.number(), Joi.boolean(), Joi.array(), Joi.object()]`

But I want to allow only one of them in the same time.

I tried the following:

`attributes: Joi.object().pattern(/\w+/, Joi.alternatives().try([Joi.array().items(Joi.string()), Joi.array().items(Joi.number()), Joi.array().items(Joi.boolean()), Joi.array().items(Joi.array()), Joi.array().items(Joi.object())]))`

Is there any way to perform such a validation? Does it work in another vesrion?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.