Skip to content

Commit de08bae

Browse files
fix(validation): improve minLength error message to include actual string length
When a string fails minLength validation, the previous error always said 'got empty string' even when the string was non-empty but too short. Now the message distinguishes the two cases: - empty string: 'expected string with minLength N, got empty string' - short string: 'expected string with minLength N, got string of length M' Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 904f5fc commit de08bae

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

skills/rig/rig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,8 @@ function validateSchema(value: unknown, schema: Schema, path: string, optional:
18151815
if (typeof value !== "string") return bad(path, "string", value);
18161816
const { minLength, format } = schema as StringSchema;
18171817
if (minLength !== undefined && value.length < minLength) {
1818-
return { ok: false, error: `${path}: expected string with minLength ${minLength}, got empty string` };
1818+
const gotDesc = value.length === 0 ? "empty string" : `string of length ${value.length}`;
1819+
return { ok: false, error: `${path}: expected string with minLength ${minLength}, got ${gotDesc}` };
18191820
}
18201821
if (format === "uri") {
18211822
try { new URL(value); } catch { return { ok: false, error: `${path}: expected a valid URL, got ${JSON.stringify(value)}` }; }

src/rig.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,11 +1338,30 @@ describe("s.nonEmptyString", () => {
13381338
expect(result.ok).toBe(true);
13391339
});
13401340

1341-
it("rejects empty strings", () => {
1341+
it("rejects empty strings with 'empty string' in message", () => {
13421342
const result = analyzeResponse(JSON.stringify(""), s.nonEmptyString, "test", 1);
13431343
expect(result.ok).toBe(false);
13441344
if (!result.ok) {
13451345
expect(result.error.message).toContain("minLength");
1346+
expect(result.error.message).toContain("empty string");
1347+
}
1348+
});
1349+
1350+
it("rejects short non-empty strings with actual length in message", () => {
1351+
// s.object with a string field that has minLength 5 via s.string-based shape
1352+
const schema = s.object({ code: s.nonEmptyString });
1353+
// Provide a non-empty but 2-char value to trigger the too-short branch
1354+
// We need a schema with minLength > 2; build one from scratch via toJsonSchema round-trip isn't possible,
1355+
// so verify via analyzeResponse with a direct schema literal accepted by the public API.
1356+
// Use s.nonEmptyString (minLength:1) against "" for the empty branch,
1357+
// and build a custom schema object for minLength > 1:
1358+
const minLen5Schema = { type: "string" as const, minLength: 5 };
1359+
const wrappedSchema = s.object({ code: minLen5Schema as ReturnType<typeof s.string> });
1360+
const result = analyzeResponse(JSON.stringify({ code: "ab" }), wrappedSchema, "test", 1);
1361+
expect(result.ok).toBe(false);
1362+
if (!result.ok) {
1363+
expect(result.error.message).toContain("minLength 5");
1364+
expect(result.error.message).toContain("length 2");
13461365
}
13471366
});
13481367
});

0 commit comments

Comments
 (0)