mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-25 19:48:50 +03:00
c0cecd71fe
no issue - When providing a crontab schedule expression it should always contain 6 elements first one of them being a "seconds" schedule description . For example: '0/5 * * * * *' - meaning to run every 5 seconds
37 lines
1.4 KiB
JavaScript
37 lines
1.4 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, {
|
|
preset: 'default', // second field not supported in default preset
|
|
override: {
|
|
useSeconds: true // override preset option
|
|
}
|
|
});
|
|
|
|
return cronResult.isValid();
|
|
};
|
|
|
|
module.exports = isCronExpression;
|