Search code examples
javascriptformikyup

Yup Date Validation with Formik - Cannot Be Today nor a Historic Date


Use Case: Ensure the job posting start date is not today, nor a date in the past.

My code is:

const validationSchema = yup.object({
    startDate: yup.string().required("Please complete this field.").nullable(),
})

Question: How do I ensure it is neither today, nor a historic date?

Side note, I am using formik, with the validationSchema used as below:

  const formik = useFormik({
    initialValues,
    enableReinitialize: true,
    validationSchema: validationSchema,
    onSubmit,
  });

Solution

  • Solution is as follows:

    const validationSchema = yup.object({
        startDate: yup
          .date()
          .min(new Date(), "ERROR MESSAGE")
          .required("Please complete this field.")
          .nullable(),
    })