(value: string)
| 79 | * Parses an inclusive byte-size range (e.g. "1MB", "1MB-2MB", "-1MB", "1MB-"). |
| 80 | */ |
| 81 | export function parseByteSizeRange(value: string): ByteSizeRange { |
| 82 | const trimmed = value.trim(); |
| 83 | if (!trimmed.includes('-')) { |
| 84 | return {min: parseByteSize(trimmed), max: undefined}; |
| 85 | } |
| 86 | |
| 87 | const parts = trimmed.split('-'); |
| 88 | if (parts.length !== 2) { |
| 89 | throw new Error( |
| 90 | `Invalid byte size range: "${value}". Expected a size or range like "1MB", "1MB-2MB", "-1MB", or "1MB-".`, |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | const minValue = parts[0]; |
| 95 | const maxValue = parts[1]; |
| 96 | if (minValue === undefined || maxValue === undefined) { |
| 97 | throw new Error(`Invalid byte size range: "${value}"`); |
| 98 | } |
| 99 | |
| 100 | const minText = minValue.trim(); |
| 101 | const maxText = maxValue.trim(); |
| 102 | if (minText === '' && maxText === '') { |
| 103 | throw new Error( |
| 104 | `Invalid byte size range: "${value}". At least one bound is required.`, |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | const min = minText === '' ? 0 : parseByteSize(minText); |
| 109 | const max = maxText === '' ? undefined : parseByteSize(maxText); |
| 110 | if (max !== undefined && min > max) { |
| 111 | throw new Error( |
| 112 | `Invalid byte size range: "${value}". The lower bound must not exceed the upper bound.`, |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | return {min, max}; |
| 117 | } |
| 118 | |
| 119 | export function byteSizeRangeSchema( |
| 120 | description: string, |
no test coverage detected