Ghost/ghost/job-manager/lib/is-cron-expression.js
Naz 3da365999d Added cron expression validation
no issue

- CRON format is the most common one used for job scheduling and is well known to most developers
- This will become one of supported formats for job scheduling
2020-11-05 17:07:27 +13:00

33 lines
1.1 KiB
JavaScript

const parser = require('cron-parser');
/**
* Checks if expression follows supported CRON format as follows
* 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 CRON format
*
* @returns {boolean} wheather or not the expression is valid
*/
const isCronExpression = (expression) => {
try {
parser.parseExpression(expression);
return true;
} catch (err) {
return false;
}
};
module.exports = isCronExpression;