(pattern: string)
| 156 | * Useful for tooling, editors and DX. |
| 157 | */ |
| 158 | export function validateDetailed(pattern: string): DetailedValidation { |
| 159 | if (typeof pattern !== 'string') |
| 160 | return { valid: false, errors: [{ field: 'expression', message: 'pattern must be a string' }] }; |
| 161 | |
| 162 | if (!ALLOWED_CHARS_REGEX.test(pattern)) |
| 163 | return { valid: false, errors: [{ field: 'expression', value: pattern, message: 'pattern includes illegal characters' }] }; |
| 164 | |
| 165 | const raw = pattern.replace(/\s{2,}/g, ' ').trim().split(' '); |
| 166 | if (raw.length !== 5 && raw.length !== 6) |
| 167 | return { valid: false, errors: [{ field: 'expression', value: pattern, message: `expected 5 or 6 fields but got ${raw.length}` }] }; |
| 168 | |
| 169 | const patterns = raw.length === 5 ? ['0', ...raw] : raw; |
| 170 | const executable = convertExpression(pattern); |
| 171 | |
| 172 | const errors: CronFieldError[] = []; |
| 173 | FIELDS.forEach((f, i) => { |
| 174 | if (f.invalid(executable[i])) |
| 175 | errors.push({ field: f.key, value: patterns[i], message: `${patterns[i]} is a invalid expression for ${f.label}` }); |
| 176 | }); |
| 177 | |
| 178 | if (errors.length) return { valid: false, errors }; |
| 179 | |
| 180 | return { |
| 181 | valid: true, |
| 182 | errors: [], |
| 183 | fields: { |
| 184 | second: executable[0], |
| 185 | minute: executable[1], |
| 186 | hour: executable[2], |
| 187 | dayOfMonth: executable[3], |
| 188 | month: executable[4], |
| 189 | dayOfWeek: executable[5], |
| 190 | }, |
| 191 | }; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Parses an expression into its decomposed fields, or throws an Error with a |
no outgoing calls
no test coverage detected