1

I'm trying to use array value for the url. So I have this as Joi validation.

entity: Joi.array().allow(['person','location','organization']).unique().single().default(['person'])

It works fine if I do this

http://something.com/query?entity=person&person=organization

It sees entity as an array so when I print out value from request

console.log(request.query.entity) // ['person', 'organization']

However, if I do this

http://something.com/query?entity=person

I get entity as string instead of ['person']

console.log(request.query.entity) // 'person'

What I want is I want this url http://something.com/query?entity=person for entity to be seen as ['person']

simon-p-r
  • 3,623
  • 2
  • 20
  • 35
toy
  • 11,711
  • 24
  • 93
  • 176

1 Answers1

4

.allow() lists the valid values for the entity array, but you want to specify the type of the items in the array:

entity: Joi.array().unique().single().items(Joi.string().valid(['person','location','organization'])).default(['person'])

From the repl:

> schema = Joi.object({ entity: Joi.array().unique().single().items(Joi.string().valid(['person','location','organization'])).default(['person'])});
> Joi.validate({entity: 'person' }, schema)
{ error: null, value: { entity: [ 'person' ] } }
Gangstead
  • 4,152
  • 22
  • 35