Updated April 15, 2023
Introduction to Cron validator
Cron validator is used for checking the format of the expression specified for creating a cron job or manipulating the same. Cron jobs are the tasks that are scheduled for automatic execution periodically based on the time interval we have specified in Linux platforms. We can perform a check whether the stated cron job command expression is proper or not by using the cron validator. In this article, we will have a look at what is the ideal format of the cron expression or instruction, overview of cron validator, its applications, features, and also look at its implementation with the help of certain examples.
What is cron validator?
The cron validator is the module of a node that helps in determining whether the specified quartz cron expressions is correct or not and also comes with some error messages if any and provided if we have specified to do so. The cron expression consists of seven to six parameters or fields related to date and time and is separated from each other by using white spaces which help in specifying the periodical parameters of the scheduled job.
The format or syntax of cron job expression is as shown below.
Seconds minutes hours days months weekdays years
The last field of year is optional in nature. Some of the valid examples of cron jobs are as shown below –
0 0 6 * * ? *
The above cron job expression is scheduled for execution every day at 6 am in the morning.
The following tables illustrate the complete list of the values that can be specified for each of the filed in cron expression –
Parameter or field | Allowed values | Wildcard symbols |
Year | 1970 to 2199 | * . – / |
Hours | 0 to 23 | * . – / |
Days | 1 to 31 | * . – / ? L W |
Months | JAN to DEC or 1 to 12 | * . – / |
Weekdays | SUN to SAT or 1 to 7 | * . – L ? # |
Minutes | 0 to 59 | * . – / |
In order to understand the cron validator, it is very important to understand the quartz because the cron validator node module helps in checking and validating only quartz cron expressions. Quartz is the library used for scheduling and is completely open-source and can be integrated with other applications written using Java programming language. This is very useful while executing large-scale jobs which are complex or simple in nature.
In order to find out the official reference of where we can find the cron node module, you can refer to this site.
Usage cron validator
The method provided by the cron validator module named isValidCronExpression is taken in the parameter of string specifying the cron expression and gives the output containing the Boolean value of true or false stating whether the expression is valid or not. The usage of cron validator can be done as follows –
var sampleEDUCBAvalidator = require ("cron-expression-validator");
var whetherValid = sampleEDUCBAvalidator. isValidCronExpression("0 0 6 * * ? *");
The output of the above code snippet is as shown below –
You can also use this method inside the if condition as shown below and perform the execution of certain codes on conditional basis. If the condition becomes true that means it is a valid expression then the code block will be executed else not.
If (sampleEDUCBAvalidator. isValidCronExpression("0 0 6 * * ? *")) {
// Code to execute if cron expression is valid
}
The use of a cron validator can also be done along with the additional parameter passing for the error messages that will help in retrieving the exact reason for error occurrence. For example, we can add the error: true in JSON format in the second parameter in if condition –
If (sampleEDUCBAvalidator. isValidCronExpression("0 0 66/2 * * ? *", {error : true})) {
/* Code will return the object containing isValid key with false value and errorMessage key with the message saying Hour value must have value between 0 to 23 */
}
Features
The features of the cron expression validator are as listed below –
- We can schedule the task.
- The expressions of cron can be matched with a particular date and time value.
- Validation of the specified cron expression.
- Generation of the matching date and time values between the two specified date-time values.
Run Tests
The cron expressions can be tested by using cron testers where we can simply enter the cron expression in the provided field of definition and test it. Thereafter, the number of iterations and the dates and timings on which the cron job will be executed will be listed there itself. The cron tester
Cron Validator Examples
The examples for cron validators can range from using it to execute certain code on the conditional basis of cron expression validation or parsing the expression to either get valid or invalid in a particular variable or even for matching date and time values.
Let us consider some examples. Suppose that we have to print a message in the java program on the cron job validation. The cron expression is
var sampleValidator = require ("cron-expression-validator");
If (sampleValidator. isValidCronExpression("0 0 8 * * ? *")) {
print (" The specified cron expression is valid.");
}
The output of execution of above code snippet is as shown below –
Let us consider one more example where we will try to execute a cron expression which is not in the valid format. We have one expression * * * * * 456/986 and we will pass the error parameter as true so that the error is displayed when we run the isValidCronExpression method. The code snippet will be as shown below –
var sampleValidator = require ("cron-expression-validator");
If (sampleValidator. isValidCronExpression("* * * * * 456/986", {error : true})) {
// some code to execute is expression is valid
}
The output of above code is as shown below –
Conclusion
Cron validator is the module of node available for checking whether the entered expression is correct or not. This can also be used for various other scenarios specified in the above article.
Recommended Articles
We hope that this EDUCBA information on “Cron validator” was beneficial to you. You can view EDUCBA’s recommended articles for more information.