jquense / yup

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

Invalid Date validation #1919

Open oliviercperrier opened 1 year ago

oliviercperrier commented 1 year ago

Describe the bug

The yup.date() schema is returning these date are valid:

To Reproduce

https://codesandbox.io/s/young-star-ke5v6k

Expected behavior

This date should not be valid and the schema validation should return an error.

Platform (please complete the following information):

archit-vitaccess commented 1 year ago

Same is for _-1-01

chawes13 commented 1 year ago

There are a lot of wonky edge cases here, which seem to be related to how the native Date constructor handles parsing strings. For example, even a "well formed" date like "12-22-0088" is coerced to Thu Dec 22 1988 when passed to Date.

As a workaround, we overrode the default transform and parsed the date with date-fns: https://codesandbox.io/s/yup-playground-forked-2-t0j5vd?file=/src/index.js. Ideally we'd apply this globally to all date schemas, but I didn't spend much time investigating if that was possible.

TeChn4K commented 1 year ago

I can confirm the "well formed" exemple from @chawes13 :

My workaround

yup
  .date()
  .transform((_, originalValue) => new Date(originalValue))
  ....