Introduction to cron-expression-validator node-module

Anusha Ihalapathirana
3 min readMay 4, 2020

what is a cron expression?

Cron is a time-based job scheduler in Unix operating systems. It helps to schedule repetitive jobs to run at fixed times, dates, or intervals.

Cron expression consists of six or seven fields, separated by white space, which describes individual details of the schedule.

Cron expression takes the following format:

<secs> <mins> <hours> <days of month> <months> <days of week> <years>

Note: <year> is an optional field

As I mentioned cron expression represents time.

As an example,

0 0 2 * * ? * represents every day at 02:00 AM

and

0 0 12 1L * ? * represents Every month on the last Sunday, at noon

Fields in cron expression can contain combinations of allowed values and special characters. Allowed values for a field are shown in the following table:

Quartz

Before digging into the cron-expression-validator node-module it is good to know about quartz since the module supports to validate the Quartz engine generated cron expressions.

Quartz is an open-source job scheduling library that can be integrated within virtually any Java application. It can be used to create simple or complex schedules for executing a large amount of jobs. You can find more information about Quartz here

What is cron-expression-validator?

cron-expression-validator is a node-module that validates quartz cron expressions. You can find the cron-expression-validator node-module in here

Installation

You can simply install cron-expression-validator like any other node module. Just type,

npm install cron-expression-validator

What cron-expression-validator do?

  • It provides validation to a given cron expression — returns a boolean value
  • It provides validation and error messages if cron string is invalid — returns Object carrying boolean value and array

Usage

Once you install, include this node-module in your project

var cronValidator = require('cron-expression-validator');

Now you can call the isValidCronExpression method. The isValidCronExpression method requires string (cron expression) as the first parameter and {error: true} as an optional second parameter.

Call isValidCronExpression method with cron string

var isValid = cronValidator.isValidCronExpression("* * * * * ? *"); // isValid -> true
  1. You can pass only cron string to the isValidCronExpression method
  • example 1:
if(cronValidator.isValidCronExpression("* * * * * ? *")) { 
// returns true
// Your code
}
if(cronValidator.isValidCronExpression("* * * * * * *")) {
// returns false
}

2. If you want to get the error messages, pass the optional second parameter { error: true } to the isValidCronExpression method

  • example 2: with {error: true}
var validObj = cronValidator.isValidCronExpression(
"* * * * 25/2 ? *", {error: true});

validObj -> {
isValid: false,
errorMessage: [ ‘Month values must be between 1 and 12’ ]
}

  • example 3: with {error: true}
var validObj = cronValidator.isValidCronExpression(
"* * * ? * * 123/555", {error: true});

validObj -> {
isValid: false,
errorMessage: [
‘(Year)-Unsupported value 123 for field. Possible values are 1970–2099 , -* /’,
‘(Year) -Expression 555 is not a valid increment value. Accepted values are 0-129’
]
}

  • example 4: with {error: false}
var isValid = cronValidator.isValidCronExpression(
"0 0 12 1/2 * ? *", {error: false});

isValid -> true

I hope you found this tutorial helpful! You can also find the cron-exp-lib node-module to create quartz cron expressions in here

--

--