diff --git a/src/parsers/string.ts b/src/parsers/string.ts index 7dc6cf5..fb1396e 100644 --- a/src/parsers/string.ts +++ b/src/parsers/string.ts @@ -2,6 +2,8 @@ import { ZodStringDef } from "zod"; import { ErrorMessages, setResponseValueAndErrors } from "../errorMessages.js"; import { Refs } from "../Refs.js"; +let emojiRegex: RegExp | undefined; + /** * Generated from the regular expressions found here as of 2024-05-22: * https://github.com/colinhacks/zod/blob/master/src/types.ts. @@ -22,8 +24,21 @@ export const zodPatterns = { /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/, /** * Constructed a valid Unicode RegExp + * + * Lazily instantiate since this type of regex isn't supported + * in all envs (e.g. React Native). + * + * See: + * https://github.com/colinhacks/zod/issues/2433 + * Fix in Zod: + * https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b */ - emoji: RegExp("^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$", "u"), + emoji: () => { + if (emojiRegex === undefined) { + emojiRegex = RegExp("^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$", "u"); + } + return emojiRegex; + }, /** * Unused */ @@ -297,7 +312,7 @@ const addFormat = ( const addPattern = ( schema: JsonSchema7StringType, - regex: RegExp, + regex: RegExp | (() => RegExp), message: string | undefined, refs: Refs, ) => { @@ -340,7 +355,8 @@ const addPattern = ( }; // Mutate z.string.regex() in a best attempt to accommodate for regex flags when applyRegexFlags is true -const processRegExp = (regex: RegExp, refs: Refs): string => { +const processRegExp = (regexOrFunction: RegExp | (() => RegExp), refs: Refs): string => { + const regex = typeof regexOrFunction === "function" ? regexOrFunction() : regexOrFunction; if (!refs.applyRegexFlags || !regex.flags) return regex.source; // Currently handled flags diff --git a/test/allParsers.test.ts b/test/allParsers.test.ts index bf1dbc4..cd3fe21 100644 --- a/test/allParsers.test.ts +++ b/test/allParsers.test.ts @@ -256,6 +256,10 @@ suite("All Parsers tests", (test) => { type: "string", format: "email", }, + stringEmoji: { + type: "string", + pattern: '^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$', + }, stringUrl: { type: "string", format: "uri", @@ -577,6 +581,10 @@ suite("All Parsers tests", (test) => { type: "string", format: "email", }, + stringEmoji: { + type: "string", + pattern: '^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$', + }, stringUrl: { type: "string", format: "uri", diff --git a/test/allParsersSchema.ts b/test/allParsersSchema.ts index 8d51e31..fdc45b8 100644 --- a/test/allParsersSchema.ts +++ b/test/allParsersSchema.ts @@ -65,6 +65,7 @@ export const allParsersSchema = z stringMin: z.string().min(1), stringMax: z.string().max(1), stringEmail: z.string().email(), + stringEmoji: z.string().emoji(), stringUrl: z.string().url(), stringUuid: z.string().uuid(), stringRegEx: z.string().regex(new RegExp("abc")),