javascript - Joi multiple when condition - Stack Overflow

I want to do a validation with Joi in my body, but it seems never work and fall all the time in the sam

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 badges
Add a ment  | 

1 Answer 1

Reset to default 5

Directly 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()
   })

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745660767a4638822.html

相关推荐

  • javascript - Joi multiple when condition - Stack Overflow

    I want to do a validation with Joi in my body, but it seems never work and fall all the time in the sam

    22天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信