Skip to content

Commit 32407df

Browse files
arhxamclaude
andauthored
fix(shared): lenient JSON parser deletes commas inside string values (pingdotgg#5025)
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 162e774 commit 32407df

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎packages/shared/src/schemaJson.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ Done.`),
5757
expect(() => decodeLenientJson('{ "enabled": true,, }')).toThrow();
5858
});
5959

60+
it("preserves commas before brackets inside string values", () => {
61+
// A comma inside a string value that happens to precede `}`/`]` must not
62+
// be stripped as if it were a trailing comma.
63+
expect(decodeLenientJson('{"note":"a,]"}')).toEqual({ note: "a,]" });
64+
expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
65+
// Genuine trailing commas are still removed.
66+
expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });
67+
});
68+
6069
it("formats schema failures with paths without exposing invalid values", () => {
6170
const decodeCredential = decodeJsonResult(Schema.Struct({ token: Schema.Number }));
6271
const decoded = decodeCredential('{"token":"credential=secret-value"}');

‎packages/shared/src/schemaJson.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,14 @@ const parseLenientJsonGetter = SchemaGetter.onSome((input: string) => {
190190
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
191191
);
192192

193-
// Strip trailing commas before `}` or `]`.
194-
stripped = stripped.replace(/,(\s*[}\]])/g, "$1");
193+
// Strip trailing commas before `}` or `]`. The alternation preserves quoted
194+
// strings so a comma inside a string value (e.g. `{"note":"a,]"}`) is not
195+
// mistaken for a trailing comma and removed.
196+
stripped = stripped.replace(
197+
/("(?:[^"\\]|\\.)*")|,(\s*[}\]])/g,
198+
(match, stringLiteral: string | undefined, bracket: string | undefined) =>
199+
stringLiteral ? match : (bracket ?? ""),
200+
);
195201

196202
return decodeJsonString(stripped).pipe(
197203
Effect.map(Option.some),

0 commit comments

Comments
 (0)