title: Joi validator conditional schema
body: <p>I need to create dynamic schema to validate my api request query in <code>node js</code> using <a href="https://github.com/hapijs" rel="noreferrer">Joi validator</a> depending on a key in the request query. Say the following below mentioned pattern are my valid queries.</p>
  
  <p>I'm using <code>hapi/joi</code> version <code>16.1.8</code></p>
  
  <p><strong>Combination 1</strong></p>
  
  <pre><code>{ type: 1, firstname: 'user first name', lastname: 'user last name'}
  </code></pre>
  
  <p><strong>Combination 2</strong></p>
  
  <pre><code>{ type: 2 , salary: 1000, pension: 200}
  </code></pre>
  
  <p><strong>Combination 3</strong></p>
  
  <pre><code>{ type: 3 , credit: 550, debit: 100}
  </code></pre>
  
  <p>As you can see the object keys varies depending on the value of <code>type</code>. How this can be handled properly?</p>
  
  <p>We can handle two conditions using <a href="https://github.com/hapijs/joi/blob/master/API.md#alternativesconditionalcondition-options" rel="noreferrer">Joi.alternatives</a> like</p>
  
  <pre><code>const schema = Joi.alternatives().conditional(Joi.object({ type: 1 }).unknown(), {
      then: Joi.object({
          type: Joi.string(),
          firstname: Joi.string(),
          lastname: Joi.string()
      }),
      otherwise: Joi.object({
          type: Joi.number(),
          salary: Joi.any(),
          pension: Joi.any()
      })
  });
  </code></pre>
  
  <p>But how this can be done for 3 conditions?</p>