Comparing version 3.23.0-canary.20240417T010051 to 3.23.0-canary.20240417T235146
@@ -7,48 +7,49 @@ "use strict"; | ||
const benchmark_1 = __importDefault(require("benchmark")); | ||
const index_1 = require("../index"); | ||
const datetimeValidation = new benchmark_1.default.Suite("datetime"); | ||
const DATA = "2020-01-01T00:00:00Z"; | ||
const datetimeRegex = index_1.z.datetimeRegex({}); | ||
console.log(datetimeRegex); | ||
const datetimeValidationSuite = new benchmark_1.default.Suite("datetime"); | ||
const DATA = "2021-01-01"; | ||
const MONTHS_31 = new Set([1, 3, 5, 7, 8, 10, 12]); | ||
const MONTHS_30 = new Set([4, 6, 9, 11]); | ||
datetimeValidation | ||
const simpleDatetimeRegex = /^(\d{4})-(\d{2})-(\d{2})$/; | ||
const datetimeRegexNoLeapYearValidation = /^\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\d|2\d))$/; | ||
const datetimeRegexWithLeapYearValidation = /^((\d\d[2468][048]|\d\d[13579][26]|\d\d0[48]|[02468][048]00|[13579][26]00)-02-29|\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\d|3[01])|(0[469]|11)-(0[1-9]|[12]\d|30)|(02)-(0[1-9]|1\d|2[0-8])))$/; | ||
datetimeValidationSuite | ||
.add("new Date()", () => { | ||
const d = new Date(DATA); | ||
return !isNaN(d.getTime()); | ||
return !isNaN(new Date(DATA).getTime()); | ||
}) | ||
.add("regex validation", () => { | ||
return datetimeRegex.test(DATA); | ||
.add("regex (no validation)", () => { | ||
return simpleDatetimeRegex.test(DATA); | ||
}) | ||
.add("simple regex with validation", () => { | ||
// Regex to validate ISO 8601 format with 'Z' timezone | ||
const regex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/; | ||
const match = DATA.match(regex); | ||
if (!match) { | ||
.add("regex (no leap year)", () => { | ||
return datetimeRegexNoLeapYearValidation.test(DATA); | ||
}) | ||
.add("regex (w/ leap year)", () => { | ||
return datetimeRegexWithLeapYearValidation.test(DATA); | ||
}) | ||
.add("capture groups + code", () => { | ||
const match = DATA.match(simpleDatetimeRegex); | ||
if (!match) | ||
return false; | ||
} | ||
// Extract year, month, and day from the capture groups | ||
const year = parseInt(match[1], 10); | ||
const month = parseInt(match[2], 10); // month is 0-indexed in JavaScript Date, so subtract 1 | ||
const day = parseInt(match[3], 10); | ||
if (month == 2) { | ||
const year = Number.parseInt(match[1], 10); | ||
const month = Number.parseInt(match[2], 10); // month is 0-indexed in JavaScript Date, so subtract 1 | ||
const day = Number.parseInt(match[3], 10); | ||
if (month === 2) { | ||
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) { | ||
if (month === 2 && day > 29) { | ||
return false; | ||
} | ||
return day <= 29; | ||
} | ||
return day <= 28; | ||
} | ||
if (MONTHS_30.has(month) && day > 30) { | ||
return false; | ||
if (MONTHS_30.has(month)) { | ||
return day <= 30; | ||
} | ||
if (MONTHS_31.has(month) && day > 31) { | ||
return false; | ||
if (MONTHS_31.has(month)) { | ||
return day <= 31; | ||
} | ||
return true; | ||
return false; | ||
}) | ||
.on("cycle", (e) => { | ||
console.log(`${datetimeValidation.name}: ${e.target}`); | ||
console.log(`${datetimeValidationSuite.name}: ${e.target}`); | ||
}); | ||
exports.default = { | ||
suites: [datetimeValidation], | ||
suites: [datetimeValidationSuite], | ||
}; |
@@ -176,2 +176,5 @@ import { enumUtil } from "./helpers/enumUtil"; | ||
} | { | ||
kind: "duration"; | ||
message?: string; | ||
} | { | ||
kind: "ip"; | ||
@@ -222,2 +225,3 @@ version?: IpVersion; | ||
}): ZodString; | ||
duration(message?: errorUtil.ErrMessage): ZodString; | ||
regex(regex: RegExp, message?: errorUtil.ErrMessage): ZodString; | ||
@@ -244,2 +248,3 @@ includes(value: string, options?: { | ||
get isTime(): boolean; | ||
get isDuration(): boolean; | ||
get isEmail(): boolean; | ||
@@ -246,0 +251,0 @@ get isURL(): boolean; |
@@ -73,3 +73,3 @@ import type { TypeOf, ZodType } from "."; | ||
} | ||
export declare type StringValidation = "email" | "url" | "emoji" | "uuid" | "nanoid" | "regex" | "cuid" | "cuid2" | "ulid" | "datetime" | "date" | "time" | "ip" | "base64" | { | ||
export declare type StringValidation = "email" | "url" | "emoji" | "uuid" | "nanoid" | "regex" | "cuid" | "cuid2" | "ulid" | "datetime" | "date" | "time" | "duration" | "ip" | "base64" | { | ||
includes: string; | ||
@@ -76,0 +76,0 @@ position?: number; |
{ | ||
"name": "zod", | ||
"version": "3.23.0-canary.20240417T010051", | ||
"version": "3.23.0-canary.20240417T235146", | ||
"author": "Colin McDonnell <colin@colinhacks.com>", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
671277
15100
3081