( cronExpression: string, timezone?: string )
| 13 | * @returns Validation result with isValid flag, error message, and next run date |
| 14 | */ |
| 15 | export function validateCronExpression( |
| 16 | cronExpression: string, |
| 17 | timezone?: string |
| 18 | ): { |
| 19 | isValid: boolean |
| 20 | error?: string |
| 21 | nextRun?: Date |
| 22 | } { |
| 23 | if (!cronExpression?.trim()) { |
| 24 | return { |
| 25 | isValid: false, |
| 26 | error: 'Cron expression cannot be empty', |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | try { |
| 31 | const cron = new Cron(cronExpression, timezone ? { timezone } : undefined) |
| 32 | const nextRun = cron.nextRun() |
| 33 | |
| 34 | if (!nextRun) { |
| 35 | return { |
| 36 | isValid: false, |
| 37 | error: 'Cron expression produces no future occurrences', |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return { |
| 42 | isValid: true, |
| 43 | nextRun, |
| 44 | } |
| 45 | } catch (error) { |
| 46 | return { |
| 47 | isValid: false, |
| 48 | error: getErrorMessage(error, 'Invalid cron expression syntax'), |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** Upper bound on how many excluded occurrences the next-run search skips before giving up. */ |
| 54 | const MAX_OCCURRENCE_SKIP = 1000 |
no test coverage detected