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

Validate dates in .datetime() #2825

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@ You can create a Zod schema for any TypeScript type by using `z.custom()`. This

```ts
const px = z.custom<`${number}px`>((val) => {
return /^\d+px$/.test(val as string);
return typeof val === "string" ? /^\d+px$/.test(val) : false;
});

type px = z.infer<typeof px>; // `${number}px`
Expand Down
8 changes: 5 additions & 3 deletions deno/lib/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ test("datetime parsing", () => {
expect(() => datetime.parse("2020-10-14")).toThrow();
expect(() => datetime.parse("T18:45:12.123")).toThrow();
expect(() => datetime.parse("2020-10-14T17:42:29+00:00")).toThrow();
expect(() => datetime.parse("1234-12-12T12:12:99.123Z")).toThrow();
expect(() => datetime.parse("1234-12-12T12:99:12.123Z")).toThrow();
expect(() => datetime.parse("1234-12-12T99:12:12.123Z")).toThrow();
expect(() => datetime.parse("1234-12-99T12:12:12.123Z")).toThrow();
expect(() => datetime.parse("1234-99-12T12:12:12.123Z")).toThrow();

const datetimeNoMs = z.string().datetime({ precision: 0 });
datetimeNoMs.parse("1970-01-01T00:00:00Z");
Expand All @@ -435,7 +440,6 @@ test("datetime parsing", () => {
datetimeOffset.parse("2020-10-14T17:42:29+00:00");
datetimeOffset.parse("2020-10-14T17:42:29+03:15");
datetimeOffset.parse("2020-10-14T17:42:29+0315");
datetimeOffset.parse("2020-10-14T17:42:29+03");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These aren't parseable by new Date().

expect(() => datetimeOffset.parse("tuna")).toThrow();
expect(() => datetimeOffset.parse("2022-10-13T09:52:31.Z")).toThrow();

Expand All @@ -446,7 +450,6 @@ test("datetime parsing", () => {
datetimeOffsetNoMs.parse("2022-10-13T09:52:31Z");
datetimeOffsetNoMs.parse("2020-10-14T17:42:29+00:00");
datetimeOffsetNoMs.parse("2020-10-14T17:42:29+0000");
datetimeOffsetNoMs.parse("2020-10-14T17:42:29+00");
expect(() => datetimeOffsetNoMs.parse("tuna")).toThrow();
expect(() => datetimeOffsetNoMs.parse("1970-01-01T00:00:00.000Z")).toThrow();
expect(() => datetimeOffsetNoMs.parse("1970-01-01T00:00:00.Z")).toThrow();
Expand All @@ -459,7 +462,6 @@ test("datetime parsing", () => {
datetimeOffset4Ms.parse("1970-01-01T00:00:00.1234Z");
datetimeOffset4Ms.parse("2020-10-14T17:42:29.1234+00:00");
datetimeOffset4Ms.parse("2020-10-14T17:42:29.1234+0000");
datetimeOffset4Ms.parse("2020-10-14T17:42:29.1234+00");
expect(() => datetimeOffset4Ms.parse("tuna")).toThrow();
expect(() => datetimeOffset4Ms.parse("1970-01-01T00:00:00.123Z")).toThrow();
expect(() =>
Expand Down
7 changes: 6 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ const datetimeRegex = (args: { precision: number | null; offset: boolean }) => {
}
};

const isValidDate = (str: string) => {
const date = new Date(str);
return !isNaN(date.getTime());
};

function isValidIP(ip: string, version?: IpVersion) {
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
return true;
Expand Down Expand Up @@ -822,7 +827,7 @@ export class ZodString extends ZodType<string, ZodStringDef> {
} else if (check.kind === "datetime") {
const regex = datetimeRegex(check);

if (!regex.test(input.data)) {
if (!regex.test(input.data) || !isValidDate(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
Expand Down
8 changes: 5 additions & 3 deletions src/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ test("datetime parsing", () => {
expect(() => datetime.parse("2020-10-14")).toThrow();
expect(() => datetime.parse("T18:45:12.123")).toThrow();
expect(() => datetime.parse("2020-10-14T17:42:29+00:00")).toThrow();
expect(() => datetime.parse("1234-12-12T12:12:99.123Z")).toThrow();
expect(() => datetime.parse("1234-12-12T12:99:12.123Z")).toThrow();
expect(() => datetime.parse("1234-12-12T99:12:12.123Z")).toThrow();
expect(() => datetime.parse("1234-12-99T12:12:12.123Z")).toThrow();
expect(() => datetime.parse("1234-99-12T12:12:12.123Z")).toThrow();

const datetimeNoMs = z.string().datetime({ precision: 0 });
datetimeNoMs.parse("1970-01-01T00:00:00Z");
Expand All @@ -434,7 +439,6 @@ test("datetime parsing", () => {
datetimeOffset.parse("2020-10-14T17:42:29+00:00");
datetimeOffset.parse("2020-10-14T17:42:29+03:15");
datetimeOffset.parse("2020-10-14T17:42:29+0315");
datetimeOffset.parse("2020-10-14T17:42:29+03");
expect(() => datetimeOffset.parse("tuna")).toThrow();
expect(() => datetimeOffset.parse("2022-10-13T09:52:31.Z")).toThrow();

Expand All @@ -445,7 +449,6 @@ test("datetime parsing", () => {
datetimeOffsetNoMs.parse("2022-10-13T09:52:31Z");
datetimeOffsetNoMs.parse("2020-10-14T17:42:29+00:00");
datetimeOffsetNoMs.parse("2020-10-14T17:42:29+0000");
datetimeOffsetNoMs.parse("2020-10-14T17:42:29+00");
expect(() => datetimeOffsetNoMs.parse("tuna")).toThrow();
expect(() => datetimeOffsetNoMs.parse("1970-01-01T00:00:00.000Z")).toThrow();
expect(() => datetimeOffsetNoMs.parse("1970-01-01T00:00:00.Z")).toThrow();
Expand All @@ -458,7 +461,6 @@ test("datetime parsing", () => {
datetimeOffset4Ms.parse("1970-01-01T00:00:00.1234Z");
datetimeOffset4Ms.parse("2020-10-14T17:42:29.1234+00:00");
datetimeOffset4Ms.parse("2020-10-14T17:42:29.1234+0000");
datetimeOffset4Ms.parse("2020-10-14T17:42:29.1234+00");
expect(() => datetimeOffset4Ms.parse("tuna")).toThrow();
expect(() => datetimeOffset4Ms.parse("1970-01-01T00:00:00.123Z")).toThrow();
expect(() =>
Expand Down
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ const datetimeRegex = (args: { precision: number | null; offset: boolean }) => {
}
};

const isValidDate = (str: string) => {
const date = new Date(str);
return !isNaN(date.getTime());
};

function isValidIP(ip: string, version?: IpVersion) {
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
return true;
Expand Down Expand Up @@ -822,7 +827,7 @@ export class ZodString extends ZodType<string, ZodStringDef> {
} else if (check.kind === "datetime") {
const regex = datetimeRegex(check);

if (!regex.test(input.data)) {
if (!regex.test(input.data) || !isValidDate(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_string,
Expand Down
Loading