(value: unknown, label: string)
| 442 | * Returns `undefined` for empty values and throws a helpful error for invalid JSON strings. |
| 443 | */ |
| 444 | export function parseOptionalJsonInput<T = unknown>(value: unknown, label: string): T | undefined { |
| 445 | if (value === undefined || value === null || value === '') { |
| 446 | return undefined |
| 447 | } |
| 448 | |
| 449 | if (typeof value === 'string') { |
| 450 | const trimmed = value.trim() |
| 451 | if (trimmed.length === 0) { |
| 452 | return undefined |
| 453 | } |
| 454 | |
| 455 | try { |
| 456 | return JSON.parse(trimmed) as T |
| 457 | } catch (error) { |
| 458 | throw new Error(`Invalid JSON for ${label}: ${toError(error).message}`) |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return value as T |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Parses an optional numeric block input value. |
no test coverage detected