Yup validation transforms date string to datetime

See original GitHub issue

Version Number

V7

Codesandbox/Expo snack

https://codesandbox.io/s/react-hook-form-v7-yup-and-dates-p80hu

Steps to reproduce

Hello, First of all thanks for this wonderful library and the new V7. I have been experiencing an unexpected behavior when using react-hook-form with yup for date validation. This also happens when using v6 I believe.

  1. Go to sandbox
  2. Choose a date
  3. Click submit and check console
  4. Date field value in watch is a string of type yyyy-mm-dd
  5. However data from on submit is of type Date (basically a timestamp)

Expected behaviour

I would expect form data not not to be transformed by yup resolver, meaning formState = data passed in on submit. I assume what’s happening is that Yup parse the date string into a Date Object and that is the element returned

What browsers are you seeing the problem on?

No response

Relevant log output

submited data =  Thu Feb 03 2022 00:00:00 GMT+0100 (heure normale d’Europe centrale)
form Data =  2022-02-03 

Code of Conduct

  • I agree to follow this project’s Code of Conduct

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
Moshyfawncommented, Feb 3, 2022

I wouldn’t consider making a change at the moment

What change are we talking about? Yup transforms the values to “help with validation” which is very subjective and totally different from, say, Zod’s paradigm, where they sure allow you to preprocess the value before validating, but they don’t mutate the actual value. It’s not like it’s RHF job to bring everyone “up to standard”, as it all depends on the validation lib ppl choose for the way they handle the data (transform the data or not in case of Yup and Zod). If RHF takes the responsibility to update (or not) the validated data, there gonna be tons of ppl with different libs creating issues on how the behavior of RHF doesn’t match their lib.

To sum it up, IMO, if you use Yup, you can expect the values to be “mutated” during the validation, as Yup does exactly that. If you use, say, Zod, it’s their paradigm that you chose and don’t expect the values to be mutated during the validation

2reactions
chrisbagcommented, Feb 3, 2022

Thanks for your views that I completely understand and it makes senses. I hope this will help out some react-hook-form/ yup users stumbling into the same issue. I was surprised that one would potentially expect the validation library to mutate data. I’ll probably just make my custom resolver to solve this issue 😁 thanks for your time

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup.date() convert date into string without timezone after ...
Use Yup.string() · Convert to date · Perform the necessary validation · Reconvert date to string · And return string.
Read more >
Yup Date Validation with Custom Transform - Code Daily
If the way you're storing your date value doesn't work when passing to new Date() then you'll need to setup a custom transform....
Read more >
Transform date after validation #1153 - jquense/yup - GitHub
i use yup to validate my dates but i need the final result to be in ISOString(). Everytime , yup transforms the iso...
Read more >
How to use the yup.date function in yup - Snyk
To help you get started, we've selected a few yup.date examples, based on popular ways it is used in public projects.
Read more >
Yup Validation for Date and Time With MomentJS - Bionic Julia
Looking at start_date , I want three separate validation rules - (i) that the value takes the form of a Date object; (ii)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found