forked from jkbrzt/rrule
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: rrule and ruleset validate #8
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@offload-project/rrule": minor | ||
| --- | ||
|
|
||
| Add `validate` function for checking RRULE and RRuleSet strings without throwing. | ||
|
|
||
| - New `validate(s, options?)` function that returns `{ valid: true }` or `{ valid: false, error: { message, cause } }` | ||
| - Accepts the same string formats and options as `rrulestr` | ||
| - Exported `ValidationResult`, `ValidationSuccess`, and `ValidationError` types |
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
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,31 @@ | ||
| import { type RRuleStrOptions, rrulestr } from './parse/rrulestr'; | ||
|
|
||
| export interface ValidationSuccess { | ||
| valid: true; | ||
| } | ||
|
|
||
| export interface ValidationError { | ||
| valid: false; | ||
| error: { | ||
| message: string; | ||
| cause?: Error; | ||
| }; | ||
| } | ||
|
|
||
| export type ValidationResult = ValidationSuccess | ValidationError; | ||
|
|
||
| export function validate(s: string, options?: Partial<RRuleStrOptions>): ValidationResult { | ||
| try { | ||
| rrulestr(s, options); | ||
| return { valid: true }; | ||
| } catch (e) { | ||
| const cause = e instanceof Error ? e : new Error(String(e)); | ||
| return { | ||
| valid: false, | ||
| error: { | ||
| message: cause.message, | ||
| cause, | ||
| }, | ||
| }; | ||
| } | ||
| } |
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,108 @@ | ||
| import { validate } from '../src'; | ||
|
|
||
| describe('validate', () => { | ||
| describe('valid inputs', () => { | ||
| it('validates a simple RRULE string', () => { | ||
| const result = validate('FREQ=YEARLY;COUNT=3'); | ||
| expect(result.valid).toBe(true); | ||
| }); | ||
|
|
||
| it('validates an RRULE with DTSTART', () => { | ||
| const result = validate('DTSTART:19970902T090000Z\nRRULE:FREQ=YEARLY;COUNT=3'); | ||
| expect(result.valid).toBe(true); | ||
| }); | ||
|
|
||
| it('validates a full ruleset string', () => { | ||
| const result = validate( | ||
| 'DTSTART:19970902T090000Z\n' + | ||
| 'RRULE:FREQ=YEARLY;COUNT=6;BYDAY=TU,TH\n' + | ||
| 'EXRULE:FREQ=YEARLY;COUNT=3;BYDAY=TH\n' + | ||
| 'RDATE:19970904T090000Z\n' + | ||
| 'EXDATE:19970911T090000Z', | ||
| ); | ||
| expect(result.valid).toBe(true); | ||
| }); | ||
|
|
||
| it('validates with TZID', () => { | ||
| const result = validate('DTSTART;TZID=America/New_York:19970902T090000\nRRULE:FREQ=DAILY'); | ||
| expect(result.valid).toBe(true); | ||
| }); | ||
|
|
||
| it('validates with options.dtstart provided', () => { | ||
| const result = validate('RRULE:FREQ=WEEKLY;COUNT=3', { | ||
| dtstart: new Date('1997-09-02T09:00:00Z'), | ||
| }); | ||
| expect(result.valid).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('invalid inputs', () => { | ||
| it('rejects empty string', () => { | ||
| const result = validate(''); | ||
| expect(result.valid).toBe(false); | ||
| if (!result.valid) { | ||
| expect(result.error.message).toContain('Invalid empty string'); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects unsupported property', () => { | ||
| const result = validate('DTSTART:19970902T090000Z\nVTODO:something'); | ||
| expect(result.valid).toBe(false); | ||
| if (!result.valid) { | ||
| expect(result.error.message).toContain('unsupported property'); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects unknown RRULE property', () => { | ||
| const result = validate('RRULE:FREQ=YEARLY;BOGUSPROP=1'); | ||
| expect(result.valid).toBe(false); | ||
| if (!result.valid) { | ||
| expect(result.error.message).toContain("Unknown RRULE property 'BOGUSPROP'"); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects invalid weekday string', () => { | ||
| const result = validate('RRULE:FREQ=WEEKLY;BYDAY=XY'); | ||
| expect(result.valid).toBe(false); | ||
| }); | ||
|
|
||
| it('rejects invalid UNTIL date format', () => { | ||
| const result = validate('RRULE:FREQ=YEARLY;UNTIL=not-a-date'); | ||
| expect(result.valid).toBe(false); | ||
| if (!result.valid) { | ||
| expect(result.error.message).toContain('Invalid UNTIL value'); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects invalid frequency', () => { | ||
| const result = validate('RRULE:FREQ=BOGUS'); | ||
| expect(result.valid).toBe(false); | ||
| if (!result.valid) { | ||
| expect(result.error.message).toContain('Invalid frequency'); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects unsupported RDATE parm', () => { | ||
| const result = validate('DTSTART:19970902T090000Z\nRDATE;BOGUS=1:19970904T090000Z'); | ||
| expect(result.valid).toBe(false); | ||
| if (!result.valid) { | ||
| expect(result.error.message).toContain('unsupported RDATE/EXDATE parm'); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('contract', () => { | ||
| it('never throws', () => { | ||
| expect(() => validate(null as unknown as string)).not.toThrow(); | ||
| expect(() => validate(undefined as unknown as string)).not.toThrow(); | ||
| expect(() => validate(123 as unknown as string)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('returns error.cause as an Error instance', () => { | ||
| const result = validate(''); | ||
| if (!result.valid) { | ||
| expect(result.error.cause).toBeInstanceOf(Error); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
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.
Inconsistent package name in import statement. This line uses
'rrule'while the new validate examples added in lines 166 and 770 use'@offload-project/rrule'. Consider using the full package name'@offload-project/rrule'consistently throughout the documentation, or using the short form'rrule'consistently.