Z
Zod7mo ago
Luan Mário

Luan Mário - how to make an input of type date ...

how to make an input of type date optional?
5 Replies
Svish
Svish7mo ago
z.date().optional()
z.date().optional()
Luan Mário
Luan MárioOP7mo ago
does not work return invalid date error when submitting the form I'm using z.coerce.date
Svish
Svish7mo ago
Well, coerce runs whatever it gets through new Date, and if you pass undefined or null through that, you'll probably find you get an invalid date. Personally I would really avoid using coerce, as it makes the schema a bit weird and unpredictable. Much better to just make sure the data-types you hand to the schema are correct to begin with. In the case of a form, write it so that the form state contains strings, numbers, and dates as you'd expect. Not "whatever the form feels like".
Luan Mário
Luan MárioOP7mo ago
It is a problem that there is an entry of type date. in the api query for the editing screen, data in the format 2007-06-24T00:00:00 may or may not appear
Svish
Svish7mo ago
Then coerce is not the right thing to use. It's specifically string you want to convert to Date, so then it's probably safer to use z.preprocess, or something like that.
Want results from more Discord servers?
Add your server