* Validate a parsed model output against the prompt's declared return type. * `outputType` is the IR-serialised type expression, e.g. "string", * "json", `enum<["a","b"]>`, "File", or `list `. We deliberately keep * the surface narrow: we recognise primitives, list , enum literals, * and
(value, outputType)
| 84 | * accept so users can specify rich return shapes via a custom validator. |
| 85 | */ |
| 86 | function validateSchemaOnly(value, outputType) { |
| 87 | const issues = []; |
| 88 | // File: { path: string, content: string, kind?: string } |
| 89 | if (outputType === "File") { |
| 90 | fileShapeIssues(value, issues, ""); |
| 91 | return finalize(issues, "file"); |
| 92 | } |
| 93 | // list<File> |
| 94 | if (/^list<\s*File\s*>$/.test(outputType)) { |
| 95 | if (!Array.isArray(value)) { |
| 96 | issues.push(`expected list<File>, got ${typeof value}`); |
| 97 | return finalize(issues, "files"); |
| 98 | } |
| 99 | if (value.length === 0) { |
| 100 | issues.push("expected at least one File, got empty array"); |
| 101 | return finalize(issues, "files"); |
| 102 | } |
| 103 | // Defensive: reject duplicate paths early — the consumer will fail later anyway. |
| 104 | const seen = new Set(); |
| 105 | for (let i = 0; i < value.length; i++) { |
| 106 | fileShapeIssues(value[i], issues, `file[${i}]: `); |
| 107 | const f = value[i]; |
| 108 | if (typeof f?.path === "string") { |
| 109 | if (seen.has(f.path)) |
| 110 | issues.push(`file[${i}]: duplicate path "${f.path}"`); |
| 111 | seen.add(f.path); |
| 112 | } |
| 113 | } |
| 114 | return finalize(issues, "files"); |
| 115 | } |
| 116 | // Enum: literal set of allowed values. |
| 117 | const enumValues = parseEnumDecl(outputType); |
| 118 | if (enumValues) { |
| 119 | if (typeof value !== "string") { |
| 120 | issues.push(`expected one of [${enumValues.join(", ")}], got ${typeof value}`); |
| 121 | } |
| 122 | else if (!enumValues.includes(value)) { |
| 123 | issues.push(`value "${value.slice(0, 64)}" is not in [${enumValues.join(", ")}]`); |
| 124 | } |
| 125 | return finalize(issues, "enum"); |
| 126 | } |
| 127 | // list<T> |
| 128 | const listMatch = outputType.match(/^list<(.+)>$/); |
| 129 | if (listMatch) { |
| 130 | if (!Array.isArray(value)) { |
| 131 | issues.push(`expected list<${listMatch[1]}>, got ${typeof value}`); |
| 132 | } |
| 133 | else { |
| 134 | const inner = listMatch[1]; |
| 135 | for (let i = 0; i < value.length; i++) { |
| 136 | const issue = declaredPrimitiveCheck(inner, value[i]); |
| 137 | if (issue) |
| 138 | issues.push(`item[${i}]: ${issue}`); |
| 139 | } |
| 140 | } |
| 141 | return finalize(issues, "list"); |
| 142 | } |
| 143 | // Primitive (or json — accepts anything). |
no test coverage detected