forge42dev / remix-hook-form

Open source wrapper for react-hook-form aimed at Remix.run
MIT License
330 stars 27 forks source link

feat: added handling for date in createFormData #67

Closed TheYoxy closed 7 months ago

TheYoxy commented 8 months ago

Description

As is, when serializing a date as JSON, it add annoying quotes in the output, which leads to error when coupling with zod.

const date = new Date(2023, 1, 1);
console.log(JSON.stringify(date), date.toISOString(), JSON.stringify(date) === date.toISOString());
// "2023-01-31T23:00:00.000Z" 2023-01-31T23:00:00.000Z false

I've added a case when not "stringify" form data while using createFormData method to use the toISOString() method instead of the JSON.stringify.

Type of change

Please delete options that are not relevant.

How Has This Been Tested?

All the tests has been added inside the pr to match the issue encountered.

Checklist:

AlemTuzlak commented 7 months ago

@TheYoxy thank you for this, wondering if this could potentially break anything for existing implementations, I'd assume not

TheYoxy commented 7 months ago

I'd assume not too, as I feel that it causes more problems to keep in this state and the user should patch it if it ever want to keep the date object instead of getting the string value.