Skip to content

Commit

Permalink
feat: add isCronTimeValid function to validate cron expressions (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillianAgostini authored Feb 24, 2025
1 parent fa08aa3 commit cbd8106
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
console.log(`The job would run in ${timeout}ms`);
```

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

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

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

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

try {
new CronJob('NOT VALID', () => {
console.log("shouldn't get printed");
});
} catch (e) {
console.log('omg err', e);
}
console.log('is valid? ', validateCronExpression('NOT VALID').valid);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export const sendAt = (cronTime: string | Date | DateTime): DateTime =>

export const timeout = (cronTime: string | Date | DateTime): number =>
new CronTime(cronTime).getTimeout();

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
35 changes: 34 additions & 1 deletion tests/crontime.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DateTime } from 'luxon';
import sinon from 'sinon';
import { CronTime } from '../src';
import { CronTime, validateCronExpression } from '../src';
import { CronError } from '../src/errors';

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

describe('validateCronExpression', () => {
it('should return true for valid cron expressions', () => {
const validExpressions = [
'* * * * *',
'0 0 * * *',
'0 0 1 1 *',
'*/5 * * * *'
];

validExpressions.forEach(expression => {
const validation = validateCronExpression(expression);
expect(validation.valid).toBe(true);
expect(validation.error).toBeUndefined();
});
});

it('should return false for invalid cron expressions', () => {
const invalidExpressions = [
'* * * *',
'60 * * * *',
'* * * * * * *',
'invalid cron'
];

invalidExpressions.forEach(expression => {
const validation = validateCronExpression(expression);
expect(validation.valid).toBe(false);
expect(validation.error).toBeInstanceOf(CronError);
});
});
});

0 comments on commit cbd8106

Please sign in to comment.