Friday, 2 July 2021

Yup date validation + 1 day

I have a form using formik / yup, and I'm validating dates against other dates in the form. I have a field for expirationDate that I want to be at a minimum the day after enteredDate, the day the form was entered.

I currently have this, which works but validates against the enteredDate-- the problem is that if you set 06/21/21 as the entered date, it accepts 06/21/21 as the expiration date-- I need it to set 06/22/21 as the minimum date, or enteredDate + 1 day.

yup.date('Expiration Date')
    .nullable()
    .min(yup.ref('enteredDate'),
        ({ min }) => `Expiration Date needs to be after Entered Date`
    )

I've tried many variations of

yup.date('Expiration Date')
    .nullable()
    .min(yup.ref('enteredDate') ? daysjs(yup.ref('enteredDate').add(1, 'day') : null,
        ({ min }) => `Expiration Date needs to be after Entered Date`
    )

but the yup.ref doesn't seem to return a date object, since the error is TypeError: The value of field could not be cast to a value that satisfies the schema type: "date".

How can I tell yup to use the next day?



from Yup date validation + 1 day

No comments:

Post a Comment