How to validate cron expression using Later.js method
I want to validate a cron expression using javascript. Here is a library that deals with cron expression http://bunkat.github.io/later/
From the documentation, I cannot find how to check the cron expression. No matter what I go to var cronSched = later.parse.cron('$%#');
, it is always populated with cronSched object. So how do I get the true / false value for the cron expression?
source to share
Had the exact same needs and this tool appears to handle the job:
https://github.com/harrisiirak/cron-parser
Here's a solution using lodash:
try{
var validacao = cronParse.parseString(valor.cron);
if (_.isEmpty(validacao.errors)){
return true;
}
throw "Invalid Cron!"
}
catch(ex){
return false;
}
Then you can check your return value to fill your needs. Hope this helps!
source to share
The call var cronSched = later.parse.cron('$%#');
returns a schedule later
or throws an exception, so surround it try { ... } catch (e) { ... }
because it doesn't indicate what type contains the exceptions.
Source: https://github.com/bunkat/later/blob/master/src/parse/cron.js
source to share