cron-expression-validator
Advanced tools
Comparing version 1.0.7 to 1.0.8
164
index.js
@@ -0,1 +1,3 @@ | ||
const CONSTANTS = require('./constants'); | ||
const MONTH_LIST = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; | ||
@@ -7,6 +9,7 @@ const WEEK_ARRRAY = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']; | ||
const MAX_HOUR_VALUE = 23; | ||
let isError = false; | ||
let errorMsg = []; | ||
exports.isValidCronExpression = function(cronExpression, errorObj) { | ||
exports.isValidCronExpression = function(cronExpression) { | ||
let cronArray = cronExpression.split(" "); | ||
@@ -21,4 +24,2 @@ | ||
let year = cronArray[6].trim(); | ||
let error = false; | ||
let errorMsg = ''; | ||
@@ -33,3 +34,10 @@ let isValidSeconds = isValidTimeValue(seconds, MAX_MIN_SEC_VALUE); | ||
return isValidSeconds && isValidMinutes && isValidHour && isValidDayOfMonth && isValidMonth && isValidDayOfWeek && isValidYear; | ||
if(errorObj && errorObj.error && isError) { | ||
return { | ||
isValid: isValidSeconds && isValidMinutes && isValidHour && isValidDayOfMonth && isValidMonth && isValidDayOfWeek && isValidYear, | ||
errorMessage: errorMsg, | ||
} | ||
} else { | ||
return isValidSeconds && isValidMinutes && isValidHour && isValidDayOfMonth && isValidMonth && isValidDayOfWeek && isValidYear; | ||
} | ||
} | ||
@@ -43,5 +51,21 @@ | ||
let startingDayOfWeekOptionArr = dayOfWeek.split('/'); | ||
if(!isValidateMonthNo([startingDayOfWeekOptionArr[0]], 1, 7)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_1); | ||
} | ||
if(!isValidateMonthNo([startingDayOfWeekOptionArr[1]], 0, 7)) { | ||
isError = true; | ||
errorMsg.push('Expression '+startingDayOfWeekOptionArr[1]+' is not a valid increment value. Accepted values are 0-7'); | ||
} | ||
return isValidateMonthNo([startingDayOfWeekOptionArr[0]], 1, 7) && isValidateMonthNo([startingDayOfWeekOptionArr[1]], 0, 7); | ||
} else if(dayOfWeek.includes('-') && dayOfMonth === '?') { | ||
let dayOfWeekRangeArr = dayOfWeek.split('-'); | ||
if(!isNaN(parseInt(dayOfWeekRangeArr[0])) && !isNaN(parseInt(dayOfWeekRangeArr[1])) && !isValidateMonthNo(dayOfWeekRangeArr, 1, 7)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_1); | ||
} | ||
if(isNaN(parseInt(dayOfWeekRangeArr[0])) && isNaN(parseInt(dayOfWeekRangeArr[1])) && !isValidateMonthStr(dayOfWeekRangeArr, WEEK_ARRRAY)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_2); | ||
} | ||
return !isNaN(parseInt(dayOfWeekRangeArr[0])) && !isNaN(parseInt(dayOfWeekRangeArr[1])) ? | ||
@@ -51,8 +75,41 @@ isValidateMonthNo(dayOfWeekRangeArr, 1, 7) : isValidateMonthStr(dayOfWeekRangeArr, WEEK_ARRRAY); | ||
let multiDayOfWeekArr = dayOfWeek.split(','); | ||
if(!isNaN(parseInt(multiDayOfWeekArr[0])) && !isValidateMonthNo(multiDayOfWeekArr, 1, 7)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_1); | ||
} | ||
if(isNaN(parseInt(multiDayOfWeekArr[0])) && !isValidateMonthStr(multiDayOfWeekArr, WEEK_ARRRAY)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_2); | ||
} | ||
return !isNaN(parseInt(multiDayOfWeekArr[0])) ? | ||
isValidateMonthNo(multiDayOfWeekArr, 1, 7) : isValidateMonthStr(multiDayOfWeekArr, WEEK_ARRRAY); | ||
} else if(dayOfWeek.includes('#') && dayOfMonth === '?') { | ||
let weekdayOfMonthArr = dayOfWeek.split('#'); | ||
if(!isValidateMonthNo([weekdayOfMonthArr[0]], 1, 7)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_3); | ||
} | ||
if(!isValidateMonthNo([weekdayOfMonthArr[1]], 1, 5)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_4); | ||
} | ||
return isValidateMonthNo([weekdayOfMonthArr[0]], 1, 7) && isValidateMonthNo([weekdayOfMonthArr[1]], 1, 5) | ||
} else if(typeof dayOfWeek === 'string' && dayOfMonth === '?') { | ||
if(!isNaN(parseInt(dayOfWeek)) && !isValidateMonthNo([dayOfWeek], 1, 7) ) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_1); | ||
} | ||
if(isNaN(parseInt(dayOfWeek)) && !isValidateMonthStr([dayOfWeek], WEEK_ARRRAY)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_2); | ||
} | ||
return !isNaN(parseInt(dayOfWeek)) ? | ||
isValidateMonthNo([dayOfWeek], 1, 7) : isValidateMonthStr([dayOfWeek], WEEK_ARRRAY); | ||
} else { | ||
isError = true; | ||
if(dayOfWeek === '*' && dayOfMonth === '*' || dayOfWeek === '?' && dayOfMonth === '?') { | ||
errorMsg.push('? can only be specfied for Day-of-Month -OR- Day-of-Week') | ||
} else { | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_WEEK_ERROR_MSG_2+ " or * or /"); | ||
} | ||
return false; | ||
@@ -67,8 +124,24 @@ } | ||
let startingDayOfMonthOptionArr = dayOfMonth.split('/'); | ||
if(!isValidateMonthNo([startingDayOfMonthOptionArr[0]], 1, 31)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_1); | ||
} | ||
if(!isValidateMonthNo([startingDayOfMonthOptionArr[1]], 0, 31)) { | ||
isError = true; | ||
errorMsg.push("(Day of month) - Expression "+startingDayOfMonthOptionArr[1]+" is not a valid increment value. Accepted values are 0-31"); | ||
} | ||
return isValidateMonthNo([startingDayOfMonthOptionArr[0]], 1, 31) && isValidateMonthNo([startingDayOfMonthOptionArr[1]], 0, 31); | ||
} else if(dayOfMonth.includes('-') && dayOfWeek === '?') { | ||
let dayOfMonthRangeArr = dayOfMonth.split('-'); | ||
if(!isValidateMonthNo(dayOfMonthRangeArr, 1, 31)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_1); | ||
} | ||
return isValidateMonthNo(dayOfMonthRangeArr, 1, 31); | ||
} else if(dayOfMonth.includes(',') && dayOfWeek === '?') { | ||
let multiDayOfMonthArr = dayOfMonth.split(','); | ||
if(!isValidateMonthNo(multiDayOfMonthArr, 1, 12)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_1); | ||
} | ||
return isValidateMonthNo(multiDayOfMonthArr, 1, 12); | ||
@@ -78,4 +151,14 @@ } else if(typeof dayOfMonth === 'string' && dayOfWeek === '?' && (dayOfMonth.toLowerCase() === 'l' || dayOfMonth.toLowerCase() === 'lw')) { | ||
} else if(typeof dayOfMonth === 'string' && dayOfWeek === '?') { | ||
if(parseInt(dayOfMonth) <1 && parseInt(dayOfMonth) > 31) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_1); | ||
} | ||
return parseInt(dayOfMonth) >=1 && parseInt(dayOfMonth) <= 31; | ||
} else { | ||
isError = true; | ||
if(dayOfWeek === '*' && dayOfMonth === '*' || dayOfWeek === '?' && dayOfMonth === '?') { | ||
errorMsg.push('? can only be specfied for Day-of-Month -OR- Day-of-Week') | ||
} else { | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_1); | ||
} | ||
return false; | ||
@@ -102,5 +185,21 @@ } | ||
let startingMonthOptionArr = month.split('/'); | ||
if(!isValidateMonthNo([startingMonthOptionArr[0]], 1, 12)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_ERROR_MSG); | ||
} | ||
if(!isValidateMonthNo([startingMonthOptionArr[1]], 0, 12)) { | ||
isError = true; | ||
errorMsg.push('(Month) - Expression '+startingMonthOptionArr[1]+' is not a valid increment value. Accepted values are 0-12'); | ||
} | ||
return isValidateMonthNo([startingMonthOptionArr[0]], 1, 12) && isValidateMonthNo([startingMonthOptionArr[1]], 0, 12); | ||
} else if(month.includes('-')) { | ||
let monthRangeArr = month.split('-'); | ||
if(!isNaN(parseInt(monthRangeArr[0])) && !isNaN(parseInt(monthRangeArr[1])) && !isValidateMonthNo(monthRangeArr, 1, 12)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_ERROR_MSG); | ||
} | ||
if(isNaN(parseInt(monthRangeArr[0])) && isNaN(parseInt(monthRangeArr[1])) && !isValidateMonthStr(monthRangeArr, MONTH_LIST)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_LETTER_ERROR_MSG); | ||
} | ||
return !isNaN(parseInt(monthRangeArr[0])) && !isNaN(parseInt(monthRangeArr[1])) ? | ||
@@ -110,8 +209,26 @@ isValidateMonthNo(monthRangeArr, 1, 12) : isValidateMonthStr(monthRangeArr, MONTH_LIST); | ||
let multiMonthArr = month.split(','); | ||
if(!isNaN(parseInt(multiMonthArr[0])) && !isValidateMonthNo(multiMonthArr, 1, 12)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_ERROR_MSG); | ||
} | ||
if(isNaN(parseInt(multiMonthArr[0])) && isValidateMonthStr(multiMonthArr, MONTH_LIST)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_LETTER_ERROR_MSG); | ||
} | ||
return !isNaN(parseInt(multiMonthArr[0])) ? | ||
isValidateMonthNo(multiMonthArr, 1, 12) : isValidateMonthStr(multiMonthArr, MONTH_LIST); | ||
} else if(typeof month === 'string') { | ||
if(!isNaN(parseInt(month)) && !isValidateMonthNo([month], 1, 12)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_ERROR_MSG); | ||
} | ||
if(isNaN(parseInt(month)) && !isValidateMonthStr([month], MONTH_LIST)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_LETTER_ERROR_MSG); | ||
} | ||
return !isNaN(parseInt(month)) ? | ||
isValidateMonthNo([month], 1, 12) : isValidateMonthStr([month], MONTH_LIST); | ||
} else { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.MONTH_LETTER_ERROR_MSG); | ||
return false; | ||
@@ -134,2 +251,10 @@ } | ||
let isValidRepeatOccurrence = parseInt(startingYearOptionArr[1]) >= 0 && parseInt(startingYearOptionArr[1]) <=129; | ||
if(!isValidYear) { | ||
isError = true; | ||
errorMsg.push('(Year) - Unsupported value '+startingYearOptionArr[0]+' for field. Possible values are 1970-2099 , - * /'); | ||
} | ||
if(!isValidRepeatOccurrence) { | ||
isError = true; | ||
errorMsg.push('(Year) - Expression '+startingYearOptionArr[1]+' is not a valid increment value. Accepted values are 0-129'); | ||
} | ||
return isValidYear && isValidRepeatOccurrence; | ||
@@ -140,5 +265,17 @@ } else if(year.includes('-')) { | ||
let isValidRange = parseInt(yearRangeArr[0]) <= parseInt(yearRangeArr[1]); | ||
if(!isValidYear) { | ||
isError = true; | ||
errorMsg.push('(Year) - Unsupported value '+startingYearOptionArr[0]+' for field. Possible values are 1970-2099 , - * /'); | ||
} | ||
if(!isValidRange) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.YEAR_ERROR_MSG); | ||
} | ||
return isValidYear && isValidRange; | ||
} else if(year.includes(',')) { | ||
let multiYearArr = year.split(','); | ||
if(!isValidateYear(multiYearArr)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.YEAR_UNSUPPORT_VAL_ERROR_MSG); | ||
} | ||
return isValidateYear(multiYearArr); | ||
@@ -148,2 +285,4 @@ } else if (parseInt(year) >= 1970 && parseInt(year) <= 2099) { | ||
} else { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.YEAR_UNSUPPORT_VAL_ERROR_MSG); | ||
return false; | ||
@@ -158,8 +297,20 @@ } | ||
let startingSecOptionArr = time.split('/'); | ||
if(!isValidateTime(startingSecOptionArr, val)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.TIME_ERROR_MSG); | ||
} | ||
return isValidateTime(startingSecOptionArr, val); | ||
} else if(time.includes('-')) { | ||
let secRangeArr = time.split('-'); | ||
if(!isValidateTime(secRangeArr, val)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.TIME_ERROR_MSG); | ||
} | ||
return isValidateTime(secRangeArr, val); | ||
} else if(time.includes(',')) { | ||
let multiSecArr = time.split(','); | ||
if(!isValidateTime(multiSecArr, val)) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.TIME_ERROR_MSG); | ||
} | ||
return isValidateTime(multiSecArr, val) | ||
@@ -169,2 +320,4 @@ } else if (parseInt(time) >= 0 && parseInt(time) <= val) { | ||
} else { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.TIME_ERROR_MSG); | ||
return false; | ||
@@ -180,1 +333,2 @@ } | ||
console.log(module.exports.isValidCronExpression('0 0 12 ? * 4#56 *', {error: true})); |
{ | ||
"name": "cron-expression-validator", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Validate cron expressions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# cron-expression-validator | ||
cron-expression-validator is a **Node.JS** library to validate **quartz** cron expressions. | ||
cron-expression-validator is a **Node.JS** library to validate **quartz** cron expressions | ||
@@ -23,7 +23,38 @@ ## Installation | ||
```js | ||
if(cronValidator.isValidCronExpression("* * * * * * *")) { // returns false | ||
if(cronValidator.isValidCronExpression("* * * * * * *")) { | ||
// returns false | ||
} | ||
``` | ||
### Optional second param if you want to get error message | ||
Can get error message by passing ``` js { error: true } ``` as second parameter | ||
```js | ||
if(cronValidator.isValidCronExpression("* * * * 25/2 ? *"), {error: true}) { | ||
/** returns { | ||
/* isValid: false, | ||
/* errorMessage: [ 'Month values must be between 1 and 12' ] | ||
/* } | ||
**/ | ||
} | ||
``` | ||
``` js | ||
if(cronValidator.isValidCronExpression("* * * ? * * 123/555"), {error: true}) { | ||
/** returns { | ||
/* isValid: false, | ||
/* errorMessage: [ '(Year) - Unsupported value 123 for field. Possible values are 1970-2099 , - * /', | ||
/* '(Year) - Expression 555 is not a valid increment value. Accepted values are 0-129' | ||
/* ] | ||
/* } | ||
**/ | ||
} | ||
``` | ||
``` js | ||
if(cronValidator.isValidCronExpression("0 0 12 1/2 * ? *"), {error: false}) { // returns true | ||
// Your code | ||
} | ||
``` | ||
## Cron accepted values | ||
@@ -30,0 +61,0 @@ Seconds: 0-59 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18435
4
313
70