mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
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
This commit is contained in:
parent
5506c64ae4
commit
3da365999d
3 changed files with 52 additions and 0 deletions
32
ghost/job-manager/lib/is-cron-expression.js
Normal file
32
ghost/job-manager/lib/is-cron-expression.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
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;
|
|
@ -24,6 +24,7 @@
|
|||
"sinon": "9.2.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"cron-parser": "2.17.0",
|
||||
"fastq": "1.9.0",
|
||||
"p-wait-for": "3.1.0"
|
||||
}
|
||||
|
|
19
ghost/job-manager/test/is-cron-expression.test.js
Normal file
19
ghost/job-manager/test/is-cron-expression.test.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Switch these lines once there are useful utils
|
||||
// const testUtils = require('./utils');
|
||||
require('./utils');
|
||||
|
||||
const isCronExpression = new require('../lib/is-cron-expression');
|
||||
|
||||
describe('Is cron expression', function () {
|
||||
it('valid cron expressions', function () {
|
||||
should(isCronExpression('* * * * *')).be.true();
|
||||
should(isCronExpression('1 * * * *')).be.true();
|
||||
should(isCronExpression('* * 12-15 * * *'), 'Range should be 0-23').be.true();
|
||||
});
|
||||
|
||||
it('invalid cron expressions', function () {
|
||||
should(isCronExpression('123 * * * *')).not.be.true();
|
||||
should(isCronExpression('a * * * *')).not.be.true();
|
||||
should(isCronExpression('* * 12-36 * * *'), 'Invalid range should be 0-23').not.be.true();
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue