From 483d45dbc96fc9004a6392b3801fed1e65dbcee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivo=20Ilic=CC=81?= Date: Sun, 23 Apr 2023 20:11:50 -0400 Subject: [PATCH 1/3] Added support for nanoid --- README.md | 1 + deno/lib/README.md | 1 + deno/lib/ZodError.ts | 1 + deno/lib/__tests__/string.test.ts | 39 +++++++++++++++++++++++++++++++ deno/lib/types.ts | 24 +++++++++++++++++++ src/ZodError.ts | 1 + src/__tests__/string.test.ts | 39 +++++++++++++++++++++++++++++++ src/types.ts | 22 ++++++++++++++--- 8 files changed, 125 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 815960b70..23549160a 100644 --- a/README.md +++ b/README.md @@ -843,6 +843,7 @@ z.string().email(); z.string().url(); z.string().emoji(); z.string().uuid(); +z.string().nanoid(); z.string().cuid(); z.string().cuid2(); z.string().ulid(); diff --git a/deno/lib/README.md b/deno/lib/README.md index 8b60064fc..4793633a3 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -843,6 +843,7 @@ z.string().email(); z.string().url(); z.string().emoji(); z.string().uuid(); +z.string().nanoid(); z.string().cuid(); z.string().cuid2(); z.string().ulid(); diff --git a/deno/lib/ZodError.ts b/deno/lib/ZodError.ts index a30787cb2..7710d2ed3 100644 --- a/deno/lib/ZodError.ts +++ b/deno/lib/ZodError.ts @@ -93,6 +93,7 @@ export type StringValidation = | "url" | "emoji" | "uuid" + | "nanoid" | "regex" | "cuid" | "cuid2" diff --git a/deno/lib/__tests__/string.test.ts b/deno/lib/__tests__/string.test.ts index 36549036d..cafe9b76e 100644 --- a/deno/lib/__tests__/string.test.ts +++ b/deno/lib/__tests__/string.test.ts @@ -230,6 +230,29 @@ test("bad uuid", () => { } }); +test("nanoid", () => { + const nanoid = z.string().nanoid("custom error"); + nanoid.parse("lfNZluvAxMkf7Q8C5H-QS"); + nanoid.parse("mIU_4PJWikaU8fMbmkouz"); + nanoid.parse("Hb9ZUtUa2JDm_dD-47EGv"); + nanoid.parse("5Noocgv_8vQ9oPijj4ioQ"); + const result = nanoid.safeParse("Xq90uDyhddC53KsoASYJGX"); + expect(result.success).toEqual(false); + if (!result.success) { + expect(result.error.issues[0].message).toEqual("custom error"); + } +}); + +test("bad nanoid", () => { + const nanoid = z.string().nanoid("custom error"); + nanoid.parse("ySh_984wpDUu7IQRrLXAp"); + const result = nanoid.safeParse("invalid nanoid"); + expect(result.success).toEqual(false); + if (!result.success) { + expect(result.error.issues[0].message).toEqual("custom error"); + } +}); + test("cuid", () => { const cuid = z.string().cuid(); cuid.parse("ckopqwooh000001la8mbi2im9"); @@ -309,6 +332,7 @@ test("checks getters", () => { expect(z.string().email().isCUID).toEqual(false); expect(z.string().email().isCUID2).toEqual(false); expect(z.string().email().isUUID).toEqual(false); + expect(z.string().email().isNANOID).toEqual(false); expect(z.string().email().isIP).toEqual(false); expect(z.string().email().isULID).toEqual(false); @@ -317,6 +341,7 @@ test("checks getters", () => { expect(z.string().url().isCUID).toEqual(false); expect(z.string().url().isCUID2).toEqual(false); expect(z.string().url().isUUID).toEqual(false); + expect(z.string().url().isNANOID).toEqual(false); expect(z.string().url().isIP).toEqual(false); expect(z.string().url().isULID).toEqual(false); @@ -325,6 +350,7 @@ test("checks getters", () => { expect(z.string().cuid().isCUID).toEqual(true); expect(z.string().cuid().isCUID2).toEqual(false); expect(z.string().cuid().isUUID).toEqual(false); + expect(z.string().cuid().isNANOID).toEqual(false); expect(z.string().cuid().isIP).toEqual(false); expect(z.string().cuid().isULID).toEqual(false); @@ -333,6 +359,7 @@ test("checks getters", () => { expect(z.string().cuid2().isCUID).toEqual(false); expect(z.string().cuid2().isCUID2).toEqual(true); expect(z.string().cuid2().isUUID).toEqual(false); + expect(z.string().cuid2().isNANOID).toEqual(false); expect(z.string().cuid2().isIP).toEqual(false); expect(z.string().cuid2().isULID).toEqual(false); @@ -341,14 +368,25 @@ test("checks getters", () => { expect(z.string().uuid().isCUID).toEqual(false); expect(z.string().uuid().isCUID2).toEqual(false); expect(z.string().uuid().isUUID).toEqual(true); + expect(z.string().uuid().isNANOID).toEqual(false); expect(z.string().uuid().isIP).toEqual(false); expect(z.string().uuid().isULID).toEqual(false); + expect(z.string().nanoid().isEmail).toEqual(false); + expect(z.string().nanoid().isURL).toEqual(false); + expect(z.string().nanoid().isCUID).toEqual(false); + expect(z.string().nanoid().isCUID2).toEqual(false); + expect(z.string().nanoid().isUUID).toEqual(false); + expect(z.string().nanoid().isNANOID).toEqual(true); + expect(z.string().nanoid().isIP).toEqual(false); + expect(z.string().nanoid().isULID).toEqual(false); + expect(z.string().ip().isEmail).toEqual(false); expect(z.string().ip().isURL).toEqual(false); expect(z.string().ip().isCUID).toEqual(false); expect(z.string().ip().isCUID2).toEqual(false); expect(z.string().ip().isUUID).toEqual(false); + expect(z.string().ip().isNANOID).toEqual(false); expect(z.string().ip().isIP).toEqual(true); expect(z.string().ip().isULID).toEqual(false); @@ -357,6 +395,7 @@ test("checks getters", () => { expect(z.string().ulid().isCUID).toEqual(false); expect(z.string().ulid().isCUID2).toEqual(false); expect(z.string().ulid().isUUID).toEqual(false); + expect(z.string().ulid().isNANOID).toEqual(false); expect(z.string().ulid().isIP).toEqual(false); expect(z.string().ulid().isULID).toEqual(true); }); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index ecf94a7ff..bb01bc202 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -529,6 +529,7 @@ export type ZodStringCheck = | { kind: "url"; message?: string } | { kind: "emoji"; message?: string } | { kind: "uuid"; message?: string } + | { kind: "nanoid"; message?: string } | { kind: "cuid"; message?: string } | { kind: "includes"; value: string; position?: number; message?: string } | { kind: "cuid2"; message?: string } @@ -570,6 +571,7 @@ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/; // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i; +const nanoidRegex = /^[a-z0-9_-]{21}$/i; // from https://stackoverflow.com/a/46181/1550155 // old version: too slow, didn't support unicode // const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i; @@ -755,6 +757,16 @@ export class ZodString extends ZodType { }); status.dirty(); } + } else if (check.kind === "nanoid") { + if (!nanoidRegex.test(input.data)) { + ctx = this._getOrReturnCtx(input, ctx); + addIssueToContext(ctx, { + validation: "nanoid", + code: ZodIssueCode.invalid_string, + message: check.message, + }); + status.dirty(); + } } else if (check.kind === "cuid") { if (!cuidRegex.test(input.data)) { ctx = this._getOrReturnCtx(input, ctx); @@ -933,7 +945,13 @@ export class ZodString extends ZodType { uuid(message?: errorUtil.ErrMessage) { return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) }); } +<<<<<<< HEAD +======= + nanoid(message?: errorUtil.ErrMessage) { + return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) }); + } +>>>>>>> f2615c0 (Added support for nanoid) cuid(message?: errorUtil.ErrMessage) { return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) }); } @@ -1124,7 +1142,13 @@ export class ZodString extends ZodType { get isUUID() { return !!this._def.checks.find((ch) => ch.kind === "uuid"); } +<<<<<<< HEAD +======= + get isNANOID() { + return !!this._def.checks.find((ch) => ch.kind === "nanoid"); + } +>>>>>>> f2615c0 (Added support for nanoid) get isCUID() { return !!this._def.checks.find((ch) => ch.kind === "cuid"); } diff --git a/src/ZodError.ts b/src/ZodError.ts index 9dd22d713..202f84804 100644 --- a/src/ZodError.ts +++ b/src/ZodError.ts @@ -93,6 +93,7 @@ export type StringValidation = | "url" | "emoji" | "uuid" + | "nanoid" | "regex" | "cuid" | "cuid2" diff --git a/src/__tests__/string.test.ts b/src/__tests__/string.test.ts index 9063a8bf2..52d4bb843 100644 --- a/src/__tests__/string.test.ts +++ b/src/__tests__/string.test.ts @@ -229,6 +229,29 @@ test("bad uuid", () => { } }); +test("nanoid", () => { + const nanoid = z.string().nanoid("custom error"); + nanoid.parse("lfNZluvAxMkf7Q8C5H-QS"); + nanoid.parse("mIU_4PJWikaU8fMbmkouz"); + nanoid.parse("Hb9ZUtUa2JDm_dD-47EGv"); + nanoid.parse("5Noocgv_8vQ9oPijj4ioQ"); + const result = nanoid.safeParse("Xq90uDyhddC53KsoASYJGX"); + expect(result.success).toEqual(false); + if (!result.success) { + expect(result.error.issues[0].message).toEqual("custom error"); + } +}); + +test("bad nanoid", () => { + const nanoid = z.string().nanoid("custom error"); + nanoid.parse("ySh_984wpDUu7IQRrLXAp"); + const result = nanoid.safeParse("invalid nanoid"); + expect(result.success).toEqual(false); + if (!result.success) { + expect(result.error.issues[0].message).toEqual("custom error"); + } +}); + test("cuid", () => { const cuid = z.string().cuid(); cuid.parse("ckopqwooh000001la8mbi2im9"); @@ -308,6 +331,7 @@ test("checks getters", () => { expect(z.string().email().isCUID).toEqual(false); expect(z.string().email().isCUID2).toEqual(false); expect(z.string().email().isUUID).toEqual(false); + expect(z.string().email().isNANOID).toEqual(false); expect(z.string().email().isIP).toEqual(false); expect(z.string().email().isULID).toEqual(false); @@ -316,6 +340,7 @@ test("checks getters", () => { expect(z.string().url().isCUID).toEqual(false); expect(z.string().url().isCUID2).toEqual(false); expect(z.string().url().isUUID).toEqual(false); + expect(z.string().url().isNANOID).toEqual(false); expect(z.string().url().isIP).toEqual(false); expect(z.string().url().isULID).toEqual(false); @@ -324,6 +349,7 @@ test("checks getters", () => { expect(z.string().cuid().isCUID).toEqual(true); expect(z.string().cuid().isCUID2).toEqual(false); expect(z.string().cuid().isUUID).toEqual(false); + expect(z.string().cuid().isNANOID).toEqual(false); expect(z.string().cuid().isIP).toEqual(false); expect(z.string().cuid().isULID).toEqual(false); @@ -332,6 +358,7 @@ test("checks getters", () => { expect(z.string().cuid2().isCUID).toEqual(false); expect(z.string().cuid2().isCUID2).toEqual(true); expect(z.string().cuid2().isUUID).toEqual(false); + expect(z.string().cuid2().isNANOID).toEqual(false); expect(z.string().cuid2().isIP).toEqual(false); expect(z.string().cuid2().isULID).toEqual(false); @@ -340,14 +367,25 @@ test("checks getters", () => { expect(z.string().uuid().isCUID).toEqual(false); expect(z.string().uuid().isCUID2).toEqual(false); expect(z.string().uuid().isUUID).toEqual(true); + expect(z.string().uuid().isNANOID).toEqual(false); expect(z.string().uuid().isIP).toEqual(false); expect(z.string().uuid().isULID).toEqual(false); + expect(z.string().nanoid().isEmail).toEqual(false); + expect(z.string().nanoid().isURL).toEqual(false); + expect(z.string().nanoid().isCUID).toEqual(false); + expect(z.string().nanoid().isCUID2).toEqual(false); + expect(z.string().nanoid().isUUID).toEqual(false); + expect(z.string().nanoid().isNANOID).toEqual(true); + expect(z.string().nanoid().isIP).toEqual(false); + expect(z.string().nanoid().isULID).toEqual(false); + expect(z.string().ip().isEmail).toEqual(false); expect(z.string().ip().isURL).toEqual(false); expect(z.string().ip().isCUID).toEqual(false); expect(z.string().ip().isCUID2).toEqual(false); expect(z.string().ip().isUUID).toEqual(false); + expect(z.string().ip().isNANOID).toEqual(false); expect(z.string().ip().isIP).toEqual(true); expect(z.string().ip().isULID).toEqual(false); @@ -356,6 +394,7 @@ test("checks getters", () => { expect(z.string().ulid().isCUID).toEqual(false); expect(z.string().ulid().isCUID2).toEqual(false); expect(z.string().ulid().isUUID).toEqual(false); + expect(z.string().ulid().isNANOID).toEqual(false); expect(z.string().ulid().isIP).toEqual(false); expect(z.string().ulid().isULID).toEqual(true); }); diff --git a/src/types.ts b/src/types.ts index c73377dab..0f573f929 100644 --- a/src/types.ts +++ b/src/types.ts @@ -529,6 +529,7 @@ export type ZodStringCheck = | { kind: "url"; message?: string } | { kind: "emoji"; message?: string } | { kind: "uuid"; message?: string } + | { kind: "nanoid"; message?: string } | { kind: "cuid"; message?: string } | { kind: "includes"; value: string; position?: number; message?: string } | { kind: "cuid2"; message?: string } @@ -569,7 +570,8 @@ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/; // const uuidRegex = // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; const uuidRegex = - /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i; + /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; +const nanoidRegex = /^[a-z0-9_-]{21}$/i; // from https://stackoverflow.com/a/46181/1550155 // old version: too slow, didn't support unicode // const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i; @@ -755,6 +757,16 @@ export class ZodString extends ZodType { }); status.dirty(); } + } else if (check.kind === "nanoid") { + if (!nanoidRegex.test(input.data)) { + ctx = this._getOrReturnCtx(input, ctx); + addIssueToContext(ctx, { + validation: "nanoid", + code: ZodIssueCode.invalid_string, + message: check.message, + }); + status.dirty(); + } } else if (check.kind === "cuid") { if (!cuidRegex.test(input.data)) { ctx = this._getOrReturnCtx(input, ctx); @@ -933,7 +945,9 @@ export class ZodString extends ZodType { uuid(message?: errorUtil.ErrMessage) { return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) }); } - + nanoid(message?: errorUtil.ErrMessage) { + return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) }); + } cuid(message?: errorUtil.ErrMessage) { return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) }); } @@ -1124,7 +1138,9 @@ export class ZodString extends ZodType { get isUUID() { return !!this._def.checks.find((ch) => ch.kind === "uuid"); } - + get isNANOID() { + return !!this._def.checks.find((ch) => ch.kind === "nanoid"); + } get isCUID() { return !!this._def.checks.find((ch) => ch.kind === "cuid"); } From 9e0f2108f1d5ad4aa18279c5124f494fc76c504d Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Mon, 8 Apr 2024 20:22:10 -0700 Subject: [PATCH 2/3] Updates --- deno/lib/README.md | 6 +++--- deno/lib/helpers/parseUtil.ts | 6 ++++-- deno/lib/types.ts | 13 +++---------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/deno/lib/README.md b/deno/lib/README.md index 4793633a3..23549160a 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -900,9 +900,9 @@ date/time library. However, they are very convenient for validating user input. The `z.string().date()` method validates strings in the format `YYYY-MM-DD`. -The `z.string().time()` method validates strings in the format `HH:mm:ss[.SSSSSS][Z|(+|-)hh[:]mm]` -(the time portion of [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)). It defaults -to `HH:mm:ss[.SSSSSS]` validation: no timezone offsets or `Z`, with arbitrary sub-second +The `z.string().time()` method validates strings in the format `HH:mm:ss[.SSSSSS][Z|(+|-)hh[:]mm]` +(the time portion of [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)). It defaults +to `HH:mm:ss[.SSSSSS]` validation: no timezone offsets or `Z`, with arbitrary sub-second decimal. The `z.string().datetime()` method enforces ISO 8601; default is no timezone offsets and arbitrary sub-second decimal precision. diff --git a/deno/lib/helpers/parseUtil.ts b/deno/lib/helpers/parseUtil.ts index 8df221464..0e4da903c 100644 --- a/deno/lib/helpers/parseUtil.ts +++ b/deno/lib/helpers/parseUtil.ts @@ -112,9 +112,11 @@ export class ParseStatus { ): Promise> { const syncPairs: ObjectPair[] = []; for (const pair of pairs) { + const key = await pair.key; + const value = await pair.value; syncPairs.push({ - key: await pair.key, - value: await pair.value, + key, + value, }); } return ParseStatus.mergeObjectSync(status, syncPairs); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index bb01bc202..d9c9945c3 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -570,7 +570,7 @@ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/; // const uuidRegex = // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; const uuidRegex = - /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i; + /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; const nanoidRegex = /^[a-z0-9_-]{21}$/i; // from https://stackoverflow.com/a/46181/1550155 // old version: too slow, didn't support unicode @@ -945,13 +945,9 @@ export class ZodString extends ZodType { uuid(message?: errorUtil.ErrMessage) { return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) }); } -<<<<<<< HEAD - -======= nanoid(message?: errorUtil.ErrMessage) { return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) }); } ->>>>>>> f2615c0 (Added support for nanoid) cuid(message?: errorUtil.ErrMessage) { return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) }); } @@ -1142,13 +1138,9 @@ export class ZodString extends ZodType { get isUUID() { return !!this._def.checks.find((ch) => ch.kind === "uuid"); } -<<<<<<< HEAD - -======= get isNANOID() { return !!this._def.checks.find((ch) => ch.kind === "nanoid"); } ->>>>>>> f2615c0 (Added support for nanoid) get isCUID() { return !!this._def.checks.find((ch) => ch.kind === "cuid"); } @@ -2474,9 +2466,10 @@ export class ZodObject< const syncPairs: any[] = []; for (const pair of pairs) { const key = await pair.key; + const value = await pair.value; syncPairs.push({ key, - value: await pair.value, + value, alwaysSet: pair.alwaysSet, }); } From f75a9c405a6ec6a47883e25716efb87dc8bd088b Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Mon, 8 Apr 2024 20:23:44 -0700 Subject: [PATCH 3/3] Fix uuid regex --- deno/lib/types.ts | 2 +- src/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deno/lib/types.ts b/deno/lib/types.ts index d9c9945c3..715f207db 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -570,7 +570,7 @@ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/; // const uuidRegex = // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; const uuidRegex = - /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; + /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i; const nanoidRegex = /^[a-z0-9_-]{21}$/i; // from https://stackoverflow.com/a/46181/1550155 // old version: too slow, didn't support unicode diff --git a/src/types.ts b/src/types.ts index 0f573f929..abd4eb9fa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -570,7 +570,7 @@ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/; // const uuidRegex = // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; const uuidRegex = - /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i; + /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i; const nanoidRegex = /^[a-z0-9_-]{21}$/i; // from https://stackoverflow.com/a/46181/1550155 // old version: too slow, didn't support unicode