1

我正在尝试对 url 使用数组值。所以我把它作为 Joi 验证。

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

如果我这样做,效果很好

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

它被entity视为一个数组,所以当我从request

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

但是,如果我这样做

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

我得到entity字符串而不是['person']

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

我想要的是我希望这个网址http://something.com/query?entity=personentity视为['person']

4

1 回答 1

3

.allow()列出数组的有效值entity,但您想指定数组中项的类型:

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

从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' ] } }
于 2017-02-07T21:50:07.780 回答