-
-
Notifications
You must be signed in to change notification settings - Fork 369
feat: support for relative URLs #1552
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 @@ | ||
| export * from './urlRelative.ts'; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||||||||||||||||||||||||
| import type { | ||||||||||||||||||||||||||||
| BaseIssue, | ||||||||||||||||||||||||||||
| BaseValidation, | ||||||||||||||||||||||||||||
| ErrorMessage, | ||||||||||||||||||||||||||||
| } from '../../types/index.ts'; | ||||||||||||||||||||||||||||
| import { _addIssue } from '../../utils/index.ts'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| function isRelativeUrl(value: string): boolean { | ||||||||||||||||||||||||||||
| if (value === '') return false; | ||||||||||||||||||||||||||||
| if (/^[a-z][a-z0-9+.-]*:/i.test(value)) return false; | ||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||
| new URL(value, 'http://example.com'); | ||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export interface UrlRelativeIssue<TInput extends string> | ||||||||||||||||||||||||||||
| extends BaseIssue<TInput> { | ||||||||||||||||||||||||||||
| readonly kind: 'validation'; | ||||||||||||||||||||||||||||
| readonly type: 'url_relative'; | ||||||||||||||||||||||||||||
| readonly expected: null; | ||||||||||||||||||||||||||||
| readonly received: `"${string}"`; | ||||||||||||||||||||||||||||
| readonly requirement: (input: string) => boolean; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export interface UrlRelativeAction< | ||||||||||||||||||||||||||||
| TInput extends string, | ||||||||||||||||||||||||||||
| TMessage extends ErrorMessage<UrlRelativeIssue<TInput>> | undefined, | ||||||||||||||||||||||||||||
| > extends BaseValidation<TInput, TInput, UrlRelativeIssue<TInput>> { | ||||||||||||||||||||||||||||
| readonly type: 'url_relative'; | ||||||||||||||||||||||||||||
| readonly reference: typeof urlRelative; | ||||||||||||||||||||||||||||
| readonly expects: null; | ||||||||||||||||||||||||||||
| readonly requirement: (input: string) => boolean; | ||||||||||||||||||||||||||||
| readonly message: TMessage; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export function urlRelative<TInput extends string>(): UrlRelativeAction< | ||||||||||||||||||||||||||||
|
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. P3: This new public validation action has no tests, so acceptance/rejection behavior—including empty strings, scheme-relative references, and absolute schemes—can regress without detection. Add runtime and declaration tests following Prompt for AI agents |
||||||||||||||||||||||||||||
| TInput, | ||||||||||||||||||||||||||||
| undefined | ||||||||||||||||||||||||||||
| >; | ||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+42
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 | 🟡 Minor | ⚡ Quick win Add JSDoc for the exported function. As per coding guidelines, a JSDoc comment is required on the first overload of exported functions. 📝 Proposed JSDoc addition+/**
+ * Creates a relative URL validation action.
+ *
+ * `@returns` A relative URL action.
+ */
export function urlRelative<TInput extends string>(): UrlRelativeAction<
TInput,
undefined
>;📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export function urlRelative< | ||||||||||||||||||||||||||||
| TInput extends string, | ||||||||||||||||||||||||||||
| const TMessage extends ErrorMessage<UrlRelativeIssue<TInput>> | undefined, | ||||||||||||||||||||||||||||
| >(message: TMessage): UrlRelativeAction<TInput, TMessage>; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // @__NO_SIDE_EFFECTS__ | ||||||||||||||||||||||||||||
| export function urlRelative( | ||||||||||||||||||||||||||||
| message?: ErrorMessage<UrlRelativeIssue<string>>, | ||||||||||||||||||||||||||||
| ): UrlRelativeAction<string, ErrorMessage<UrlRelativeIssue<string>> | undefined> { | ||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||
| kind: 'validation', | ||||||||||||||||||||||||||||
| type: 'url_relative', | ||||||||||||||||||||||||||||
| reference: urlRelative, | ||||||||||||||||||||||||||||
| async: false, | ||||||||||||||||||||||||||||
| expects: null, | ||||||||||||||||||||||||||||
| requirement: isRelativeUrl, | ||||||||||||||||||||||||||||
| message, | ||||||||||||||||||||||||||||
| '~run'(dataset, config) { | ||||||||||||||||||||||||||||
| if (dataset.typed && !this.requirement(dataset.value)) { | ||||||||||||||||||||||||||||
| _addIssue(this, 'URL', dataset, config); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return dataset; | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
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.
P1: Absolute URLs preceded by URL-parser-trimmed whitespace are accepted, violating the scheme rejection guarantee. Check for a scheme after leading parser-trimmed whitespace before resolving the reference.
Prompt for AI agents