8000 datetime validation fails if seconds are missing · Issue #3636 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

datetime validation fails if seconds are missing #3636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
u2ix opened this issue Jul 11, 2024 · 11 comments
Closed

datetime validation fails if seconds are missing #3636

u2ix opened this issue Jul 11, 2024 · 11 comments

Comments

@u2ix
Copy link
u2ix commented Jul 11, 2024

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
Copy link

I have the same issue.

@itsthekeming
Copy link

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

@anthonyhagi
Copy link

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

@Dino-Kupinic
Copy link

An option to toggle between HH:MM and HH:MM:SS would be nice

@fvilers
Copy link
fvilers commented Feb 7, 2025

At first I was open to write a PR to implement it but the documentation specifies that the .time() function validates against the ISO time format so having an option to omit the seconds is not ideal here. You can always use a regex to validate the string for a valid time expression with optional seconds. Here's mine: /^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/.

@u2ix
Copy link
Author
u2ix commented Feb 7, 2025

@fvilers But a ISO time format is also valid without the seconds, and it's the way that browsers return the date in a valid ISO format, but the validation in the library tells us it's wrong. Going for a regex here, instead of fixing a obvious bug in this library, that many people have (see all the up-votes), seems to be kinda hack or a workaround, but not solving the problem.

@fvilers
Copy link
fvilers commented Feb 7, 2025

@u2ix As I'm reading https://en.wikipedia.org/wiki/ISO_8601#Times you might be right. It looks like the seconds could be omitted. I might create a PR to add this feature (an approval from the maintainer(s) would be great). Using a regex is not hacky as it's what is used behind the scenes by the time() validator. And as I see things, there is no bug in the current implementation as the documentation clearly states that the seconds are mandatory (z.string().time(); // ISO time format (HH:mm:ss[.SSSSSS]))

@ronnyandre
Copy link

Pleeease fix this annoying issue (not bug).. I think a modern library should handle without seconds.

@stevemk42
Copy link

Same here. input with datetime-local type fails zod validation.

For my use case, I had to break down my input in 2 parts: time and date.

Waiting for some update here. Thanks ahead

@timorthi
Copy link
Contributor
timorthi commented May 3, 2025

Hi all, I was also running into the same issue, so I've submitted a PR for this change (#4315).

@colinhacks
Copy link
Owner

Landed in 3.24.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants
0