-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add JWT string validator #3887
Add JWT string validator #3887
Conversation
- Add JWT validation to string schema - Implement validation in _parse method - Add comprehensive test coverage - Support optional algorithm validation - Maintain cross-runtime compatibility
✅ Deploy Preview for guileless-rolypoly-866f8a ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also Prettier is failing, run yarn prettier:fix
to fix that.
src/types.ts
Outdated
|
||
// Validate algorithm if specified | ||
if (check.options?.alg && header.alg !== check.options.alg) { | ||
throw new Error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid throwing errors in hot path for performance reasons
src/types.ts
Outdated
|
||
// Decode and validate header | ||
const header = JSON.parse(atob(parts[0])); | ||
if (!header.typ || !header.alg) throw new Error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid throwing errors in hot path for performance reasons
src/types.ts
Outdated
try { | ||
// Validate all parts are base64 | ||
for (const part of parts) { | ||
if (!base64Regex.test(part)) throw new Error(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid throwing errors in hot path for performance reasons
@@ -943,6 +963,41 @@ export class ZodString extends ZodType<string, ZodStringDef, string> { | |||
}); | |||
status.dirty(); | |||
} | |||
} else if (check.kind === "jwt") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try splitting the JWT validation into a standalone utility function
const parts = input.data.split("."); | ||
if (parts.length !== 3) { | ||
ctx = this._getOrReturnCtx(input, ctx); | ||
addIssueToContext(ctx, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
structure the code to avoid multiple calls to addIssueToContext
for bundle size reasons
Thanks for the review! The throwing error part + |
This PR retroactively implements the jwt string format in Zod v3. Resolves #2946
Supports the following API:
Validates:
Link to Devin run: https://preview.devin.ai/sessions/7aa913c03a7d4ce0b5c8f87d2ce59ec5