-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add individual checker files and add helper functions
- Loading branch information
Konstantin
committed
Apr 12, 2020
1 parent
f85f36a
commit 24ecf74
Showing
8 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CronData, Options } from '../index' | ||
import { err, valid } from '../result' | ||
import checkField from '../helper' | ||
|
||
const checkDaysOfMonth = (cronData: CronData, options: Options) => { | ||
if (!cronData.daysOfMonth) { | ||
return err(['daysOfMonth field is undefined.']) | ||
} | ||
|
||
const { daysOfMonth } = cronData | ||
|
||
return checkField(daysOfMonth, 'daysOfMonth', options) | ||
} | ||
|
||
export default checkDaysOfMonth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CronData, Options } from '../index' | ||
import { err, valid } from '../result' | ||
import checkField from '../helper' | ||
|
||
const checkDaysOfWeek = (cronData: CronData, options: Options) => { | ||
if (!cronData.daysOfWeek) { | ||
return err(['daysOfWeek field is undefined.']) | ||
} | ||
|
||
const { daysOfWeek } = cronData | ||
|
||
return checkField(daysOfWeek, 'daysOfWeek', options) | ||
} | ||
|
||
export default checkDaysOfWeek |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CronData, Options } from '../index' | ||
import { err, valid } from '../result' | ||
import checkField from '../helper' | ||
|
||
const checkHours = (cronData: CronData, options: Options) => { | ||
if (!cronData.hours) { | ||
return err(['hours field is undefined.']) | ||
} | ||
|
||
const { hours } = cronData | ||
|
||
return checkField(hours, 'hours', options) | ||
} | ||
|
||
export default checkHours |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CronData, Options } from '../index' | ||
import { err, valid } from '../result' | ||
import checkField from '../helper' | ||
|
||
const checkMinutes = (cronData: CronData, options: Options) => { | ||
if (!cronData.minutes) { | ||
return err(['minutes field is undefined.']) | ||
} | ||
|
||
const { minutes } = cronData | ||
|
||
return checkField(minutes, 'minutes', options) | ||
} | ||
|
||
export default checkMinutes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CronData, Options } from '../index' | ||
import { err, valid } from '../result' | ||
import checkField from '../helper' | ||
|
||
const checkMonths = (cronData: CronData, options: Options) => { | ||
if (!cronData.months) { | ||
return err(['months field is undefined.']) | ||
} | ||
|
||
const { months } = cronData | ||
|
||
return checkField(months, 'months', options) | ||
} | ||
|
||
export default checkMonths |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { CronData } from '../index' | ||
import { err, valid } from '../result' | ||
import checkField from '../helper' | ||
import { Options } from '../option' | ||
|
||
const checkSeconds = (cronData: CronData, options: Options) => { | ||
if (!cronData.seconds) { | ||
return err(['seconds field is undefined, but useSeconds options is enabled.']) | ||
} | ||
|
||
const { seconds } = cronData | ||
|
||
return checkField(seconds, 'seconds', options) | ||
} | ||
|
||
export default checkSeconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CronData, Options } from '../index' | ||
import { err, valid } from '../result' | ||
import checkField from '../helper' | ||
|
||
const checkYears = (cronData: CronData, options: Options) => { | ||
if (!cronData.years) { | ||
return err(['years field is undefined, but useYears option is enabled.']) | ||
} | ||
|
||
const { years } = cronData | ||
|
||
return checkField(years, 'years', options) | ||
} | ||
|
||
export default checkYears |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { CronData, CronFieldType } from './index' | ||
import { Err, err, Valid, valid } from './result' | ||
import checkSeconds from './fieldCheckers/secondChecker' | ||
import { Options } from './option' | ||
|
||
const checkSingleElement = ( | ||
element: string, | ||
cronFieldType: CronFieldType, | ||
options: Options | ||
) => { | ||
if (element === '*') { | ||
return valid(true) | ||
} | ||
|
||
if (element === '') { | ||
return err(`One of the elements is empty in ${cronFieldType} field.`) | ||
} | ||
|
||
const number = Number(element) | ||
if (isNaN(number)) { | ||
return err(`Element '${element} of ${cronFieldType} field is invalid.`) | ||
} | ||
|
||
const lowerLimit = options[cronFieldType]?.lowerLimit | ||
const upperLimit = options[cronFieldType]?.upperLimit | ||
if (number < lowerLimit) { | ||
} | ||
|
||
return valid(true) | ||
} | ||
|
||
const checkRangeElement = ( | ||
element: string, | ||
cronFieldType: CronFieldType, | ||
options: Options | ||
) => { | ||
if (element === '*') { | ||
return err(`'*' can't be part of a range in ${cronFieldType} field.`) | ||
} | ||
|
||
if (element === '') { | ||
return err(`One of the range elements is empty in ${cronFieldType} field.`) | ||
} | ||
|
||
const number = Number(element) | ||
if (isNaN(number)) { | ||
return err(`Element '${element} of ${cronFieldType} field is invalid.`) | ||
} | ||
|
||
return valid(true) | ||
} | ||
|
||
const checkFirstStepElement = ( | ||
firstStepElement: string, | ||
cronFieldType: CronFieldType, | ||
options: Options | ||
) => { | ||
const rangeArray = firstStepElement.split('-') | ||
if (rangeArray.length > 3) { | ||
return err( | ||
`List element '${firstStepElement}' is not valid. (More than one '-')` | ||
) | ||
} | ||
|
||
if (rangeArray.length === 1) { | ||
return checkSingleElement(rangeArray[0], cronFieldType, options) | ||
} else if (rangeArray.length === 2) { | ||
const firstRangeElementResult = checkRangeElement( | ||
rangeArray[0], | ||
cronFieldType, | ||
options | ||
) | ||
const secondRangeElementResult = checkRangeElement( | ||
rangeArray[1], | ||
cronFieldType, | ||
options | ||
) | ||
|
||
if (firstRangeElementResult.isError()) { | ||
return firstRangeElementResult | ||
} | ||
|
||
if (secondRangeElementResult.isError()) { | ||
return secondRangeElementResult | ||
} | ||
|
||
if (rangeArray[0] > rangeArray[1]) { | ||
return err( | ||
`Lower range end '${rangeArray[0]}' is bigger than upper range end '${rangeArray[1]}' of ${cronFieldType} field.` | ||
) | ||
} | ||
|
||
return valid(true) | ||
} | ||
|
||
return err( | ||
'Some other error in checkFirstStepElement (rangeArray less than 1)' | ||
) | ||
} | ||
|
||
const checkListElement = ( | ||
listElement: string, | ||
cronFieldType: CronFieldType, | ||
options: Options | ||
) => { | ||
const stepArray = listElement.split('/') | ||
if (stepArray.length > 3) { | ||
return err( | ||
`List element '${listElement}' is not valid. (More than one '/')` | ||
) | ||
} | ||
|
||
const firstElementResult = checkFirstStepElement( | ||
stepArray[0], | ||
cronFieldType, | ||
options | ||
) | ||
|
||
if (firstElementResult.isError()) { | ||
return firstElementResult | ||
} | ||
|
||
if (stepArray.length === 2) { | ||
// check second element (x) of */x | ||
} | ||
|
||
return valid(true) | ||
} | ||
|
||
const checkField = ( | ||
cronField: string, | ||
cronFieldType: CronFieldType, | ||
options: Options | ||
) => { | ||
if ( | ||
![ | ||
'seconds', | ||
'minutes', | ||
'hours', | ||
'daysOfMonth', | ||
'months', | ||
'daysOfWeek', | ||
'years', | ||
].includes(cronFieldType) | ||
) { | ||
return err([`Cron field type '${cronFieldType}' does not exist.`]) | ||
} | ||
|
||
// Check for lists e.g. 4,5,6,8-18,20-40/2 | ||
const listArray = cronField.split(',') | ||
const checkResults: (Valid<boolean, unknown> | Err<unknown, string>)[] = [] | ||
listArray.forEach((listElement: string) => { | ||
checkResults.push(checkListElement(listElement, cronFieldType, options)) | ||
}) | ||
|
||
if (checkResults.every(value => value.isValid())) { | ||
return valid(true) | ||
} | ||
|
||
const errorArray: string[] = [] | ||
checkResults.forEach(result => { | ||
if (result.isError()) { | ||
errorArray.push(result.getError()) | ||
} | ||
}) | ||
return err(errorArray) | ||
} | ||
|
||
export default checkField |