admin管理员组文章数量:1026989
I want to do a validation with Joi in my body, but it seems never work and fall all the time in the same condition. So if i POST with this
endPoint: /elasticSearch?eType=scroll&scroll=1h
Body:{}
that supposed to throw an error, because eType is scroll and in this case scroll_id need to be required,not null, not empty.
even when i POST with this
endPoint: /elasticSearch?eType=search&scroll=1h
Body:{}
that supposed to throw an error, because eType is search and in this case query need to be required.
so with these codes,
in one case it just always pass like if it's had no validation even if they should not pass in my opinion and in the second case, i got error: query is required, and scroll_id is required all the time when i make a call.
so someone can help me to understand why these validation are wrong ?
Thanks
Update
By default, if i do that like this:
body:
{
query:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('search'),
then: Joi.required()
}
),
scroll_id:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('scroll'),
then: Joi.required()
}
)
}
That required query and scroll_id all time.
I want to do a validation with Joi in my body, but it seems never work and fall all the time in the same condition. So if i POST with this
endPoint: /elasticSearch?eType=scroll&scroll=1h
Body:{}
that supposed to throw an error, because eType is scroll and in this case scroll_id need to be required,not null, not empty.
even when i POST with this
endPoint: /elasticSearch?eType=search&scroll=1h
Body:{}
that supposed to throw an error, because eType is search and in this case query need to be required.
so with these codes,
in one case it just always pass like if it's had no validation even if they should not pass in my opinion and in the second case, i got error: query is required, and scroll_id is required all the time when i make a call.
so someone can help me to understand why these validation are wrong ?
Thanks
Update
By default, if i do that like this:
body:
{
query:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('search'),
then: Joi.required()
}
),
scroll_id:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('scroll'),
then: Joi.required()
}
)
}
That required query and scroll_id all time.
Share Improve this question edited Feb 4, 2019 at 17:58 samuel cote asked Jan 31, 2019 at 19:02 samuel cotesamuel cote 1033 silver badges7 bronze badges1 Answer
Reset to default 5Directly copied from documentation.
When using a Joi validation object, the values of the other inputs (i.e. headers, query, params, payload, and auth) are made available under the validation context (accessible in rules as Joi.ref('$query.key')).
So, use Joi.ref('$query.eType')
in your eType references, because you are trying to validate payload according to query parameters, in the validation phase, they are in separate scopes.
Joi.alternatives()
.when(Joi.ref('$query.eType'), {
is: Joi.string().equal('search'),
then: Joi.required()
})
I want to do a validation with Joi in my body, but it seems never work and fall all the time in the same condition. So if i POST with this
endPoint: /elasticSearch?eType=scroll&scroll=1h
Body:{}
that supposed to throw an error, because eType is scroll and in this case scroll_id need to be required,not null, not empty.
even when i POST with this
endPoint: /elasticSearch?eType=search&scroll=1h
Body:{}
that supposed to throw an error, because eType is search and in this case query need to be required.
so with these codes,
in one case it just always pass like if it's had no validation even if they should not pass in my opinion and in the second case, i got error: query is required, and scroll_id is required all the time when i make a call.
so someone can help me to understand why these validation are wrong ?
Thanks
Update
By default, if i do that like this:
body:
{
query:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('search'),
then: Joi.required()
}
),
scroll_id:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('scroll'),
then: Joi.required()
}
)
}
That required query and scroll_id all time.
I want to do a validation with Joi in my body, but it seems never work and fall all the time in the same condition. So if i POST with this
endPoint: /elasticSearch?eType=scroll&scroll=1h
Body:{}
that supposed to throw an error, because eType is scroll and in this case scroll_id need to be required,not null, not empty.
even when i POST with this
endPoint: /elasticSearch?eType=search&scroll=1h
Body:{}
that supposed to throw an error, because eType is search and in this case query need to be required.
so with these codes,
in one case it just always pass like if it's had no validation even if they should not pass in my opinion and in the second case, i got error: query is required, and scroll_id is required all the time when i make a call.
so someone can help me to understand why these validation are wrong ?
Thanks
Update
By default, if i do that like this:
body:
{
query:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('search'),
then: Joi.required()
}
),
scroll_id:
Joi.alternatives()
.when(Joi.ref('$query.eType'),
{
is: Joi.string().equal('scroll'),
then: Joi.required()
}
)
}
That required query and scroll_id all time.
Share Improve this question edited Feb 4, 2019 at 17:58 samuel cote asked Jan 31, 2019 at 19:02 samuel cotesamuel cote 1033 silver badges7 bronze badges1 Answer
Reset to default 5Directly copied from documentation.
When using a Joi validation object, the values of the other inputs (i.e. headers, query, params, payload, and auth) are made available under the validation context (accessible in rules as Joi.ref('$query.key')).
So, use Joi.ref('$query.eType')
in your eType references, because you are trying to validate payload according to query parameters, in the validation phase, they are in separate scopes.
Joi.alternatives()
.when(Joi.ref('$query.eType'), {
is: Joi.string().equal('search'),
then: Joi.required()
})
本文标签: javascriptJoi multiple when conditionStack Overflow
版权声明:本文标题:javascript - Joi multiple when condition - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660766a2161876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论