title: Joi nested `when` conditions failed in schema definition with `ERR_ASSERTION ` error
body: <p>I have been trying to work with Joi (old version 14.3.1) to create a schema and unfortunately, I am stuck. I have tried multiple ways to define schemas but results always come with an error in the schema definition.</p>
  <p>Here is my schema definition:</p>
  <pre><code> const allowedUser = {
      admin: 'ADMIN',
      normal: 'NORMAL'
    };
  
    const schema = validator
      .object()
      .keys({
        user: validator.any().valid(Object.values(allowedUser)).required(),
        context: validator.object().default({})
      })
      .when('user', {
        is: allowedUser.admin,
        then: validator.when(validator.object({ context: validator.exist() }), {
          then: validator.object().keys({
            session: validator.string().required()
          })
        })
      })
      .when('user', {
        is: allowedUser.normal,
        then: validator.when(validator.object({ context: validator.exist() }), {
          then: validator.object().keys({
            id: validator.string().required()
          })
        })
      });
  </code></pre>
  <p>Another schema definition that i tried:</p>
  <pre><code>  const schema = validator.object().keys({
      user: validator.any().valid(Object.values(allowedUser)).required(),
      context: validator
        .object()
        .when('user', {
          is: allowedUser.admin,
          then: validator.when('context', {
            is: validator.exist(),
            then: validator.object().keys({
              session: validator.string().required()
            })
          })
        })
        .when('user', {
          is: allowedUser.normal,
          then: validator.when('context', {
            is: validator.exist(),
            then: validator.object().keys({
              id: validator.string().required()
            })
          })
        })
        .default({})
    });
  </code></pre>
  <p>So conditions for schema are as follows:</p>
  <ol>
  <li>In the case of <code>user</code> type <code>ADMIN</code>: if  <code>context</code> property is defined it should contain property <code>session</code>. if <code>context</code> property is undefined it should set the default to <code>{}</code></li>
  <li>In the case of <code>user</code> type <code>NORMAL</code>: if  <code>context</code> property is defined it should contain property <code>id</code>. if <code>context</code> property is undefined it should set value default to <code>{}</code></li>
  <li>Implicit case is if <code>context</code> property contains unknown property it should fail too</li>
  </ol>
  <p>When I run the code I always get an error in schema definition which is as follows:</p>
  <pre><code>AssertionError [ERR_ASSERTION]: Cannot merge type object with another type: alternatives
      
  </code></pre>
  <p>My minimal example code is as follows:</p>
  <pre><code>const validator = require('joi');
  
  
    const allowedUser = {
      admin: 'ADMIN',
      normal: 'NORMAL'
    };
  
    const bodyWhenUndefined = {
      user: 'ADMIN',
      message: 'Hi'
    };
  
    const bodyWhenValidDefined = {
      user: 'ADMIN',
      message: 'Hi'
    };
  
  
    const bodyWhenDefinedEmpty = {
      user: 'ADMIN',
      message: 'Hi 2',
      context: {}
    };
  
    const bodyWhenDefinedWrong = {
      user: 'admin',
      message: 'Hi 3',
      context: {abc: 'wrong property'}
    };
  
    const schema = validator
      .object()
      .keys({
        user: validator.any().valid(Object.values(allowedUser)).required(),
        context: validator.object().default({})
      })
      .when('user', {
        is: allowedUser.admin,
        then: validator.when(validator.object({ context: validator.exist() }), {
          then: validator.object().keys({
            session: validator.string().required()
          })
        })
      })
      .when('user', {
        is: allowedUser.normal,
        then: validator.when(validator.object({ context: validator.exist() }), {
          then: validator.object().keys({
            id: validator.string().required()
          })
        })
      });
  
  
     const resultSuccess = validator.validate(bodyWhenValidDefined, schema);
      console.log(&quot;This is with success in validation&quot;,resultSuccess.value);
  
      const result = validator.validate(bodyWhenUndefined, schema);
      console.log(&quot;This is with default context property&quot;,result.value);
  
  
      const resultWithEmptyError = validator.validate(bodyWhenDefinedEmpty, schema);
      console.log(&quot;This should come with error with required property session&quot;,resultWithEmptyError.error);
  
      const resultWithWrongProp = validator.validate(bodyWhenDefinedWrong, schema);
      console.log(&quot;This should come with error with required property session&quot;,resultWithWrongProp.error);
  
  
  </code></pre>
  <p>I have created a working REPL fiddle <a href="https://repl.it/join/qvvkcdli-fahimmahmoodmir" rel="nofollow noreferrer">here</a> Help would be appreciated to tell me where I am doing wrong.</p>