Add more test cases and fix more validation

This commit is contained in:
Jacob Wallraff
2023-01-10 09:57:07 -08:00
parent 7be6d9619c
commit 5626b7fb85
2 changed files with 29 additions and 8 deletions
@@ -3,6 +3,7 @@ import { isValidCron } from "./cron"
describe("isValidCron", () => {
const valid = [
"0 0 * * *",
"0 000 001 * *",
"15 * * * *",
"2,10 4,5 * * *",
"30 4-6 * * *",
@@ -19,7 +20,6 @@ describe("isValidCron", () => {
"0 0 * * SUN-2",
"0 * * */FEB */TUE",
"0 2-4/5 * * *",
// "0 29/5 * * *" <- crontab guru says this is valid, need to test with go library
]
for (const cron of valid) {
@@ -30,13 +30,25 @@ describe("isValidCron", () => {
const invalid = [
"0 0 * *",
"0 0 * * * * *",
"0 -1 * * *",
"0 1- * * *",
"0 /1 * * *",
"0 1/ * * *",
"0 ,1 * * *",
"0 1, * * *",
"0 -- * * *",
"0 // * * *",
"0 ,, * * *",
"0 ** * * *",
"0 0 * * BUN",
"0 0 * SUN JAN",
"0 0 * * FRI-TUE",
"0 12-4 * * *",
"0 */0 * * *",
"0 2/4-5 * * *", // TODO
"0 2-4-6/5 * * *", // TODO
"0 2/4-5 * * *",
"0 2-4-6 * * *",
"0 2/4/6 * * *",
]
for (const cron of invalid) {
@@ -62,12 +62,15 @@ function validateRange(
return true
}
// Operator precedence: , > / > -
if (value.includes(",")) {
if (!allowSeparators) {
return false
}
// Allow separators
return value.split(",").every((v) => validateRange(v, range))
return value.split(",").every((v) => {
v && validateRange(v, range)
})
}
if (value.includes("/")) {
@@ -76,18 +79,24 @@ function validateRange(
}
const [start, step, ...rest] = value.split("/")
// Supports */TUE and similar, which nees to be verified with the go cron library
// Supports */TUE and similar, which needs to be verified with the go cron library
const stepNumber = convertToNumber(range, step)
if (rest.length > 0 || stepNumber <= 0) {
if (rest.length > 0 || stepNumber <= 0 || !start || !step) {
return false
}
return (
validateRange(start, range, false) && validateRange(step, range, false)
validateRange(start, range) && validateRange(step, range, false)
)
}
if (value.includes("-")) {
const [start, end] = value.split("-")
if (!allowSeparators) {
return false
}
const [start, end, ...rest] = value.split("-")
if (rest.length > 0 || !start || !end) {
return false
}
// Convert name to integers so we can make sure end >= start
const startNumber = convertToNumber(range, start)