Skip to content
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

fix: support unqualified (local) datetime strings #2913

Closed
wants to merge 2 commits into from

Conversation

0xturner
Copy link

@0xturner 0xturner commented Oct 27, 2023

FIxes #2385

Adds support for local unqualified date time strings. (the ones that don't end in "Z")

ISO-8601 allows unqualified date time strings, in which case the time is assumed to be in local time rather than UTC. (See)

To prevent breaking changes, this is an an additional opt-in option to the existing string().dateTime() method.

const datetime = z.string().datetime();
datetime.parse("2020-01-01T00:00:00"); // fail, same as before, requires trailing "z"

const datetimeUnqualified = z.string().datetime({ unqualified: true });
datetime.parse("2020-01-01T00:00:00"); // pass

I realized shortly after creating this PR that there is a similar effort at #2522. I've taken a slightly different approach in this PR. I am not sure what the etiquette is in terms of duplicate PRs, happy to collaborate and merge efforts with #2522 if that is possible/preferred.

@netlify
Copy link

netlify bot commented Oct 27, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 7cff53a
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/653c880faeb4b50008fce1e2
😎 Deploy Preview https://deploy-preview-2913--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@0xturner 0xturner marked this pull request as draft October 28, 2023 03:24
@0xturner 0xturner changed the title fix: support unqualified datetime strings fix: support unqualified (non-utc) datetime strings Oct 28, 2023
@0xturner 0xturner changed the title fix: support unqualified (non-utc) datetime strings fix: support unqualified (local) datetime strings Oct 28, 2023
@@ -709,7 +709,7 @@ z.string().regex(regex);
z.string().includes(string);
z.string().startsWith(string);
z.string().endsWith(string);
z.string().datetime(); // ISO 8601; default is without UTC offset, see below for options
z.string().datetime(); // ISO 8601; default is UTC with zero offset, see below for options
Copy link
Author

@0xturner 0xturner Oct 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choosing more explicit wording here.

"Without UTC offset" is potentially misleading, as there is some nuance in the fact that "Z" is a zone indicator for the zero UTC offset, as opposed to the absence of an offset.

@@ -535,6 +535,7 @@ export type ZodStringCheck =
kind: "datetime";
offset: boolean;
precision: number | null;
unqualified: boolean;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some other names I considered for this option, open to changing:

  • local
  • utc (defaults to true, set to false to allow unqualified dates)

@Lephilosophe237
Copy link

Lephilosophe237 commented Oct 31, 2023

Hi I got this issue with the datetime, do you know when this fix will be release please

@ahmed-abdelmotey
Copy link

any updates on when this is getting merged ?

@Irvenae
Copy link

Irvenae commented Jan 29, 2024

can we get this merged?

@ezze
Copy link

ezze commented Mar 29, 2024

Please, any feedback at least, guys, why it's not merged yet?

@ericdew
Copy link

ericdew commented Apr 4, 2024

Also interested in this change - would save a lot of headaches when working with forms with datetime inputs.

@colinhacks
Copy link
Owner

Thanks @0xturner — I ended up merging the other PR after modifying it to use an approach/API almost identical to yours 😅 Whoops. Great stuff here!

@colinhacks colinhacks closed this Apr 13, 2024
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

Successfully merging this pull request may close these issues.

Datetime validation without offset should allow omission of Z zone designator
8 participants