title: Yup: How to validate two dates which are dependent on each other?
body: <p>I am using <code>Formik</code> and <code>yup</code> for its validation.<br>
  I have the following requirements for validation of two fields <code>started</code> and <code>ended</code></p>
  
  <ol>
  <li><code>started</code> must be a valid date.</li>
  <li><code>started</code> must be a past date.</li>
  <li><code>ended</code> can be null, means it is not required.</li>
  <li><code>ended</code> must not be null and must be a valid date if the <code>started</code>
  has a valid date value.</li>
  </ol>
  
  <p>This is my validation schema:</p>
  
  <pre><code>validationSchema={yup.object().shape({
      started: yup.date().max(new Date(), "Future date not allowed").typeError("Invalid Started date"),
      ended: yup.date().default(null).when("started", {
          is: yup.date().isValid(),
          then: yup.date().min(yup.ref("started"), "Ended date must be later than Start date"),
          otherwise: yup.date().nullable()
      }).typeError("Invalid Ended date")
  })}
  </code></pre>
  
  <p>1,2,3 are working, but the 4th validation is not working.<br>
  I also tried changing it to <code>is: started =&gt; started !==null</code>, this is completely wrong because I <code>console.log(started)</code> and that printed me <code>Invalid date</code>.<br>
  I even tried <code>is: true</code> that also didn't work.</p>
  
  <p>What would be the correct way of validating this requirement?</p>