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().ulid() - add support for ulids #2049

Merged
merged 4 commits into from
Mar 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ z.string().emoji();
z.string().uuid();
z.string().cuid();
z.string().cuid2();
z.string().ulid();
z.string().regex(regex);
z.string().startsWith(string);
z.string().endsWith(string);
Expand Down
1 change: 1 addition & 0 deletions deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type StringValidation =
| "regex"
| "cuid"
| "cuid2"
| "ulid"
| "datetime"
| "ip"
| { startsWith: string }
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 @@ -217,6 +217,16 @@ test("cuid2", () => {
}
});

test("ulid", () => {
const cuid = z.string().cuid();
cuid.parse("01ARZ3NDEKTSV4RRFFQ69G5FAV");
const result = cuid.safeParse("invalidulid");
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues[0].message).toEqual("Invalid ulid");
}
});

test("regex", () => {
z.string()
.regex(/^moo+$/)
Expand Down
21 changes: 20 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export type ZodStringCheck =
| { kind: "uuid"; message?: string }
| { kind: "cuid"; message?: string }
| { kind: "cuid2"; message?: string }
| { kind: "ulid"; message?: string }
| { kind: "startsWith"; value: string; message?: string }
| { kind: "endsWith"; value: string; message?: string }
| { kind: "regex"; regex: RegExp; message?: string }
Expand All @@ -519,6 +520,7 @@ export interface ZodStringDef extends ZodTypeDef {

const cuidRegex = /^c[^\s-]{8,}$/i;
const cuid2Regex = /^[a-z][a-z0-9]*$/;
const ulidRegex = /[0-9A-HJKMNP-TV-Z]{26}/;
const uuidRegex =
/^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
// from https://stackoverflow.com/a/46181/1550155
Expand Down Expand Up @@ -710,6 +712,16 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "ulid") {
if (!ulidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "ulid",
code: ZodIssueCode.invalid_string,
message: check.message,
});
status.dirty();
}
} else if (check.kind === "url") {
try {
new URL(input.data);
Expand Down Expand Up @@ -827,10 +839,14 @@ export class ZodString extends ZodType<string, ZodStringDef> {
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
}

ulid(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
}

ip(options?: string | { version?: "v4" | "v6"; message?: string }) {
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
}

datetime(
options?:
| string
Expand Down Expand Up @@ -952,6 +968,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
get isCUID2() {
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
}
get isULID() {
return !!this._def.checks.find((ch) => ch.kind === "ulid");
}
get isIP() {
return !!this._def.checks.find((ch) => ch.kind === "ip");
}
Expand Down
1 change: 1 addition & 0 deletions src/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type StringValidation =
| "regex"
| "cuid"
| "cuid2"
| "ulid"
| "datetime"
| "ip"
| { startsWith: string }
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 @@ -216,6 +216,16 @@ test("cuid2", () => {
}
});

test("ulid", () => {
const cuid = z.string().cuid();
cuid.parse("01ARZ3NDEKTSV4RRFFQ69G5FAV");
const result = cuid.safeParse("invalidulid");
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues[0].message).toEqual("Invalid ulid");
}
});

test("regex", () => {
z.string()
.regex(/^moo+$/)
Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export type ZodStringCheck =
| { kind: "uuid"; message?: string }
| { kind: "cuid"; message?: string }
| { kind: "cuid2"; message?: string }
| { kind: "ulid"; message?: string }
| { kind: "startsWith"; value: string; message?: string }
| { kind: "endsWith"; value: string; message?: string }
| { kind: "regex"; regex: RegExp; message?: string }
Expand All @@ -519,6 +520,7 @@ export interface ZodStringDef extends ZodTypeDef {

const cuidRegex = /^c[^\s-]{8,}$/i;
const cuid2Regex = /^[a-z][a-z0-9]*$/;
const ulidRegex = /[0-9A-HJKMNP-TV-Z]{26}/;
const uuidRegex =
/^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
// from https://stackoverflow.com/a/46181/1550155
Expand Down Expand Up @@ -710,6 +712,16 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "ulid") {
if (!ulidRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "ulid",
code: ZodIssueCode.invalid_string,
message: check.message,
});
status.dirty();
}
} else if (check.kind === "url") {
try {
new URL(input.data);
Expand Down Expand Up @@ -826,6 +838,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
cuid2(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
}
ulid(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
}

ip(options?: string | { version?: "v4" | "v6"; message?: string }) {
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
Expand Down Expand Up @@ -952,6 +967,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
get isCUID2() {
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
}
get isULID() {
return !!this._def.checks.find((ch) => ch.kind === "ulid");
}
get isIP() {
return !!this._def.checks.find((ch) => ch.kind === "ip");
}
Expand Down