Gathering detailed insights and metrics for cron-validate
Gathering detailed insights and metrics for cron-validate
Gathering detailed insights and metrics for cron-validate
Gathering detailed insights and metrics for cron-validate
A cron-expression validator for TypeScript/JavaScript projects.
npm install cron-validate
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
72 Stars
398 Commits
12 Forks
2 Watching
30 Branches
6 Contributors
Updated on 09 Sept 2024
Minified
Minified + Gzipped
TypeScript (99.79%)
JavaScript (0.21%)
Cumulative downloads
Total Downloads
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
1
21
Cron-validate is a cron-expression validator written in TypeScript. The validation options are customizable and cron fields like seconds and years are supported.
Pacakge is available on npm:
npm install -S cron-validate
1import cron from 'cron-validate' 2 3const cronResult = cron('* * * * *') 4if (cronResult.isValid()) { 5 // !cronResult.isError() 6 // valid code 7} else { 8 // error code 9}
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.)
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.
The following presets are already defined by cron-validate:
To select a preset for your validation, you can simply do this:
1cron('* * * * *', { 2 preset: 'npm-cron-schedule', 3})
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
useSeconds: boolean
useYears: boolean
useAliases: boolean
useBlankDay: boolean
allowOnlyOneBlankDayField: boolean
mustHaveBlankDayField: boolean
allowOnlyOneBlankDayField
, it means that there will always be either day or day of week as ?
useLastDayOfMonth: boolean
L-2
would me the 2nd to last day of the month.useLastDayOfWeek: boolean
5L
would mean the last friday of the month.useNearestWeekday: boolean
15W
would mean the weekday (mon-fri) closest to the 15th when the 15th is on sat-sun.useNthWeekdayOfMonth: boolean
6#3
would mean the 3rd friday of the month (assuming 6 = friday).minValue: number
maxValue: number
lowerLimit?: number
upperLimit?: number
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}))
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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
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
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
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