Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/heavy-times-turn.md
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
69 changes: 68 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $ npm install @offload-project/rrule
**RRule:**

```js
import { datetime, RRule, RRuleSet, rrulestr } from 'rrule'
import { datetime, RRule, RRuleSet, rrulestr, validate } from 'rrule'

Copilot AI Feb 22, 2026

Copy link

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.

Suggested change
import { datetime, RRule, RRuleSet, rrulestr, validate } from 'rrule'
import { datetime, RRule, RRuleSet, rrulestr, validate } from '@offload-project/rrule'

Copilot uses AI. Check for mistakes.

// Create a rule:
const rule = new RRule({
Expand Down Expand Up @@ -160,6 +160,19 @@ rrulestr(
)
```

**validate:**

```js
import { validate } from '@offload-project/rrule'

// Check if a string is a valid rule or ruleset
validate('RRULE:FREQ=WEEKLY;COUNT=3')
// { valid: true }

validate('RRULE:FREQ=BOGUS')
// { valid: false, error: { message: 'Invalid frequency: ...', cause: Error } }
```

### Important: Use UTC dates

Dates in JavaScript are tricky. `RRule` tries to support as much flexibility as possible without adding any large
Expand Down Expand Up @@ -743,6 +756,60 @@ Additionally, it accepts the following keyword arguments:

---

#### `validate` Function

```js
validate(rruleStr[, options])
```

Validates an RRULE or RRuleSet string without throwing. Accepts the same string formats and options as `rrulestr`.

Returns a `ValidationResult`:

```js
import { validate } from '@offload-project/rrule'

// Valid single rule
validate('RRULE:FREQ=WEEKLY;COUNT=3')
// { valid: true }

// Valid ruleset string
validate(
'DTSTART:19970902T090000Z\n' +
'RRULE:FREQ=YEARLY;COUNT=6;BYDAY=TU,TH\n' +
'EXDATE:19970911T090000Z'
)
// { valid: true }

// Invalid input
validate('RRULE:FREQ=BOGUS')
// { valid: false, error: { message: 'Invalid frequency: ...', cause: Error } }

// With options (same as rrulestr options)
validate('RRULE:FREQ=DAILY', { dtstart: new Date('1997-09-02T09:00:00Z') })
// { valid: true }
```

The result type is a discriminated union:

```ts
interface ValidationSuccess {
valid: true
}

interface ValidationError {
valid: false
error: {
message: string // Human-readable error description
cause?: Error // Original error object with stack trace
}
}

type ValidationResult = ValidationSuccess | ValidationError
```

---

### Differences From iCalendar RFC

- `RRule` has no `byday` keyword. The equivalent keyword has been replaced by the `byweekday` keyword, to remove the
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export { RRule } from './rrule';
export { RRuleBase } from './rrulebase';
export { RRuleSet } from './rruleset';
export { type ByWeekday, Frequency, type Options } from './types';
export { type ValidationError, type ValidationResult, type ValidationSuccess, validate } from './validate';
export { ALL_WEEKDAYS, Weekday, type WeekdayStr } from './weekday';
31 changes: 31 additions & 0 deletions src/validate.ts
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,
},
};
}
}
108 changes: 108 additions & 0 deletions test/validate.test.ts
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);
}
});
});
});