fix: serialize const string values to valid JSON#848
Open
spokodev wants to merge 1 commit into
Open
Conversation
A `const` string containing a backslash, control character, or double
quote was serialized into corrupted or invalid JSON, even though the input
matched the schema:
build({ properties: { v: { const: 'a\\b' } } })({ v: 'a\\b' })
// -> {"v":"a\b"} -> JSON.parse yields "a\b" (a backspace)
build({ properties: { v: { const: 'a\tb' } } })({ v: 'a\tb' })
// -> {"v":"a<TAB>b"} -> JSON.parse throws "Bad control character"
The value of `JSON.stringify(schema.const)` was embedded into a
single-quoted string literal in the generated function source while only
single quotes were escaped, so the `Function` constructor then re-unescaped
backslashes and control characters. `JSON.stringify` the JSON text once more
to produce a properly escaped JS string literal, mirroring how default
values are already emitted. `SINGLE_TICK` is now unused and removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A
conststring containing a backslash, control character, or double quote is serialized into corrupted or invalid JSON, even when the input matches the schema:This reproduces for
constat the top level, in properties, in arrayitems, withtypeset, and as a nullable.enumanddefaultwith the same values serialize correctly — onlyconstis affected.Cause
JSON.stringify(schema.const)produces correct JSON text, but it is then embedded into a single-quoted JS string literal in the generated function source, escaping only single quotes (SINGLE_TICK):When the
Functionconstructor evaluates that source,\\collapses back to\, a\tbecomes a raw tab, and an embedded"breaks the JSON string. (Introduced by #658, which handled only the single-quote case.)Fix
JSON.stringifythe JSON text a second time to produce a properly escaped JS string literal — the same approach already used for default values:SINGLE_TICKis now unused and removed. This also still covers the original single-quote case from #658.Tests
Added a case to
test/const.test.jsover['back\\slash', 'tab\there', 'quote"here', 'new\nline', "tick's"]asserting the output equalsJSON.stringify({ foo: value })and round-trips (red before, green after).Full unit suite green (473/473). Note: I ran it via
node --testdirectly, becausec8's bundledyargscannot load under Node 26 (require is not defined in ES module scope) — unrelated to this change.npm run test:typescriptpasses.