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: add support for jwt #3070

Closed
wants to merge 4 commits into from
Closed
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ z.string().uuid();
z.string().cuid();
z.string().cuid2();
z.string().ulid();
z.string().jwt();
z.string().regex(regex);
z.string().includes(string);
z.string().startsWith(string);
Expand Down
1 change: 1 addition & 0 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ z.string().uuid();
z.string().cuid();
z.string().cuid2();
z.string().ulid();
z.string().jwt();
z.string().regex(regex);
z.string().includes(string);
z.string().startsWith(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"
| "jwt"
| "ulid"
| "datetime"
| "ip"
Expand Down
58 changes: 58 additions & 0 deletions deno/lib/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,48 @@ test("ulid", () => {
}
});

test.each([
[
"missing header",
{
jwt: ".something.else",
},
],
[
"missing payload",
{
jwt: "something..else",
},
],
[
"wrong character",
{
jwt: "-eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJncm91cCI6ImFuZHJvaWQiLCJhdWQiOiJhbmRyb2lkIiwiaXNzIjoiYXBpLnNvY2lhbGRlYWwubmwiLCJtZW1iZXIiOnsibmFtZSI6ImVyaWsifSwiZXhwIjoxNDUyMDgzMjA3LCJpYXQiOjE0NTE5OTY4MDd9.u7ZBa9RB8U4QL8eBk4hmsjg8oFW19AHuen12c8CvLMj0IQUsNqeC-vwNQvAINpgBM0bzDf5cotyrUzf55eXch6mzfKMa-OJXguO-lARp4fc40HaBWbfnEvGe7yEgSESkt6gJNuprG51A6f4AJyNlXG_3u7O4bAMwiPZJc3AAU84_JXC7Vlq1X3FMaLVGmZdxzA4TvYZEiTt_KHoA49UgzeZtNXo3YiDq-GgL1eV8Li01fwy-M-xzbp4cPcY89jkPyYxUIJEoITOULr3zXQwRfYVe6i0P28oyu5ZzAwYCajBb2T98zN7sFJarNmtcxSKNfhCPnMVn3wrpxx4_Kd2a",
},
],
])("wrong jwt: %s", (_testName: string, { jwt }: { jwt: string }) => {
const jwtSchema = z.string().jwt();
const result = jwtSchema.safeParse(jwt);
expect(result.success).toEqual(false);

if (result.success) {
fail("fail as should not be success response");
} else {
expect(result.error.issues[0].message).toEqual("Invalid jwt");
}
expect(() => jwtSchema.parse(jwt)).toThrow();
});

test("valid jwt", () => {
const jwtSchema = z.string().jwt();
const jwt =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJncm91cCI6ImFuZHJvaWQiLCJhdWQiOiJhbmRyb2lkIiwiaXNzIjoiYXBpLnNvY2lhbGRlYWwubmwiLCJtZW1iZXIiOnsibmFtZSI6ImVyaWsifSwiZXhwIjoxNDUyMDgzMjA3LCJpYXQiOjE0NTE5OTY4MDd9.u7ZBa9RB8U4QL8eBk4hmsjg8oFW19AHuen12c8CvLMj0IQUsNqeC-vwNQvAINpgBM0bzDf5cotyrUzf55eXch6mzfKMa-OJXguO-lARp4fc40HaBWbfnEvGe7yEgSESkt6gJNuprG51A6f4AJyNlXG_3u7O4bAMwiPZJc3AAU84_JXC7Vlq1X3FMaLVGmZdxzA4TvYZEiTt_KHoA49UgzeZtNXo3YiDq-GgL1eV8Li01fwy-M-xzbp4cPcY89jkPyYxUIJEoITOULr3zXQwRfYVe6i0P28oyu5ZzAwYCajBb2T98zN7sFJarNmtcxSKNfhCPnMVn3wrpxx4_Kd2a";
const result = jwtSchema.safeParse(jwt);
expect(result.success).toEqual(true);

jwtSchema.parse(jwt);
});

test("regex", () => {
z.string()
.regex(/^moo+$/)
Expand Down Expand Up @@ -311,6 +353,7 @@ test("checks getters", () => {
expect(z.string().email().isUUID).toEqual(false);
expect(z.string().email().isIP).toEqual(false);
expect(z.string().email().isULID).toEqual(false);
expect(z.string().email().isJWT).toEqual(false);

expect(z.string().url().isEmail).toEqual(false);
expect(z.string().url().isURL).toEqual(true);
Expand All @@ -319,6 +362,7 @@ test("checks getters", () => {
expect(z.string().url().isUUID).toEqual(false);
expect(z.string().url().isIP).toEqual(false);
expect(z.string().url().isULID).toEqual(false);
expect(z.string().url().isJWT).toEqual(false);

expect(z.string().cuid().isEmail).toEqual(false);
expect(z.string().cuid().isURL).toEqual(false);
Expand All @@ -327,6 +371,7 @@ test("checks getters", () => {
expect(z.string().cuid().isUUID).toEqual(false);
expect(z.string().cuid().isIP).toEqual(false);
expect(z.string().cuid().isULID).toEqual(false);
expect(z.string().cuid().isJWT).toEqual(false);

expect(z.string().cuid2().isEmail).toEqual(false);
expect(z.string().cuid2().isURL).toEqual(false);
Expand All @@ -335,6 +380,7 @@ test("checks getters", () => {
expect(z.string().cuid2().isUUID).toEqual(false);
expect(z.string().cuid2().isIP).toEqual(false);
expect(z.string().cuid2().isULID).toEqual(false);
expect(z.string().cuid2().isJWT).toEqual(false);

expect(z.string().uuid().isEmail).toEqual(false);
expect(z.string().uuid().isURL).toEqual(false);
Expand All @@ -343,6 +389,7 @@ test("checks getters", () => {
expect(z.string().uuid().isUUID).toEqual(true);
expect(z.string().uuid().isIP).toEqual(false);
expect(z.string().uuid().isULID).toEqual(false);
expect(z.string().uuid().isJWT).toEqual(false);

expect(z.string().ip().isEmail).toEqual(false);
expect(z.string().ip().isURL).toEqual(false);
Expand All @@ -351,6 +398,7 @@ test("checks getters", () => {
expect(z.string().ip().isUUID).toEqual(false);
expect(z.string().ip().isIP).toEqual(true);
expect(z.string().ip().isULID).toEqual(false);
expect(z.string().ip().isJWT).toEqual(false);

expect(z.string().ulid().isEmail).toEqual(false);
expect(z.string().ulid().isURL).toEqual(false);
Expand All @@ -359,6 +407,16 @@ test("checks getters", () => {
expect(z.string().ulid().isUUID).toEqual(false);
expect(z.string().ulid().isIP).toEqual(false);
expect(z.string().ulid().isULID).toEqual(true);
expect(z.string().ulid().isJWT).toEqual(false);

expect(z.string().jwt().isEmail).toEqual(false);
expect(z.string().jwt().isURL).toEqual(false);
expect(z.string().jwt().isCUID).toEqual(false);
expect(z.string().jwt().isCUID2).toEqual(false);
expect(z.string().jwt().isUUID).toEqual(false);
expect(z.string().jwt().isIP).toEqual(false);
expect(z.string().jwt().isULID).toEqual(false);
expect(z.string().jwt().isJWT).toEqual(true);
});

test("min max getters", () => {
Expand Down
18 changes: 18 additions & 0 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ export type ZodStringCheck =
| { kind: "includes"; value: string; position?: number; message?: string }
| { kind: "cuid2"; message?: string }
| { kind: "ulid"; message?: string }
| { kind: "jwt"; message?: string }
| { kind: "startsWith"; value: string; message?: string }
| { kind: "endsWith"; value: string; message?: string }
| { kind: "regex"; regex: RegExp; message?: string }
Expand All @@ -548,6 +549,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 jwtRegex = /^([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]*)$/;
// 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;
const uuidRegex =
Expand Down Expand Up @@ -763,6 +765,16 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "jwt") {
if (!jwtRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "jwt",
code: ZodIssueCode.invalid_string,
message: check.message,
});
status.dirty();
}
} else if (check.kind === "url") {
try {
new URL(input.data);
Expand Down Expand Up @@ -893,6 +905,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
ulid(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
}
jwt(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(message) });
}

ip(options?: string | { version?: "v4" | "v6"; message?: string }) {
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
Expand Down Expand Up @@ -1035,6 +1050,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
get isULID() {
return !!this._def.checks.find((ch) => ch.kind === "ulid");
}
get isJWT() {
return !!this._def.checks.find((ch) => ch.kind === "jwt");
}
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"
| "jwt"
| "ulid"
| "datetime"
| "ip"
Expand Down
58 changes: 58 additions & 0 deletions src/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,48 @@ test("ulid", () => {
}
});

test.each([
[
"missing header",
{
jwt: ".something.else",
},
],
[
"missing payload",
{
jwt: "something..else",
},
],
[
"wrong character",
{
jwt: "-eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJncm91cCI6ImFuZHJvaWQiLCJhdWQiOiJhbmRyb2lkIiwiaXNzIjoiYXBpLnNvY2lhbGRlYWwubmwiLCJtZW1iZXIiOnsibmFtZSI6ImVyaWsifSwiZXhwIjoxNDUyMDgzMjA3LCJpYXQiOjE0NTE5OTY4MDd9.u7ZBa9RB8U4QL8eBk4hmsjg8oFW19AHuen12c8CvLMj0IQUsNqeC-vwNQvAINpgBM0bzDf5cotyrUzf55eXch6mzfKMa-OJXguO-lARp4fc40HaBWbfnEvGe7yEgSESkt6gJNuprG51A6f4AJyNlXG_3u7O4bAMwiPZJc3AAU84_JXC7Vlq1X3FMaLVGmZdxzA4TvYZEiTt_KHoA49UgzeZtNXo3YiDq-GgL1eV8Li01fwy-M-xzbp4cPcY89jkPyYxUIJEoITOULr3zXQwRfYVe6i0P28oyu5ZzAwYCajBb2T98zN7sFJarNmtcxSKNfhCPnMVn3wrpxx4_Kd2a",
},
],
])("wrong jwt: %s", (_testName: string, { jwt }: { jwt: string }) => {
const jwtSchema = z.string().jwt();
const result = jwtSchema.safeParse(jwt);
expect(result.success).toEqual(false);

if (result.success) {
fail("fail as should not be success response");
} else {
expect(result.error.issues[0].message).toEqual("Invalid jwt");
}
expect(() => jwtSchema.parse(jwt)).toThrow();
});

test("valid jwt", () => {
const jwtSchema = z.string().jwt();
const jwt =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJncm91cCI6ImFuZHJvaWQiLCJhdWQiOiJhbmRyb2lkIiwiaXNzIjoiYXBpLnNvY2lhbGRlYWwubmwiLCJtZW1iZXIiOnsibmFtZSI6ImVyaWsifSwiZXhwIjoxNDUyMDgzMjA3LCJpYXQiOjE0NTE5OTY4MDd9.u7ZBa9RB8U4QL8eBk4hmsjg8oFW19AHuen12c8CvLMj0IQUsNqeC-vwNQvAINpgBM0bzDf5cotyrUzf55eXch6mzfKMa-OJXguO-lARp4fc40HaBWbfnEvGe7yEgSESkt6gJNuprG51A6f4AJyNlXG_3u7O4bAMwiPZJc3AAU84_JXC7Vlq1X3FMaLVGmZdxzA4TvYZEiTt_KHoA49UgzeZtNXo3YiDq-GgL1eV8Li01fwy-M-xzbp4cPcY89jkPyYxUIJEoITOULr3zXQwRfYVe6i0P28oyu5ZzAwYCajBb2T98zN7sFJarNmtcxSKNfhCPnMVn3wrpxx4_Kd2a";
const result = jwtSchema.safeParse(jwt);
expect(result.success).toEqual(true);

jwtSchema.parse(jwt);
});

test("regex", () => {
z.string()
.regex(/^moo+$/)
Expand Down Expand Up @@ -310,6 +352,7 @@ test("checks getters", () => {
expect(z.string().email().isUUID).toEqual(false);
expect(z.string().email().isIP).toEqual(false);
expect(z.string().email().isULID).toEqual(false);
expect(z.string().email().isJWT).toEqual(false);

expect(z.string().url().isEmail).toEqual(false);
expect(z.string().url().isURL).toEqual(true);
Expand All @@ -318,6 +361,7 @@ test("checks getters", () => {
expect(z.string().url().isUUID).toEqual(false);
expect(z.string().url().isIP).toEqual(false);
expect(z.string().url().isULID).toEqual(false);
expect(z.string().url().isJWT).toEqual(false);

expect(z.string().cuid().isEmail).toEqual(false);
expect(z.string().cuid().isURL).toEqual(false);
Expand All @@ -326,6 +370,7 @@ test("checks getters", () => {
expect(z.string().cuid().isUUID).toEqual(false);
expect(z.string().cuid().isIP).toEqual(false);
expect(z.string().cuid().isULID).toEqual(false);
expect(z.string().cuid().isJWT).toEqual(false);

expect(z.string().cuid2().isEmail).toEqual(false);
expect(z.string().cuid2().isURL).toEqual(false);
Expand All @@ -334,6 +379,7 @@ test("checks getters", () => {
expect(z.string().cuid2().isUUID).toEqual(false);
expect(z.string().cuid2().isIP).toEqual(false);
expect(z.string().cuid2().isULID).toEqual(false);
expect(z.string().cuid2().isJWT).toEqual(false);

expect(z.string().uuid().isEmail).toEqual(false);
expect(z.string().uuid().isURL).toEqual(false);
Expand All @@ -342,6 +388,7 @@ test("checks getters", () => {
expect(z.string().uuid().isUUID).toEqual(true);
expect(z.string().uuid().isIP).toEqual(false);
expect(z.string().uuid().isULID).toEqual(false);
expect(z.string().uuid().isJWT).toEqual(false);

expect(z.string().ip().isEmail).toEqual(false);
expect(z.string().ip().isURL).toEqual(false);
Expand All @@ -350,6 +397,7 @@ test("checks getters", () => {
expect(z.string().ip().isUUID).toEqual(false);
expect(z.string().ip().isIP).toEqual(true);
expect(z.string().ip().isULID).toEqual(false);
expect(z.string().ip().isJWT).toEqual(false);

expect(z.string().ulid().isEmail).toEqual(false);
expect(z.string().ulid().isURL).toEqual(false);
Expand All @@ -358,6 +406,16 @@ test("checks getters", () => {
expect(z.string().ulid().isUUID).toEqual(false);
expect(z.string().ulid().isIP).toEqual(false);
expect(z.string().ulid().isULID).toEqual(true);
expect(z.string().ulid().isJWT).toEqual(false);

expect(z.string().jwt().isEmail).toEqual(false);
expect(z.string().jwt().isURL).toEqual(false);
expect(z.string().jwt().isCUID).toEqual(false);
expect(z.string().jwt().isCUID2).toEqual(false);
expect(z.string().jwt().isUUID).toEqual(false);
expect(z.string().jwt().isIP).toEqual(false);
expect(z.string().jwt().isULID).toEqual(false);
expect(z.string().jwt().isJWT).toEqual(true);
});

test("min max getters", () => {
Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ export type ZodStringCheck =
| { kind: "includes"; value: string; position?: number; message?: string }
| { kind: "cuid2"; message?: string }
| { kind: "ulid"; message?: string }
| { kind: "jwt"; message?: string }
| { kind: "startsWith"; value: string; message?: string }
| { kind: "endsWith"; value: string; message?: string }
| { kind: "regex"; regex: RegExp; message?: string }
Expand All @@ -548,6 +549,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 jwtRegex = /^([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]*)$/;
// 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;
const uuidRegex =
Expand Down Expand Up @@ -763,6 +765,16 @@ export class ZodString extends ZodType<string, ZodStringDef> {
});
status.dirty();
}
} else if (check.kind === "jwt") {
if (!jwtRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "jwt",
code: ZodIssueCode.invalid_string,
message: check.message,
});
status.dirty();
}
} else if (check.kind === "url") {
try {
new URL(input.data);
Expand Down Expand Up @@ -893,6 +905,9 @@ export class ZodString extends ZodType<string, ZodStringDef> {
ulid(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
}
jwt(message?: errorUtil.ErrMessage) {
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(message) });
}

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