|
| 1 | +// Copyright 2026, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +export type ConfigStringValidationOptions = { |
| 5 | + required?: boolean; |
| 6 | + trim?: boolean; |
| 7 | + maxLength?: number; |
| 8 | + pattern?: RegExp; |
| 9 | + validate?: (value: string) => string | undefined; |
| 10 | +}; |
| 11 | + |
| 12 | +export type ConfigNumberValidationOptions = { |
| 13 | + min?: number; |
| 14 | + max?: number; |
| 15 | + integer?: boolean; |
| 16 | +}; |
| 17 | + |
| 18 | +export function normalizeConfigStringInput(value: string, options?: ConfigStringValidationOptions): string { |
| 19 | + if (options?.trim === false) { |
| 20 | + return value; |
| 21 | + } |
| 22 | + return value.trim(); |
| 23 | +} |
| 24 | + |
| 25 | +export function validateConfigStringInput(value: string, options?: ConfigStringValidationOptions): string | undefined { |
| 26 | + const normalizedValue = normalizeConfigStringInput(value, options); |
| 27 | + |
| 28 | + if (options?.required && normalizedValue.length === 0) { |
| 29 | + return "Required"; |
| 30 | + } |
| 31 | + if (normalizedValue.length === 0) { |
| 32 | + return; |
| 33 | + } |
| 34 | + if (options?.maxLength != null && normalizedValue.length > options.maxLength) { |
| 35 | + return `Must be ${options.maxLength} characters or less`; |
| 36 | + } |
| 37 | + if (options?.pattern != null && !options.pattern.test(normalizedValue)) { |
| 38 | + return "Invalid format"; |
| 39 | + } |
| 40 | + return options?.validate?.(normalizedValue); |
| 41 | +} |
| 42 | + |
| 43 | +export function validateConfigNumberInput( |
| 44 | + value: string, |
| 45 | + options?: ConfigNumberValidationOptions |
| 46 | +): { value: number | undefined; error?: string } { |
| 47 | + const trimmedValue = value.trim(); |
| 48 | + if (trimmedValue.length === 0) { |
| 49 | + return { value: undefined }; |
| 50 | + } |
| 51 | + |
| 52 | + const parsedValue = Number(trimmedValue); |
| 53 | + if (!Number.isFinite(parsedValue)) { |
| 54 | + return { value: undefined, error: "Must be a number" }; |
| 55 | + } |
| 56 | + if (options?.integer && !Number.isInteger(parsedValue)) { |
| 57 | + return { value: undefined, error: "Must be a whole number" }; |
| 58 | + } |
| 59 | + if (options?.min != null && parsedValue < options.min) { |
| 60 | + return { value: undefined, error: `Must be at least ${options.min}` }; |
| 61 | + } |
| 62 | + if (options?.max != null && parsedValue > options.max) { |
| 63 | + return { value: undefined, error: `Must be at most ${options.max}` }; |
| 64 | + } |
| 65 | + |
| 66 | + return { value: parsedValue }; |
| 67 | +} |
0 commit comments