2017-02-03 5 views
0

URLに配列値を使用しようとしています。だから私はジョイの検証としてこれを持っています。Joi for Hapiは、配列の1つの要素を配列として変換しません。

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

それは私がこの

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

をすれば、私はこの

を行う場合、私は、しかし request

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

から値をプリントアウトしたときにそれはとても配列としてentityを見て正常に動作します

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

私はentityためhttp://something.com/query?entity=person['person']

+0

http://something.com/query?entity=['person '] – Red

答えて

2

.allow()リストentity配列のための有効な値として見られるように、このURLを望んで欲しい代わりに['person']

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

の文字列として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' ] } } 
関連する問題