-
-
Notifications
You must be signed in to change notification settings - Fork 369
feat: RFC 3339 duration Validator #1554
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
Open
hazrid93
wants to merge
1
commit into
open-circle:main
Choose a base branch
from
hazrid93:hazrid93/27_1497
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './isoDuration.ts'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||
| import type { | ||||||
| BaseIssue, | ||||||
| BaseValidation, | ||||||
| ErrorMessage, | ||||||
| } from '../../types/index.ts'; | ||||||
| import { _addIssue } from '../../utils/index.ts'; | ||||||
|
|
||||||
| /** | ||||||
| * The ISO 8601 / RFC 3339 duration regex. Matches strings like `P1D`, | ||||||
| * `PT5H30M`, `P1Y2M3DT4H5M6S`, with optional decimal values. A bare `P` or `PT` | ||||||
| * (no components) is rejected. See issue #1497. | ||||||
| */ | ||||||
| const ISO_DURATION_REGEX: RegExp = | ||||||
| /^P(?!T?$)(?:(\d+(?:\.\d+)?)Y)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)W)?(?:(\d+(?:\.\d+)?)D)?(?:T(?:(\d+(?:\.\d+)?)H)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)S)?)?$/; | ||||||
|
|
||||||
|
Comment on lines
+14
to
+15
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: Regex accepts Prompt for AI agents
Suggested change
|
||||||
| /** | ||||||
| * ISO duration issue interface. | ||||||
| */ | ||||||
| export interface IsoDurationIssue<TInput extends string> | ||||||
| extends BaseIssue<TInput> { | ||||||
| readonly kind: 'validation'; | ||||||
| readonly type: 'iso_duration'; | ||||||
| readonly expected: null; | ||||||
| readonly received: `"${string}"`; | ||||||
| readonly requirement: RegExp; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * ISO duration action interface. | ||||||
| */ | ||||||
| export interface IsoDurationAction< | ||||||
| TInput extends string, | ||||||
| TMessage extends ErrorMessage<IsoDurationIssue<TInput>> | undefined, | ||||||
| > extends BaseValidation<TInput, TInput, IsoDurationIssue<TInput>> { | ||||||
| readonly type: 'iso_duration'; | ||||||
| readonly reference: typeof isoDuration; | ||||||
| readonly expects: null; | ||||||
| readonly requirement: RegExp; | ||||||
| readonly message: TMessage; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) / | ||||||
| * [RFC 3339](https://www.rfc-editor.org/rfc/rfc3339) duration validation action. | ||||||
| * | ||||||
| * Format: `P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S` (e.g. `P1D`, `PT5H30M`, | ||||||
| * `P1Y2M3DT4H5M6S`). Decimal values are supported. A bare `P` or `PT` with no | ||||||
| * components is rejected. See issue #1497. | ||||||
| * | ||||||
| * @returns An ISO duration action. | ||||||
| */ | ||||||
| export function isoDuration<TInput extends string>(): IsoDurationAction< | ||||||
| TInput, | ||||||
| undefined | ||||||
| >; | ||||||
|
|
||||||
| /** | ||||||
| * Creates an ISO duration validation action. | ||||||
| * | ||||||
| * @param message The error message. | ||||||
| * | ||||||
| * @returns An ISO duration action. | ||||||
| */ | ||||||
| export function isoDuration< | ||||||
| TInput extends string, | ||||||
| const TMessage extends ErrorMessage<IsoDurationIssue<TInput>> | undefined, | ||||||
| >(message: TMessage): IsoDurationAction<TInput, TMessage>; | ||||||
|
|
||||||
| // @__NO_SIDE_EFFECTS__ | ||||||
| export function isoDuration( | ||||||
| message?: ErrorMessage<IsoDurationIssue<string>>, | ||||||
| ): IsoDurationAction<string, ErrorMessage<IsoDurationIssue<string>> | undefined> { | ||||||
| return { | ||||||
| kind: 'validation', | ||||||
| type: 'iso_duration', | ||||||
| reference: isoDuration, | ||||||
| async: false, | ||||||
| expects: null, | ||||||
| requirement: ISO_DURATION_REGEX, | ||||||
| message, | ||||||
| '~run'(dataset, config) { | ||||||
| if (dataset.typed && !this.requirement.test(dataset.value)) { | ||||||
| _addIssue(this, 'duration', dataset, config); | ||||||
| } | ||||||
| return dataset; | ||||||
| }, | ||||||
| }; | ||||||
| } | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 | 🟡 Minor | ⚡ Quick win
Prevent the
Tdesignator from matching when time components are absent.In the current
ISO_DURATION_REGEX, the time components (H,M,S) are fully optional inside theTgroup(?:T(?:...H)?(?:...M)?(?:...S)?)?. This allows invalid durations likeP1YTto pass validation, asTcan be matched with nothing following it. According to the ISO 8601 standard, if there are no time components, theTdesignator must be absent.Adding a positive lookahead
(?=\d)immediately after theTensures it is only matched when followed by a valid time component (which must start with a digit).💚 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents