colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
32.85k stars 1.14k forks source link

Invalid emoji regular expression #2433

Open alexreyes opened 1 year ago

alexreyes commented 1 year ago

Here is the full error message:

Invalid regular expression: /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/: Invalid property name

based on sentry, it says the error is happening on this line:

const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u;

Looking thru the zod source code quickly it seems like this line is the origin.

For context, this is happening on a NextJS (13.1.6) site with zod v3.21.4.

Based on sentry it seems like it's happening on Android phones exclusively on the following browsers:

alexreyes commented 1 year ago

Per this issue: https://github.com/colinhacks/zod/issues/2302 I'm going to try downgrading to zod@3.20.2 and see if it fixes things.

Hopefully the info in this issue is helpful in the meantime!

JLHwung commented 1 year ago

https://github.com/colinhacks/zod/blob/a8be4500851923aa865e009fe9c2855e80482047/src/types.ts#L556

The Unicode Sequence Properties proposal has offered the ability to match all emoji sequences valid for interexchange:

/^\p{RGI_Emoji}$/v

And @babel/preset-env supports the v flag. This change will also fix https://github.com/colinhacks/zod/issues/2228:

/\p{RGI_Emoji}/v.test("2")
// false

/^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u.test("2")
// true

Because 2 is an Emoji_Component that can form the keycap emoji 2️⃣. There is a warning note on UTS #51

Implementations must not, however, assume that all Emoji_Component characters are also Emoji. There are some non-emoji characters that are used in various emoji sequences, such as tag characters and ZWJ.

jiangkeyuan commented 11 months ago

hi: I also encountered this issue on Android 8

const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u; react:17.0.2 zod: 3.21.4

Is there any solution?

underbleu commented 11 months ago

any solution?

colinhacks commented 11 months ago

This regex is now lazily created: https://twitter.com/colinhacks/status/1709688442316312846

So as long as you aren't using z.string().emoji() you shouldn't get an error.

If you really need emoji validation that works on these older browsers, I'd recommend using z.string().regex() and using an emoji regex that uses older syntax. They tend to be really long and complex so I don't want to put them in Zod core.

miklosme commented 9 months ago

Encountered this issue in a CI run. Upgrading to the latest version solved the problem 👍