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

Instantiate emoji regex lazily to broaden env support #133

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 19 additions & 3 deletions src/parsers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
*/
Expand Down Expand Up @@ -297,7 +312,7 @@ const addFormat = (

const addPattern = (
schema: JsonSchema7StringType,
regex: RegExp,
regex: RegExp | (() => RegExp),
message: string | undefined,
refs: Refs,
) => {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/allParsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions test/allParsersSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down