-
-
Notifications
You must be signed in to change notification settings - Fork 369
feat: add support for base64url regex validator #1557
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||||||||||||
| import type { | ||||||||||||||||
| BaseIssue, | ||||||||||||||||
| BaseValidation, | ||||||||||||||||
| ErrorMessage, | ||||||||||||||||
| } from '../../types/index.ts'; | ||||||||||||||||
| import { _addIssue } from '../../utils/index.ts'; | ||||||||||||||||
|
|
||||||||||||||||
| const BASE64URL_REGEX: RegExp = | ||||||||||||||||
| /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu; | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: The regex accepts invalid padding Prompt for AI agentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Malformed padding and non-ASCII characters pass validation: Prompt for AI agents |
||||||||||||||||
|
|
||||||||||||||||
| export interface Base64UrlIssue<TInput extends string> | ||||||||||||||||
| extends BaseIssue<TInput> { | ||||||||||||||||
| readonly kind: 'validation'; | ||||||||||||||||
| readonly type: 'base64url'; | ||||||||||||||||
| readonly expected: null; | ||||||||||||||||
| readonly received: `"${string}"`; | ||||||||||||||||
| readonly requirement: RegExp; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export interface Base64UrlAction< | ||||||||||||||||
| TInput extends string, | ||||||||||||||||
| TMessage extends ErrorMessage<Base64UrlIssue<TInput>> | undefined, | ||||||||||||||||
| > extends BaseValidation<TInput, TInput, Base64UrlIssue<TInput>> { | ||||||||||||||||
| readonly type: 'base64url'; | ||||||||||||||||
| readonly reference: typeof base64Url; | ||||||||||||||||
| readonly expects: null; | ||||||||||||||||
| readonly requirement: RegExp; | ||||||||||||||||
| readonly message: TMessage; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export function base64Url<TInput extends string>(): Base64UrlAction< | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win Add missing JSDoc to the exported function. As per coding guidelines, JSDoc required on exported functions (first overload only for overload sets). 📝 Proposed fix to add JSDoc+/**
+ * Creates a base64url validation action.
+ *
+ * `@returns` A base64url action.
+ */
export function base64Url<TInput extends string>(): Base64UrlAction<📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||||
| TInput, | ||||||||||||||||
| undefined | ||||||||||||||||
| >; | ||||||||||||||||
|
|
||||||||||||||||
| export function base64Url< | ||||||||||||||||
| TInput extends string, | ||||||||||||||||
| const TMessage extends ErrorMessage<Base64UrlIssue<TInput>> | undefined, | ||||||||||||||||
| >(message: TMessage): Base64UrlAction<TInput, TMessage>; | ||||||||||||||||
|
|
||||||||||||||||
| // @__NO_SIDE_EFFECTS__ | ||||||||||||||||
| export function base64Url( | ||||||||||||||||
| message?: ErrorMessage<Base64UrlIssue<string>>, | ||||||||||||||||
| ): Base64UrlAction<string, ErrorMessage<Base64UrlIssue<string>> | undefined> { | ||||||||||||||||
| return { | ||||||||||||||||
| kind: 'validation', | ||||||||||||||||
| type: 'base64url', | ||||||||||||||||
| reference: base64Url, | ||||||||||||||||
| async: false, | ||||||||||||||||
| expects: null, | ||||||||||||||||
| requirement: BASE64URL_REGEX, | ||||||||||||||||
| message, | ||||||||||||||||
| '~run'(dataset, config) { | ||||||||||||||||
| if (dataset.typed && !this.requirement.test(dataset.value)) { | ||||||||||||||||
| _addIssue(this, 'base64url', dataset, config); | ||||||||||||||||
| } | ||||||||||||||||
| return dataset; | ||||||||||||||||
| }, | ||||||||||||||||
| }; | ||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './base64Url.ts'; |
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.
🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win
Fix regex to reject incorrectly padded strings.
The
={0,2}quantifier allows a single padding character (=) for a 2-character base64 block, which mathematically represents an invalid encoding (e.g.,ab=). A 2-character block must have exactly two padding characters (==) or none. Use(?:==)?and=?to enforce correct padding lengths.🐛 Proposed fix for the regex
📝 Committable suggestion
🤖 Prompt for AI Agents