Ghost/ghost/job-manager/lib/is-cron-expression.js
Naz 70b42e3a75 Switched cron validation library to cron-validate
no issue

- Previous library was relyting on try/catch block to check if the expression is valid. Flow control through error catching is not considered a good practice and can effect performance (https://riptutorial.com/javascript/example/5297/avoid-try-catch-in-performance-critical-functions)
2020-11-10 13:33:01 +13:00

32 lines
1.2 KiB
JavaScript

const cronValidate = require('cron-validate');
/**
* Checks if expression follows supported crontab format
* reference: https://www.adminschoice.com/crontab-quick-reference
* builder: https://crontab.guru/
*
* 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)
*
* @param {String} expression in crontab format (https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html)
*
* @returns {boolean} wheather or not the expression is valid
*/
const isCronExpression = (expression) => {
let cronResult = cronValidate(expression);
return cronResult.isValid();
};
module.exports = isCronExpression;