Cron-validate is a cron-expression validator written in TypeScript.
The validation options are customizable and cron fields like seconds and years are supported.
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:
import cron from 'cron-validate'const cronResult = cron('* * * * *')if (cronResult.isValid()) { const validValue = cronResult.getValue() // The valid value is a object containing all cron fields console.log(validValue) // In this case, it would be: // { seconds: undefined, minutes: '*', hours: '*', daysOfMonth: '*', months: '*', daysOfWeek: '*', years: undefiend }} else { const errorValue = cronResult.getError() // The error value contains an array of strings, which represent the cron validation errors. console.log(errorValue) // string[] of error messages}
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: