True when the declared type is a recognised primitive.
(declared: string, value: unknown)
| 44 | |
| 45 | /** True when the declared type is a recognised primitive. */ |
| 46 | function declaredPrimitiveCheck(declared: string, value: unknown): string | null { |
| 47 | switch (declared) { |
| 48 | case "string": |
| 49 | if (typeof value !== "string") return `expected string, got ${typeof value}`; |
| 50 | return value.length === 0 ? "empty string" : null; |
| 51 | case "int": case "uint": case "float": |
| 52 | if (typeof value !== "number" || !Number.isFinite(value)) return `expected number, got ${typeof value}`; |
| 53 | if (declared === "uint" && value < 0) return "expected uint, got negative"; |
| 54 | if (declared === "int" && !Number.isInteger(value)) return "expected int, got non-integer"; |
| 55 | return null; |
| 56 | case "bool": |
| 57 | return typeof value === "boolean" ? null : `expected bool, got ${typeof value}`; |
| 58 | case "json": |
| 59 | return null; |
| 60 | default: |
| 61 | return null; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Validate a parsed model output against the prompt's declared return type. |
no outgoing calls
no test coverage detected