2020-11-10 03:33:01 +03:00
|
|
|
const cronValidate = require('cron-validate');
|
2020-11-05 07:07:27 +03:00
|
|
|
|
|
|
|
/**
|
2020-11-10 03:33:01 +03:00
|
|
|
* Checks if expression follows supported crontab format
|
|
|
|
* reference: https://www.adminschoice.com/crontab-quick-reference
|
|
|
|
* builder: https://crontab.guru/
|
|
|
|
*
|
2020-11-05 07:07:27 +03:00
|
|
|
* e.g.:
|
|
|
|
* "2 * * * *" where:
|
|
|
|
*
|
|
|
|
* "* * * * * *"
|
|
|
|
* ┬ ┬ ┬ ┬ ┬ ┬
|
|
|
|
* │ │ │ │ │ |
|
|
|
|
* │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
|
|
|
|
* │ │ │ │ └───── month (1 - 12)
|
|
|
|
* │ │ │ └────────── day of month (1 - 31)
|
|
|
|
* │ │ └─────────────── hour (0 - 23)
|
|
|
|
* │ └──────────────────── minute (0 - 59)
|
|
|
|
* └───────────────────────── second (0 - 59, optional)
|
|
|
|
*
|
2020-11-10 03:33:01 +03:00
|
|
|
* @param {String} expression in crontab format (https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html)
|
2020-11-05 07:07:27 +03:00
|
|
|
*
|
|
|
|
* @returns {boolean} wheather or not the expression is valid
|
|
|
|
*/
|
|
|
|
const isCronExpression = (expression) => {
|
2020-11-24 06:37:46 +03:00
|
|
|
let cronResult = cronValidate(expression, {
|
|
|
|
preset: 'default', // second field not supported in default preset
|
|
|
|
override: {
|
|
|
|
useSeconds: true // override preset option
|
|
|
|
}
|
|
|
|
});
|
2020-11-05 07:07:27 +03:00
|
|
|
|
2020-11-10 03:33:01 +03:00
|
|
|
return cronResult.isValid();
|
2020-11-05 07:07:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = isCronExpression;
|