|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +export const MAX_ABSOLUTE_SCHEDULE_WINDOW_SECONDS = 24 * 60 * 60; |
| 4 | + |
| 5 | +export type NormalizedScheduleWindow = |
| 6 | + | { type: "duration"; durationSeconds: number } |
| 7 | + | { type: "percentage"; percentage: number }; |
| 8 | + |
| 9 | +type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; |
| 10 | + |
| 11 | +type DigitsBelow = { |
| 12 | + "0": never; |
| 13 | + "1": "0"; |
| 14 | + "2": "0" | "1"; |
| 15 | + "3": "0" | "1" | "2"; |
| 16 | + "4": "0" | "1" | "2" | "3"; |
| 17 | + "5": "0" | "1" | "2" | "3" | "4"; |
| 18 | + "6": "0" | "1" | "2" | "3" | "4" | "5"; |
| 19 | + "7": "0" | "1" | "2" | "3" | "4" | "5" | "6"; |
| 20 | + "8": "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7"; |
| 21 | + "9": "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8"; |
| 22 | +}; |
| 23 | + |
| 24 | +type DigitLength< |
| 25 | + Value extends string, |
| 26 | + Result extends 0[] = [], |
| 27 | +> = Value extends `${Digit}${infer Rest}` ? DigitLength<Rest, [...Result, 0]> : Result; |
| 28 | + |
| 29 | +type CompareEqualLength< |
| 30 | + A extends string, |
| 31 | + B extends string, |
| 32 | +> = A extends `${infer ADigit extends Digit}${infer ARest}` |
| 33 | + ? B extends `${infer BDigit extends Digit}${infer BRest}` |
| 34 | + ? ADigit extends BDigit |
| 35 | + ? CompareEqualLength<ARest, BRest> |
| 36 | + : ADigit extends DigitsBelow[BDigit] |
| 37 | + ? "lt" |
| 38 | + : "gt" |
| 39 | + : "eq" |
| 40 | + : "eq"; |
| 41 | + |
| 42 | +type DecimalStringLTE<A extends string, B extends string> = |
| 43 | + DigitLength<A> extends DigitLength<B> |
| 44 | + ? CompareEqualLength<A, B> extends "gt" |
| 45 | + ? false |
| 46 | + : true |
| 47 | + : DigitLength<B> extends [...DigitLength<A>, ...0[]] |
| 48 | + ? true |
| 49 | + : false; |
| 50 | + |
| 51 | +type IsCanonicalUnsignedInteger<Value extends string> = Value extends `${bigint}` |
| 52 | + ? Value extends `-${string}` |
| 53 | + ? false |
| 54 | + : true |
| 55 | + : false; |
| 56 | + |
| 57 | +/** The literal validation error for a configured schedule window, or `never` when valid. */ |
| 58 | +export type ScheduleWindowError<Window extends string> = string extends Window |
| 59 | + ? never |
| 60 | + : Window extends `${infer Amount}m` |
| 61 | + ? IsCanonicalUnsignedInteger<Amount> extends false |
| 62 | + ? "⛔ window must be a whole non-negative number" |
| 63 | + : DecimalStringLTE<Amount, "1440"> extends true |
| 64 | + ? never |
| 65 | + : "⛔ window duration cannot exceed 24 hours" |
| 66 | + : Window extends `${infer Amount}h` |
| 67 | + ? IsCanonicalUnsignedInteger<Amount> extends false |
| 68 | + ? "⛔ window must be a whole non-negative number" |
| 69 | + : DecimalStringLTE<Amount, "24"> extends true |
| 70 | + ? never |
| 71 | + : "⛔ window duration cannot exceed 24 hours" |
| 72 | + : Window extends `${infer Amount}%` |
| 73 | + ? IsCanonicalUnsignedInteger<Amount> extends false |
| 74 | + ? "⛔ percentage must be a whole non-negative number" |
| 75 | + : DecimalStringLTE<Amount, "100"> extends true |
| 76 | + ? never |
| 77 | + : "⛔ percentage cannot exceed 100%" |
| 78 | + : '⛔ window must look like "30m", "2h", or "50%"'; |
| 79 | + |
| 80 | +/** |
| 81 | + * Preserves valid schedule-window literals and replaces invalid literals with a descriptive type |
| 82 | + * error. Wide `string` values pass through for authoritative runtime validation. |
| 83 | + */ |
| 84 | +export type ValidatedScheduleWindow<Window extends string | undefined> = Window extends string |
| 85 | + ? [ScheduleWindowError<Window>] extends [never] |
| 86 | + ? Window |
| 87 | + : ScheduleWindowError<Window> |
| 88 | + : Window; |
| 89 | + |
| 90 | +/** Parses and normalizes the public schedule-window syntax. */ |
| 91 | +export function parseScheduleWindow(value: string): NormalizedScheduleWindow { |
| 92 | + const durationMatch = /^(0|[1-9]\d*)([mh])$/.exec(value); |
| 93 | + |
| 94 | + if (durationMatch) { |
| 95 | + const amount = Number(durationMatch[1]); |
| 96 | + const unit = durationMatch[2] as "m" | "h"; |
| 97 | + const durationSeconds = amount * (unit === "m" ? 60 : 3_600); |
| 98 | + |
| 99 | + if ( |
| 100 | + !Number.isSafeInteger(durationSeconds) || |
| 101 | + durationSeconds > MAX_ABSOLUTE_SCHEDULE_WINDOW_SECONDS |
| 102 | + ) { |
| 103 | + throw new RangeError("Schedule window duration cannot exceed 24 hours"); |
| 104 | + } |
| 105 | + |
| 106 | + return { type: "duration", durationSeconds }; |
| 107 | + } |
| 108 | + |
| 109 | + const percentageMatch = /^(0|[1-9]\d?|100)%$/.exec(value); |
| 110 | + if (percentageMatch) { |
| 111 | + return { type: "percentage", percentage: Number(percentageMatch[1]) }; |
| 112 | + } |
| 113 | + |
| 114 | + throw new TypeError( |
| 115 | + 'Schedule window must be a whole duration such as "0m", "30m", or "24h", or a percentage such as "30%"' |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +/** Runtime authority for public schedule-window values. */ |
| 120 | +export const ScheduleWindow = z.string().superRefine((value, ctx) => { |
| 121 | + try { |
| 122 | + parseScheduleWindow(value); |
| 123 | + } catch (error) { |
| 124 | + ctx.addIssue({ |
| 125 | + code: z.ZodIssueCode.custom, |
| 126 | + message: error instanceof Error ? error.message : String(error), |
| 127 | + }); |
| 128 | + } |
| 129 | +}); |
0 commit comments