Skip to content

Commit 56c2bc2

Browse files
authored
Merge pull request #42 from githubnext/rig-sampler/22-config-normalizer-00b5e5f3ce892494
[rig-sampler] Improve missing-required-field error messages with richer type descriptions
2 parents 37acb69 + 8baf866 commit 56c2bc2

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

skills/rig/rig.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ function validateSchema(value: unknown, schema: Schema, path: string, optional:
14551455
const fieldPath = `${path}.${key}`;
14561456
const isOptional = isOptionalSchema(fieldSchema);
14571457
if (fieldValue === undefined && !isOptional) {
1458-
const expectedType = "type" in fieldSchema ? (fieldSchema as { type: string }).type : "enum" in fieldSchema ? "enum value" : "value";
1458+
const expectedType = describeSchemaType(fieldSchema);
14591459
return { ok: false, error: `${fieldPath}: missing required field (expected ${expectedType})` };
14601460
}
14611461
const result = validateSchema(fieldValue, fieldSchema, fieldPath, false);
@@ -1789,6 +1789,25 @@ function previewWithFirstDiff(value: string | undefined, expected: string, maxPr
17891789
return `${prefixEllipsis}${preview}${suffixEllipsis}`;
17901790
}
17911791

1792+
function describeSchemaType(schema: Schema): string {
1793+
if ("nullable" in schema && schema.nullable === true) {
1794+
return `${describeSchemaType((schema as NullableSchema).inner)} | null`;
1795+
}
1796+
if ("type" in schema) {
1797+
return (schema as { type: string }).type;
1798+
}
1799+
if ("enum" in schema) {
1800+
return (schema as EnumSchema).enum.map((v: Json) => JSON.stringify(v)).join(" | ");
1801+
}
1802+
if ("items" in schema) {
1803+
return "array";
1804+
}
1805+
if ("properties" in schema || "additionalProperties" in schema) {
1806+
return "object";
1807+
}
1808+
return "value";
1809+
}
1810+
17921811
function bad(path: string, expected: string, actual: unknown): ValidationResult {
17931812
const actualType = actual === null ? "null" : Array.isArray(actual) ? "array" : typeof actual;
17941813
const actualRepr = JSON.stringify(actual);

src/rig.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,4 +1246,37 @@ describe("missing required field error message", () => {
12461246
const result = analyzeResponse(JSON.stringify({ name: "Alice" }), schema, "test", 1);
12471247
expect(result.ok).toBe(true);
12481248
});
1249+
1250+
it("reports a missing required array field with type 'array'", () => {
1251+
const schema = s.object({ items: s.array(s.string), name: s.string });
1252+
const result = analyzeResponse(JSON.stringify({ name: "x" }), schema, "test", 1);
1253+
expect(result.ok).toBe(false);
1254+
if (!result.ok) {
1255+
expect(result.error.message).toContain("missing required field");
1256+
expect(result.error.message).toContain("$.items");
1257+
expect(result.error.message).toContain("array");
1258+
}
1259+
});
1260+
1261+
it("reports a missing required nullable field with '<inner> | null'", () => {
1262+
const schema = s.object({ score: s.nullable(s.number), name: s.string });
1263+
const result = analyzeResponse(JSON.stringify({ name: "x" }), schema, "test", 1);
1264+
expect(result.ok).toBe(false);
1265+
if (!result.ok) {
1266+
expect(result.error.message).toContain("missing required field");
1267+
expect(result.error.message).toContain("$.score");
1268+
expect(result.error.message).toContain("number | null");
1269+
}
1270+
});
1271+
1272+
it("reports a missing required enum field with enum values", () => {
1273+
const schema = s.object({ status: s.enum("open", "closed"), name: s.string });
1274+
const result = analyzeResponse(JSON.stringify({ name: "x" }), schema, "test", 1);
1275+
expect(result.ok).toBe(false);
1276+
if (!result.ok) {
1277+
expect(result.error.message).toContain("missing required field");
1278+
expect(result.error.message).toContain("$.status");
1279+
expect(result.error.message).toContain('"open"');
1280+
}
1281+
});
12491282
});

0 commit comments

Comments
 (0)