Files
homarr/packages/cron-jobs-core/src/validation.ts
Meier Lukas 92afd82d22 refactor: add cron job core package (#704)
* refactor: add cron job core package

* docs: add comments to cron validation types

* chore(deps): move node-cron dependencies from tasks app to cron-jobs-core package

* fix: runOnInit is not running on start and rather on job creation

* fix: format issues

* fix: build fails when using top level await in cjs

* chore: update turbo gen package json typescript version to 5.5.2

* fix: format issue

* fix: deepsource issues

* chore: update turbo gen package json eslint version to 9.5.0

* chore: fix frozen lockfile and format
2024-06-22 20:00:20 +02:00

61 lines
2.5 KiB
TypeScript

// The below two types are used for a number with arbitrary length. By default the type `${number}` allows spaces which is not allowed in cron expressions.
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
type NumberWithoutSpaces = `${Digit}${number | ""}` & `${number | ""}${Digit}`;
// The below type is used to constrain the cron expression to only allow valid characters. This will find any invalid characters in the cron expression.
type CronChars = `${"*" | "/" | "-" | "," | NumberWithoutSpaces}`;
/**
* Will return false if the TMaybeCron string contains any invalid characters.
* Otherwise it will return true.
*/
type ConstrainedCronString<TMaybeCron extends string> = TMaybeCron extends ""
? true
: TMaybeCron extends `${CronChars}${infer Rest}`
? ConstrainedCronString<Rest>
: false;
/**
* Will return true if the TMaybeCron string is a valid cron expression.
* Otherwise it will return false.
*
* It allows cron expressions with 5 or 6 parts. (Seconds are optional)
* See https://nodecron.com/docs/
*/
export type ValidateCron<TMaybeCron extends string> =
TMaybeCron extends `${infer inferedSecond} ${infer inferedMinute} ${infer inferedHour} ${infer inferedMonthDay} ${infer inferedMonth} ${infer inferedWeekDay}`
? ConstrainedCronString<inferedSecond> extends true
? ConstrainedCronString<inferedMinute> extends true
? ConstrainedCronString<inferedHour> extends true
? ConstrainedCronString<inferedMonthDay> extends true
? ConstrainedCronString<inferedMonth> extends true
? ConstrainedCronString<inferedWeekDay> extends true
? true
: false
: false
: false
: false
: false
: false
: TMaybeCron extends `${infer inferedMinute} ${infer inferedHour} ${infer inferedMonthDay} ${infer inferedMonth} ${infer inferedWeekDay}`
? ConstrainedCronString<inferedMinute> extends true
? ConstrainedCronString<inferedHour> extends true
? ConstrainedCronString<inferedMonthDay> extends true
? ConstrainedCronString<inferedMonth> extends true
? ConstrainedCronString<inferedWeekDay> extends true
? true
: false
: false
: false
: false
: false
: false;
/**
* Will return the cron expression if it is valid.
* Otherwise it will return void (as type).
*/
export const checkCron = <const T extends string>(cron: T) => {
return cron as ValidateCron<T> extends true ? T : void;
};