Skip to content

Commit

Permalink
Fix emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonnell committed Feb 16, 2023
1 parent 39cbb69 commit d8f07bb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
4 changes: 3 additions & 1 deletion deno/lib/__tests__/string.test.ts
Expand Up @@ -133,9 +133,11 @@ test("emoji validations", () => {
const emoji = z.string().emoji();

emoji.parse("🍺👩‍🚀🫡");
emoji.parse("💚 💙 💜 💛 ❤️");
emoji.parse("💚💙💜💛❤️");
expect(() => emoji.parse(":-)")).toThrow();
expect(() => emoji.parse("😀 is an emoji")).toThrow();
expect(() => emoji.parse("😀stuff")).toThrow();
expect(() => emoji.parse("stuff😀")).toThrow();
});

test("uuid", () => {
Expand Down
11 changes: 2 additions & 9 deletions deno/lib/types.ts
Expand Up @@ -530,14 +530,7 @@ const emailRegex =
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression

const emojiRegex =
/^(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*$/;

// interface IsDateStringOptions extends StringDateOptions {
/**
* Match any configuration
*/
// any?: boolean;
// }
/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\uFE0E|\uFE0F)/;

// Adapted from https://stackoverflow.com/a/3143231
const datetimeRegex = (args: { precision: number | null; offset: boolean }) => {
Expand Down Expand Up @@ -660,7 +653,7 @@ export class ZodString extends ZodType<string, ZodStringDef> {
status.dirty();
}
} else if (check.kind === "emoji") {
if (!emojiRegex.test(input.data)) {
if (![...input.data].every((char) => emojiRegex.test(char))) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "emoji",
Expand Down
15 changes: 13 additions & 2 deletions playground.ts
@@ -1,5 +1,16 @@
import { z } from "./src";

const schema = z.string().emoji();
// const emoji = z.string().emoji();

schema.parse("😀 is an emoji");
const emojiRegex =
/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\uFE0E|\uFE0F)/;

function isEmoji(val: string) {
return [...val].every((char) => emojiRegex.test(char));
}
console.log(isEmoji("🍺👩‍🚀🫡"));
console.log(isEmoji("💚💙💜💛❤️"));
console.log(isEmoji(":-)"));
console.log(isEmoji("asdf"));
console.log(isEmoji("😀stuff"));
console.log(isEmoji("stuff😀"));
4 changes: 3 additions & 1 deletion src/__tests__/string.test.ts
Expand Up @@ -132,9 +132,11 @@ test("emoji validations", () => {
const emoji = z.string().emoji();

emoji.parse("🍺👩‍🚀🫡");
emoji.parse("💚 💙 💜 💛 ❤️");
emoji.parse("💚💙💜💛❤️");
expect(() => emoji.parse(":-)")).toThrow();
expect(() => emoji.parse("😀 is an emoji")).toThrow();
expect(() => emoji.parse("😀stuff")).toThrow();
expect(() => emoji.parse("stuff😀")).toThrow();
});

test("uuid", () => {
Expand Down
11 changes: 2 additions & 9 deletions src/types.ts
Expand Up @@ -530,14 +530,7 @@ const emailRegex =
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression

const emojiRegex =
/^(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*$/;

// interface IsDateStringOptions extends StringDateOptions {
/**
* Match any configuration
*/
// any?: boolean;
// }
/(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\uFE0E|\uFE0F)/;

// Adapted from https://stackoverflow.com/a/3143231
const datetimeRegex = (args: { precision: number | null; offset: boolean }) => {
Expand Down Expand Up @@ -660,7 +653,7 @@ export class ZodString extends ZodType<string, ZodStringDef> {
status.dirty();
}
} else if (check.kind === "emoji") {
if (!emojiRegex.test(input.data)) {
if (![...input.data].every((char) => emojiRegex.test(char))) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "emoji",
Expand Down

0 comments on commit d8f07bb

Please sign in to comment.