What is cron-validator?
The cron-validator npm package is used to validate and parse cron expressions. It ensures that cron expressions are syntactically correct and can be used to schedule tasks in a reliable manner.
What are cron-validator's main functionalities?
Validate Cron Expression
This feature allows you to validate a cron expression to check if it is syntactically correct. The `isValidCron` function returns a boolean indicating whether the provided cron expression is valid.
const cronValidator = require('cron-validator');
const isValid = cronValidator.isValidCron('0 0 * * *');
console.log(isValid); // true
Custom Validation Options
This feature allows you to validate a cron expression with custom options. For example, you can specify whether to include seconds in the cron expression or allow blank days.
const cronValidator = require('cron-validator');
const options = { seconds: true, allowBlankDay: true };
const isValid = cronValidator.isValidCron('0 0 0 * * *', options);
console.log(isValid); // true
Parse Cron Expression
This feature allows you to parse a cron expression into its individual components. The `parseCron` function returns an object with the parsed components of the cron expression.
const cronValidator = require('cron-validator');
const parsed = cronValidator.parseCron('0 0 * * *');
console.log(parsed); // { minutes: '0', hours: '0', dayOfMonth: '*', months: '*', dayOfWeek: '*' }
Other packages similar to cron-validator
cron-parser
The cron-parser package is used to parse and manipulate cron expressions. It provides more advanced features such as generating the next execution time for a given cron expression. Compared to cron-validator, cron-parser focuses more on parsing and scheduling rather than just validation.
node-cron
The node-cron package is a task scheduler in pure JavaScript for node.js based on cron syntax. It allows you to schedule tasks using cron expressions and provides validation as part of its scheduling functionality. While cron-validator focuses solely on validation, node-cron provides a complete scheduling solution.
cron
The cron package is a cron job scheduler for node.js. It allows you to schedule jobs using cron expressions and provides validation as part of its job scheduling functionality. Similar to node-cron, it offers a more comprehensive solution for scheduling tasks compared to the validation-focused cron-validator.
Cron Validator
Cron Validator is a util that allows you to validate a cron expression, similar to what crontab guru does, but in your code base.
Alternatives
- cron-validate:
It has more features and configuration options to restrict ranges, or create presets of configs. It includes an AWS preset that should match AWS Schedule Expressions.
Installation
npm install cron-validator
Usage
Require syntax:
const cron = require('cron-validator');
if (cron.isValidCron('* * * * *')) {
}
Or import syntax with TypeScript:
import { isValidCron } from 'cron-validator'
if (isValidCron('* * * * *')) {
}
Support for seconds can be enabled by passing the seconds
flag as true in options:
const cron = require('cron-validator');
cron.isValidCron('* * * * * *');
cron.isValidCron('* * * * * *', { seconds: true });
The same goes to enable the alias
support for months and weekdays:
const cron = require('cron-validator');
cron.isValidCron('* * * * mon');
cron.isValidCron('* * * * mon', { alias: true });
Likewise, the allowBlankDay
flag can be enabled to mark days or weekdays blank with a ?
symbol:
const cron = require('cron-validator');
cron.isValidCron('* * * * ?');
cron.isValidCron('* * * * ?', { allowBlankDay: true });
The allowSevenAsSunday
flag can be enabled to enable support for digit 7 as Sunday:
const cron = require('cron-validator');
cron.isValidCron('* * * * 7');
cron.isValidCron('* * * * 7', { allowSevenAsSunday: true });
Features
Motivations
Many great cron libraries already exists on NPM, why this one?
Libraries like node-cron are primarily made to schedule jobs using a cron expression, not validate those cron expressions. They come with additional behaviors not always required. They also bring their own set of defaults which might be in conflicts with the defaults of other external systems. We needed something to validate an expression before sending it off to an external system, so we created this to be a little more strict and configurable, with a more specific behavior.
We decided to go for the naive approach first, which results in lenghty code and tests, but also making it easier to reason about cron expressions and their specific rules.