fix(shared): lenient JSON parser deletes commas inside string values (#5025)
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
1 parent
162e774294
commit
32407df9c5
2 files changed
+17
-2
No files matched your search
@@ -57,6 +57,15 @@ Done.`),
|
||||
expect(() => decodeLenientJson('{ "enabled": true,, }')).toThrow();
|
||||
});
|
||||
|
||||
it("preserves commas before brackets inside string values", () => {
|
||||
// A comma inside a string value that happens to precede `}`/`]` must not
|
||||
// be stripped as if it were a trailing comma.
|
||||
expect(decodeLenientJson('{"note":"a,]"}')).toEqual({ note: "a,]" });
|
||||
expect(decodeLenientJson('{"list":["x,}"]}')).toEqual({ list: ["x,}"] });
|
||||
// Genuine trailing commas are still removed.
|
||||
expect(decodeLenientJson('{"values":[1, 2,],}')).toEqual({ values: [1, 2] });
|
||||
});
|
||||
|
||||
it("formats schema failures with paths without exposing invalid values", () => {
|
||||
const decodeCredential = decodeJsonResult(Schema.Struct({ token: Schema.Number }));
|
||||
const decoded = decodeCredential('{"token":"credential=secret-value"}');
|
||||
|
||||
@@ -190,8 +190,14 @@ const parseLenientJsonGetter = SchemaGetter.onSome((input: string) => {
|
||||
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
|
||||
);
|
||||
|
||||
// Strip trailing commas before `}` or `]`.
|
||||
stripped = stripped.replace(/,(\s*[}\]])/g, "$1");
|
||||
// Strip trailing commas before `}` or `]`. The alternation preserves quoted
|
||||
// strings so a comma inside a string value (e.g. `{"note":"a,]"}`) is not
|
||||
// mistaken for a trailing comma and removed.
|
||||
stripped = stripped.replace(
|
||||
/("(?:[^"\\]|\\.)*")|,(\s*[}\]])/g,
|
||||
(match, stringLiteral: string | undefined, bracket: string | undefined) =>
|
||||
stringLiteral ? match : (bracket ?? ""),
|
||||
);
|
||||
|
||||
return decodeJsonString(stripped).pipe(
|
||||
Effect.map(Option.some),
|
||||
|
||||
Reference in new issue
Block a user