cron-expression-validator
Advanced tools
Comparing version
@@ -8,2 +8,3 @@ exports.ERROR_MSGES = { | ||
DAY_OF_MONTH_ERROR_MSG_1: 'Day of month values must be between 1 and 31', | ||
DAY_OF_MONTH_ERROR_MSG_2: 'Offset from last day must be <= 30', | ||
@@ -10,0 +11,0 @@ MONTH_ERROR_MSG: 'Month values must be between 1 and 12', |
32
index.js
@@ -54,9 +54,10 @@ const CONSTANTS = require('./constants'); | ||
if(errorObj && errorObj.error && isError) { | ||
let isValidCron = isValidSeconds && isValidMinutes && isValidHour && isValidDayOfMonth && isValidMonth && isValidDayOfWeek && isValidYear; | ||
if(errorObj && errorObj.error && isError && !isValidCron) { | ||
return { | ||
isValid: isValidSeconds && isValidMinutes && isValidHour && isValidDayOfMonth && isValidMonth && isValidDayOfWeek && isValidYear, | ||
isValid: isValidCron, | ||
errorMessage: errorMsg, | ||
} | ||
} else { | ||
return isValidSeconds && isValidMinutes && isValidHour && isValidDayOfMonth && isValidMonth && isValidDayOfWeek && isValidYear; | ||
return isValidCron; | ||
} | ||
@@ -69,3 +70,4 @@ } | ||
return true; | ||
} if(dayOfWeek === '*') { | ||
} | ||
if(dayOfWeek === '*') { | ||
return dayOfMonth !== '*'; | ||
@@ -141,3 +143,5 @@ } else if(dayOfWeek.includes('/') && dayOfMonth === '?') { | ||
const isInvalidValues = function(dayOfWeek, dayOfMonth) { | ||
return ((dayOfWeek === '*' && dayOfMonth === '*') || (dayOfWeek === '?' && dayOfMonth === '?')); | ||
const isAll = dayOfWeek === '*' && dayOfMonth === '*'; | ||
const isAny = dayOfWeek === '?' && dayOfMonth === '?'; | ||
return isAll || isAny; | ||
} | ||
@@ -150,2 +154,3 @@ | ||
const isValidDayOfMonthValue = function(dayOfMonth, dayOfWeek) { | ||
const isNotLastDays = !dayOfMonth.toLowerCase().includes('l') || !dayOfMonth.toLowerCase() === 'lw'; | ||
if((dayOfMonth === '*' && dayOfWeek !== '*') || (dayOfMonth === '?' && dayOfWeek !== '?')) { | ||
@@ -155,3 +160,3 @@ return true; | ||
let startingDayOfMonthOptionArr = dayOfMonth.split('/'); | ||
if(!isValidateMonthNo([startingDayOfMonthOptionArr[0]], 1, 31)) { | ||
if(!isValidateMonthNo([startingDayOfMonthOptionArr[0]], 1, 31 && !startingDayOfMonthOptionArr[0] === '*')) { | ||
isError = true; | ||
@@ -164,10 +169,17 @@ errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_1); | ||
} | ||
return isValidateMonthNo([startingDayOfMonthOptionArr[0]], 1, 31) && isValidateMonthNo([startingDayOfMonthOptionArr[1]], 0, 31); | ||
const isValidElements = isValidateMonthNo([startingDayOfMonthOptionArr[0]], 1, 31) && isValidateMonthNo([startingDayOfMonthOptionArr[1]], 0, 31); | ||
const isValidFirstElem = startingDayOfMonthOptionArr[0] === '*' && isValidateMonthNo([startingDayOfMonthOptionArr[1]], 0, 31); | ||
return isValidElements || isValidFirstElem; | ||
} else if(dayOfMonth.includes('-') && dayOfWeek === '?') { | ||
let dayOfMonthRangeArr = dayOfMonth.split('-'); | ||
if(!isValidateMonthNo(dayOfMonthRangeArr, 1, 31)) { | ||
const isLastDayIncludes = dayOfMonthRangeArr[0] === 'L' && isValidateMonthNo([dayOfMonthRangeArr[1]], 1, 30) | ||
if(!isLastDayIncludes) { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_2); | ||
} | ||
if(!isValidateMonthNo(dayOfMonthRangeArr, 1, 31) && dayOfMonthRangeArr[0] !== 'L') { | ||
isError = true; | ||
errorMsg.push(CONSTANTS.ERROR_MSGES.DAY_OF_MONTH_ERROR_MSG_1); | ||
} | ||
return isValidateMonthNo(dayOfMonthRangeArr, 1, 31); | ||
return isValidateMonthNo(dayOfMonthRangeArr, 1, 31) || isLastDayIncludes; | ||
} else if(dayOfMonth.includes(',') && dayOfWeek === '?') { | ||
@@ -182,3 +194,3 @@ let multiDayOfMonthArr = dayOfMonth.split(','); | ||
return true; | ||
} else if(typeof dayOfMonth === 'string' && dayOfWeek === '?') { | ||
} else if(typeof dayOfMonth === 'string' && dayOfWeek === '?' && dayOfMonth !== '?' && isNotLastDays) { | ||
if(parseInt(dayOfMonth) <1 && parseInt(dayOfMonth) > 31) { | ||
@@ -185,0 +197,0 @@ isError = true; |
{ | ||
"name": "cron-expression-validator", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "Validate cron expressions", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha" | ||
}, | ||
@@ -28,3 +28,7 @@ "repository": { | ||
"license": "ISC", | ||
"homepage": "https://github.com/anushaihalapathirana/cron-expression-validator#readme" | ||
"homepage": "https://github.com/anushaihalapathirana/cron-expression-validator#readme", | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"mocha": "^7.1.2" | ||
} | ||
} |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
23120
15.44%6
20%389
13.74%0
-100%2
Infinity%