🔨 Check cron settings before saving it

This commit is contained in:
Sebastien Clement 2020-01-04 18:30:24 +01:00
parent e407c23b89
commit c2e8c08cb8
2 changed files with 50 additions and 2 deletions

View File

@ -8,6 +8,8 @@ const settingsTools = require('../tools/settingsTools');
const hassioApiTools = require('../tools/hassioApiTools');
const cronTools = require('../tools/cronTools');
@ -123,8 +125,17 @@ router.get('/backup-settings', function(req, res, next){
router.post('/backup-settings', function(req, res, next){
//TODO check if config is valid
settingsTools.setSettings(req.body);
res.send(200);
if(cronTools.checkConfig(req.body)){
settingsTools.setSettings(req.body);
res.send();
}
else{
res.status(400);
res.send();
}
});

View File

@ -0,0 +1,37 @@
function checkConfig(conf) {
if (conf.cron_base != null) {
if (conf.cron_base == '1' || conf.cron_base == '2' || conf.cron_base == '3') {
if (conf.cron_hour != null && conf.cron_hour.match(/\d\d:\d\d/)) {
if (conf.cron_base == '1')
return true;
}
else
return false;
}
if (conf.cron_base == '2') {
if (conf.cron_weekday != null && conf.cron_weekday >= 0 && conf.cron_weekday <= 6)
return true;
else
return false;
}
if ( conf.cron_base == '3'){
if (conf.cron_month_day != null && conf.cron_month_day >= 1 && conf.cron_month_day <= 28)
return true;
else
return false;
}
if(conf.cron_base == '0')
return true
}
else
return false;
return false;
}
exports.checkConfig = checkConfig;