最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Joi multiple when condition - Stack Overflow

matteradmin7PV0评论

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()
   })
Post a comment

comment list (0)

  1. No comments so far