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

feat: z.string().emoji() #2045

Merged
merged 2 commits into from
Feb 16, 2023
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
1 change: 1 addition & 0 deletions deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface ZodInvalidDateIssue extends ZodIssueBase {
export type StringValidation =
| "email"
| "url"
| "emoji"
| "uuid"
| "regex"
| "cuid"
Expand Down
10 changes: 10 additions & 0 deletions deno/lib/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ test("url error overrides", () => {
}
});

test("emoji validations", () => {
const emoji = z.string().emoji();
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just passing by, but doesn't this test always pass because of the empty try-catch?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed!

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

test("uuid", () => {
const uuid = z.string().uuid("custom error");
uuid.parse("9491d710-3185-4e06-bea0-6a2f275345e0");
Expand Down
22 changes: 22 additions & 0 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ export type ZodStringCheck =
| { kind: "length"; value: number; message?: string }
| { kind: "email"; message?: string }
| { kind: "url"; message?: string }
| { kind: "emoji"; message?: string }
| { kind: "uuid"; message?: string }
| { kind: "cuid"; message?: string }
| { kind: "cuid2"; message?: string }
Expand Down Expand Up @@ -526,6 +527,11 @@ const uuidRegex =
const emailRegex =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|([^-]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,}))$/;

// 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
Expand Down Expand Up @@ -653,6 +659,16 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "emoji") {
if (!emojiRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "emoji",
code: ZodIssueCode.invalid_string,
message: check.message,
});
status.dirty();
}
} else if (check.kind === "uuid") {
if (!uuidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
Expand Down Expand Up @@ -773,6 +789,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
url(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
}
emoji(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
}
uuid(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
}
Expand Down Expand Up @@ -879,6 +898,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
get isURL() {
return !!this._def.checks.find((ch) => ch.kind === "url");
}
get isEmoji() {
return !!this._def.checks.find((ch) => ch.kind === "emoji");
}
get isUUID() {
return !!this._def.checks.find((ch) => ch.kind === "uuid");
}
Expand Down
1 change: 1 addition & 0 deletions src/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface ZodInvalidDateIssue extends ZodIssueBase {
export type StringValidation =
| "email"
| "url"
| "emoji"
| "uuid"
| "regex"
| "cuid"
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ test("url error overrides", () => {
}
});

test("emoji validations", () => {
const emoji = z.string().emoji();
try {
emoji.parse("🍺👩‍🚀🫡");
emoji.parse("💚 💙 💜 💛 ❤️");
expect(() => emoji.parse(":-)")).toThrow();
expect(() => emoji.parse("😀 is an emoji")).toThrow()
} catch (err) {}
});

test("uuid", () => {
const uuid = z.string().uuid("custom error");
uuid.parse("9491d710-3185-4e06-bea0-6a2f275345e0");
Expand Down
22 changes: 22 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ export type ZodStringCheck =
| { kind: "length"; value: number; message?: string }
| { kind: "email"; message?: string }
| { kind: "url"; message?: string }
| { kind: "emoji"; message?: string }
| { kind: "uuid"; message?: string }
| { kind: "cuid"; message?: string }
| { kind: "cuid2"; message?: string }
Expand Down Expand Up @@ -526,6 +527,11 @@ const uuidRegex =
const emailRegex =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|([^-]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,}))$/;

// 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
Expand Down Expand Up @@ -653,6 +659,16 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "emoji") {
if (!emojiRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "emoji",
code: ZodIssueCode.invalid_string,
message: check.message,
});
status.dirty();
}
} else if (check.kind === "uuid") {
if (!uuidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
Expand Down Expand Up @@ -773,6 +789,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
url(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
}
emoji(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
}
uuid(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
}
Expand Down Expand Up @@ -879,6 +898,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
get isURL() {
return !!this._def.checks.find((ch) => ch.kind === "url");
}
get isEmoji() {
return !!this._def.checks.find((ch) => ch.kind === "emoji");
}
get isUUID() {
return !!this._def.checks.find((ch) => ch.kind === "uuid");
}
Expand Down