A cron-expression validator for TypeScript/JavaScript projects.
Installations
npm install cron-validate
Developer
Airfooox
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
16.15.1
NPM Version
8.19.2
Statistics
72 Stars
398 Commits
12 Forks
2 Watching
30 Branches
6 Contributors
Updated on 09 Sept 2024
Bundle Size
77.49 kB
Minified
22.38 kB
Minified + Gzipped
Languages
TypeScript (99.79%)
JavaScript (0.21%)
Total Downloads
Cumulative downloads
Total Downloads
14,006,548
Last day
44.7%
114,208
Compared to previous day
Last week
12.7%
491,919
Compared to previous week
Last month
10.6%
1,780,738
Compared to previous month
Last year
139.6%
7,835,217
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
21
cron-validate
Cron-validate is a cron-expression validator written in TypeScript. The validation options are customizable and cron fields like seconds and years are supported.
Installation
Pacakge is available on npm:
npm install -S cron-validate
Usage
Basic usage
1import cron from 'cron-validate' 2 3const cronResult = cron('* * * * *') 4if (cronResult.isValid()) { 5 // !cronResult.isError() 6 // valid code 7} else { 8 // error code 9}
Result system
The cron
function returns a Result-type, which is either Valid<T, E>
or Err<T, E>
.
For checking the returned result, just use result.isValid()
or result.isError()
Both result types contain values:
1import cron from 'cron-validate' 2 3const cronResult = cron('* * * * *') 4if (cronResult.isValid()) { 5 const validValue = cronResult.getValue() 6 7 // The valid value is a object containing all cron fields 8 console.log(validValue) 9 // In this case, it would be: 10 // { seconds: undefined, minutes: '*', hours: '*', daysOfMonth: '*', months: '*', daysOfWeek: '*', years: undefiend } 11} else { 12 const errorValue = cronResult.getError() 13 14 // The error value contains an array of strings, which represent the cron validation errors. 15 console.log(errorValue) // string[] of error messages 16}
Make sure to test the result type beforehand, because getValue()
only works on Valid
and getError()
only works on Err
. If you don't check, it will throw an error.
For further information, you can check out https://github.com/gDelgado14/neverthrow, because I used and modified his code for this package. (Therefor not every documented function on his package is available on this package.)
Options / Configuration
To configure the validator, cron-validate uses a preset system. There are already defined presets (default, npm-node-cron or aws), but you can also define your own preset to use for your system. You can also use the override property to set certain option on single cron validates.
Presets
The following presets are already defined by cron-validate:
- default (see: http://crontab.org/)
- npm-node-cron (see: https://github.com/kelektiv/node-cron)
- aws-cloud-watch (see: https://docs.aws.amazon.com/de_de/AmazonCloudWatch/latest/events/ScheduledEvents.html)
- npm-cron-schedule (see: https://github.com/P4sca1/cron-schedule)
To select a preset for your validation, you can simply do this:
1cron('* * * * *', { 2 preset: 'npm-cron-schedule', 3})
Defining and using your own preset
To define your own preset, use this:
1registerOptionPreset('YOUR-PRESET-ID', { 2 presetId: 'YOUR-PRESET-ID', 3 useSeconds: false, 4 useYears: false, 5 useAliases: false, // optional, default to false 6 useBlankDay: false, 7 allowOnlyOneBlankDayField: false, 8 mustHaveBlankDayField: false, // optional, default to false 9 useLastDayOfMonth: false, // optional, default to false 10 useLastDayOfWeek: false, // optional, default to false 11 useNearestWeekday: false, // optional, default to false 12 useNthWeekdayOfMonth: false, // optional, default to false 13 seconds: { 14 minValue: 0, 15 maxValue: 59, 16 lowerLimit: 0, // optional, default to minValue 17 upperLimit: 59, // optional, default to maxValue 18 }, 19 minutes: { 20 minValue: 0, 21 maxValue: 59, 22 lowerLimit: 0, // optional, default to minValue 23 upperLimit: 59, // optional, default to maxValue 24 }, 25 hours: { 26 minValue: 0, 27 maxValue: 23, 28 lowerLimit: 0, // optional, default to minValue 29 upperLimit: 23, // optional, default to maxValue 30 }, 31 daysOfMonth: { 32 minValue: 1, 33 maxValue: 31, 34 lowerLimit: 1, // optional, default to minValue 35 upperLimit: 31, // optional, default to maxValue 36 }, 37 months: { 38 minValue: 0, 39 maxValue: 12, 40 lowerLimit: 0, // optional, default to minValue 41 upperLimit: 12, // optional, default to maxValue 42 }, 43 daysOfWeek: { 44 minValue: 1, 45 maxValue: 7, 46 lowerLimit: 1, // optional, default to minValue 47 upperLimit: 7, // optional, default to maxValue 48 }, 49 years: { 50 minValue: 1970, 51 maxValue: 2099, 52 lowerLimit: 1970, // optional, default to minValue 53 upperLimit: 2099, // optional, default to maxValue 54 }, 55})
The preset properties explained:
presetId: string
- same id as in first function parameter
useSeconds: boolean
- enables seconds field in cron expression
useYears: boolean
- enables years field in cron expression
useAliases: boolean
- enables aliases for month and daysOfWeek fields (ignores limits for month and daysOfWeek, so be aware of that)
useBlankDay: boolean
- enables blank day notation '?' in daysOfMonth and daysOfWeek field
allowOnlyOneBlankDayField: boolean
- requires a day field to not be blank (so not both day fields can be blank)
mustHaveBlankDayField: boolean
- requires a day field to be blank (so not both day fields are specified)
- when mixed with
allowOnlyOneBlankDayField
, it means that there will always be either day or day of week as?
useLastDayOfMonth: boolean
- enables the 'L' character to specify the last day of the month.
- accept negative offset after the 'L' for nth last day of the month.
- e.g.:
L-2
would me the 2nd to last day of the month.
useLastDayOfWeek: boolean
- enables the 'L' character to specify the last occurrence of a weekday in a month.
- e.g.:
5L
would mean the last friday of the month.
useNearestWeekday: boolean
- enables the 'W' character to specify the use of the closest weekday.
- e.g.:
15W
would mean the weekday (mon-fri) closest to the 15th when the 15th is on sat-sun.
useNthWeekdayOfMonth: boolean
- enables the '#' character to specify the Nth weekday of the month.
- e.g.:
6#3
would mean the 3rd friday of the month (assuming 6 = friday).
- in cron fields (like seconds, minutes etc.):
minValue: number
- minimum value of your cron interpreter (like npm-node-cron only supports 0-6 for weekdays)
- can't be set as override
maxValue: number
- minimum value of your cron interpreter (like npm-node-cron only supports 0-6 for weekdays)
- can't be set as override
lowerLimit?: number
- lower limit for validation
- equal or greater than minValue
- if not set, default to minValue
upperLimit?: number
- upper limit for validation
- equal or lower than maxValue
- if not set, defaults to maxValue
Override preset options
If you want to override a option for single cron validations, you can use the override
property:
1console.log(cron('* * * * * *', { 2 preset: 'default' // second field not supported in default preset 3 override: { 4 useSeconds: true // override preset option 5 } 6})) 7 8console.log(cron('* 10-20 * * * *', { 9 preset: 'default' 10 override: { 11 minutes: { 12 lowerLimit: 10, // override preset option 13 upperLimit: 20 // override preset option 14 } 15 } 16}))
Examples
1import cron from 'cron-validate' 2 3console.log(cron('* * * * *').isValid()) // true 4 5console.log(cron('* * * * *').isError()) // false 6 7console.log(cron('* 2,3,4 * * *').isValid()) // true 8 9console.log(cron('0 */2 */5 * *').isValid()) // true 10 11console.log(cron('* * * * * *', { override: { useSeconds: true } }).isValid()) // true 12 13console.log(cron('* * * * * *', { override: { useYears: true } }).isValid()) // true 14 15console.log( 16 cron('30 * * * * *', { 17 override: { 18 useSeconds: true, 19 seconds: { 20 lowerLimit: 20, 21 upperLimit: 40, 22 }, 23 }, 24 }).isValid() 25) // true 26 27console.log( 28 cron('* 3 * * *', { 29 override: { 30 hours: { 31 lowerLimit: 0, 32 upperLimit: 2, 33 }, 34 }, 35 }).isValid() 36) // false 37 38console.log( 39 cron('* * ? * *', { 40 override: { 41 useBlankDay: true, 42 }, 43 }).isValid() 44) // true 45 46console.log( 47 cron('* * ? * ?', { 48 override: { 49 useBlankDay: true, 50 allowOnlyOneBlankDayField: true, 51 }, 52 }).isValid() 53) // false
(Planned) Features
- Basic cron validation.
- Error messenges with information about invalid cron expression.
- Seconds field support.
- Years field support.
- Option presets (classic cron, node-cron, etc.)
- Blank '?' daysOfMonth/daysOfWeek support
- Last day of month.
- Last specific weekday of month. (e.g. last Tuesday)
- Closest weekday to a specific day of the month.
- Nth specific weekday of month. (e.g. 2nd Tuesday)
- Cron alias support.
Contributors
Used by:
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 1/4 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 28 are checked with a SAST tool
Reason
11 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More