colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.91k stars 1.18k forks source link

datetime validation fails if seconds are missing #3636

Open u2ix opened 4 months ago

u2ix commented 4 months ago

Have an issue with a valid datetime value, as produced by the browser in a datetime picker field, is not accepted by the datetime() validation. After some digging I figured that when the seconds :00 are added the validation passes.

But according to ISO 8601, just the hour is required for a valid time. Also as the timestamp is produced like this "2024-07-18T11:00", I feel it would be really great if this is accepted by the library as also the Date constructor doesn't have any issue with this.

For now I had to put in a quite ugly preprocess to make the validation pass on the value provided by the browser:

z.preprocess(input => `${input}:00`,
            z.string().datetime({ local: true }))
sachix1001 commented 2 months ago

I have the same issue.

itsthekeming commented 2 months ago

Likewise. I'm using js-joda to handle dates, and its LocalDateTime.toString() implementation follows the spec.

anthonyhagi commented 2 months ago

I've noticed the same issue with z.string().time() where it requires HH:MM:SS to actually validate. The <input type='time' /> input by default uses HH:MM as mentioned in the MDN docs

Hopefully this can be fixed sometime soon