title: How can I run a custom validation against a different key in my schema?
body: <p>I'm using Joi to validate user-submitted form data. The data contains bank details: the relevant ones here are <code>accountNumber</code> and <code>sortCode</code>. I need to take the value of both inputs and run them through an external API to confirm they are valid together.</p>
  
  <p>I'm struggling though to write a custom Joi extension which can get the value of another field in the data. Here's a simplified version of what I've tried:</p>
  
  <pre><code>const baseJoi = require('joi');
  
  const bankDetails = joi =&gt; {
      return {
          name: 'bankDetails',
          base: joi.number().required(),
          rules: [
              {
                  name: 'accountNumber',
                  params: {
                      number: joi.number().required()
                  },
                  validate(params, value, state, options) {
                      console.log(value); // returns 12-34-46 (sortCode)
                      console.log(params.number); // returns a [Function: ref] object
                      console.log(state.parent.accountNumber); // returns 88888888, but feels "wrong"
                  }
              }
          ]
      };
  };
  
  const Joi = baseJoi.extend([bankDetails]);
  
  const validateBankDetails = () =&gt; {
      return Joi.bankDetails().accountNumber(Joi.ref('accountNumber')).required();
  };
  
  const schema = Joi.object({
      accountNumber: Joi.number().required(),
      sortCode: validateBankDetails(),
  });
  </code></pre>
  
  <p>I know I can use <code>Joi.ref()</code> inside Joi's own validators (eg. something like <code>Joi.number().less(Joi.ref('max'))</code>) but here I need to take the <em>value</em> of that key (<code>accountNumber</code>) and pass it through to another function (along with the <code>sortCode</code> value). Is there a Joi-approved way of doing this? I can get the value I want from the <code>state</code> parameters but this feels a bit of an anti-pattern.</p>