Skip to content

Commit

Permalink
refactor: rename isCronTimeValid to validateCronExpression and update…
Browse files Browse the repository at this point in the history
… usage examples
  • Loading branch information
WillianAgostini committed Feb 24, 2025
1 parent de1e0ee commit 158fcaa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,16 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
console.log(`The job would run in ${timeout}ms`);
```

- `isCronTimeValid`: Validates if a given cron expression is valid (returns a boolean).
- `validateCronExpression`: Validates if a given cron expression is valid (returns an object with `valid` and `error` properties).

```javascript
import * as cron from 'cron';

const isValid = cron.isCronTimeValid('0 0 * * *');
console.log(`Is the cron expression valid? ${isValid}`);
const validation = cron.validateCronExpression('0 0 * * *');
console.log(`Is the cron expression valid? ${validation.valid}`);
if (!validation.valid) {
console.error(`Validation error: ${validation.error}`);
}
```

### CronJob Class
Expand Down
4 changes: 2 additions & 2 deletions examples/is_crontime_valid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { isCronTimeValid } from '../src';
import { validateCronExpression } from '../src';

console.log('is valid? ', isCronTimeValid('NOT VALID'));
console.log('is valid? ', validateCronExpression('NOT VALID').valid);
11 changes: 1 addition & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,4 @@ export const sendAt = (cronTime: string | Date | DateTime): DateTime =>
export const timeout = (cronTime: string | Date | DateTime): number =>
new CronTime(cronTime).getTimeout();

export const isCronTimeValid = (
cronTime: string | Date | DateTime
): boolean => {
try {
new CronTime(cronTime);
return true;
} catch (_) {
return false;
}
};
export const validateCronExpression = CronTime.validateCronExpression;
17 changes: 17 additions & 0 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ export class CronTime {
}
}

static validateCronExpression(cronExpression: string): {
valid: boolean;
error?: CronError;
} {
try {
new CronTime(cronExpression);
return {
valid: true
};
} catch (error: any) {
return {
valid: false,
error
};
}
}

private _getWeekDay(date: DateTime) {
return date.weekday === 7 ? 0 : date.weekday;
}
Expand Down
20 changes: 10 additions & 10 deletions tests/crontime.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DateTime } from 'luxon';
import sinon from 'sinon';
import { CronTime, isCronTimeValid } from '../src';
import { CronTime, validateCronExpression } from '../src';

describe('crontime', () => {
// eslint-disable-next-line jest/no-standalone-expect
Expand Down Expand Up @@ -779,18 +779,18 @@ describe('crontime', () => {
});
});

describe('isCronTimeValid', () => {
describe('validateCronExpression', () => {
it('should return true for valid cron expressions', () => {
expect(isCronTimeValid('* * * * *')).toBe(true);
expect(isCronTimeValid('0 0 * * *')).toBe(true);
expect(isCronTimeValid('0 0 1 1 *')).toBe(true);
expect(isCronTimeValid('*/5 * * * *')).toBe(true);
expect(validateCronExpression('* * * * *').valid).toBe(true);
expect(validateCronExpression('0 0 * * *').valid).toBe(true);
expect(validateCronExpression('0 0 1 1 *').valid).toBe(true);
expect(validateCronExpression('*/5 * * * *').valid).toBe(true);
});

it('should return false for invalid cron expressions', () => {
expect(isCronTimeValid('* * * *')).toBe(false);
expect(isCronTimeValid('60 * * * *')).toBe(false);
expect(isCronTimeValid('* * * * * * *')).toBe(false);
expect(isCronTimeValid('invalid cron')).toBe(false);
expect(validateCronExpression('* * * *').valid).toBe(false);
expect(validateCronExpression('60 * * * *').valid).toBe(false);
expect(validateCronExpression('* * * * * * *').valid).toBe(false);
expect(validateCronExpression('invalid cron').valid).toBe(false);
});
});

0 comments on commit 158fcaa

Please sign in to comment.