jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

Uncaught (in promise) RangeError on an invalid date #2221

Closed khk1234 closed 5 months ago

khk1234 commented 5 months ago

Describe the bug When i'm entering an invalid date like 12.02.YYYY in an input field (I"m using @mui/x-date-pickers) It throws an error:

Uncaught (in promise) RangeError: Invalid time value".

The issue is that yup is not catching this error properly. When I define a Yup.date() schema with a typeError message like

Yup.date().typeError('invalidDate')

Expected behavior The date type should catch RangeError exceptions thrown by date-fns v3 and convert them into a validation error.

khk1234 commented 5 months ago

The reason why Yup.date().typeError('invalidDate') doesn't catch invalid dates is that the RangeError is thrown when creating a Date object, which is before Yup's validation kicks in. This means that Yup's date() type doesn't have a chance to catch the error and this is more likely an issue of MUI v7