Gathering detailed insights and metrics for @lobaro/lobaro-cron-validate
Gathering detailed insights and metrics for @lobaro/lobaro-cron-validate
Gathering detailed insights and metrics for @lobaro/lobaro-cron-validate
Gathering detailed insights and metrics for @lobaro/lobaro-cron-validate
A cron-expression validator with options for the lobaro Device Cron Expressions. For the use in TypeScript/JavaScript projects.
npm install @lobaro/lobaro-cron-validate
Typescript
Module System
Node Version
NPM Version
TypeScript (99.81%)
JavaScript (0.19%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
425 Commits
1 Watchers
4 Branches
3 Contributors
Updated on Jun 17, 2025
Latest Version
1.0.0
Package Id
@lobaro/lobaro-cron-validate@1.0.0
Unpacked Size
68.65 kB
Size
15.37 kB
File Count
30
NPM Version
6.14.9
Node Version
14.15.3
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
22
Cron-validate is a cron-expression validator written in TypeScript. This is a fork with adaptions to validate Lobaro Device Crons The validation options are customizable and cron fields like seconds and years are supported. The fork aims to support all options from the original repo and to amend whats needed for the Lobaro Crons
H,L,W
/lib
to repo and skip release, requires npm run build
to update compiled files in /lib
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.
No security vulnerabilities found.