Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"@codemirror/lint": "6.8.4",
"@codemirror/merge": "^6.10.0",
"@codemirror/search": "6.5.8",
"@datasert/cronjs-parser": "^1.4.0",
"@lezer/highlight": "1.2.1",
"@replit/codemirror-indentation-markers": "6.5.3",
"@replit/codemirror-vscode-keymap": "6.0.2",
Expand All @@ -119,6 +120,7 @@
"ansi_up": "^5.2.1",
"chart.js": "^4.5.0",
"codemirror-json-schema": "0.8.0",
"cronstrue": "^3.9.0",
"dayjs": "^1.11.13",
"fast-json-patch": "^3.1.1",
"focus-trap-react": "^10.3.1",
Expand Down
15 changes: 15 additions & 0 deletions src/Shared/Helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
/* eslint-disable no-param-reassign */
import { ReactElement, useEffect, useRef, useState } from 'react'
import { PromptProps } from 'react-router-dom'
import { parse as parseCronExpression } from '@datasert/cronjs-parser'
import { StrictRJSFSchema } from '@rjsf/utils'
import Tippy from '@tippyjs/react'
import cronstrue from 'cronstrue'
import { animate } from 'framer-motion'
import moment from 'moment'
import { nanoid } from 'nanoid'
Expand Down Expand Up @@ -752,3 +754,16 @@ export const formatNumberToCurrency = (value: number, currency: string, minimumF
return value.toFixed(precision)
}
}

/**
* Returns the human readable explanation of the expression
* NOTE: expectation is that the expression is valid
*
* @throws Error - if given expression is incorrect
* @param expression
* @returns string - helper text explaining the expression in a human readable format
*/
export const explainCronExpression = (expression: string): string => {
parseCronExpression(expression, { hasSeconds: expression.trim().split(' ').length > 5 })
return cronstrue.toString(expression)
}
33 changes: 10 additions & 23 deletions src/Shared/validations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { parse as parseCronExpression } from '@datasert/cronjs-parser'
import { customizeValidator } from '@rjsf/validator-ajv8'
import { parse } from 'yaml'

Expand Down Expand Up @@ -546,31 +547,17 @@ export const getIsRegexValid = (regexString: string): ValidationResponseType =>
}
}

export const validateCronExpression = (cron: string): ValidationResponseType => {
// Basic cron validation - 5 parts separated by spaces
const parts = cron.trim().split(/\s+/)
if (parts.length !== 5) {
return { isValid: false, message: 'Cron expression must have 5 parts separated by spaces' }
}

const isValid = parts.every((part) => {
if (part === '*') return true
if (/^\d+$/.test(part)) return true
if (/^\d+-\d+$/.test(part)) return true
if (/^\*\/\d+$/.test(part)) return true
if (/^(\d+,)+\d+$/.test(part)) return true
return false
})
export const validateCronExpression = (expression: string): ValidationResponseType => {
try {
parseCronExpression(expression, { hasSeconds: expression.trim().split(' ').length > 5 })

// Basic validation - each part should be either * or a number or a range
if (isValid) {
return {
isValid,
isValid: true,
}
} catch (err) {
return {
isValid: false,
message: (err as Error).message,
}
}

return {
isValid: false,
message: 'Invalid cron expression format',
}
}