Repository navigation
Description
Problem
The TypeScript JSON validator rejects an enum's exact declared serialized value when the string contains a lone surrogate followed by U+FEFF (BOM). Schema preflight succeeds. This is a core validation correctness issue, not just a browser-adapter discrepancy.
Confirmed on main at 0ef51b05e6f9c0373870c2754070e7f12b470bf7, Node.js 24.22.1, and TypeScript 7.0.2. The pinned WASM compiler 7.1.0-dev.20261030.1 also rejects it in the separate browser experiment.
Minimal reproduction
Run against a freshly built TypeChat TypeScript package (no model or credentials required):
import { typeChatSchemaValidation } from "typechat";
import { createTypeScriptJsonValidator } from "typechat/ts";
const schema = String.raw`
export enum E { X = "\ud800\uFEFFx" }
export interface Probe { value: E; }
`;
const validator = createTypeScriptJsonValidator(schema, "Probe");
try {
console.log("preflight", validator[typeChatSchemaValidation]());
console.log("declared", validator.validate({ value: "\ud800\uFEFFx" }));
console.log("altered", validator.validate({ value: "\ud800x" }));
} finally {
validator.close();
}The declared value has UTF-16 code units D800 FEFF 0078; the altered value has D800 0078.
| Check | Expected | Actual |
|---|---|---|
| Schema preflight | Success | Success |
| Exact declared value | Success | Failure |
| Altered value without U+FEFF | Failure | Failure |
Control: an enum value consisting of FEFF 0078 (leading BOM without a lone surrogate) validates; plain x correctly fails. Numeric .1 and 1e20 controls did not fail end-to-end and are not claimed as affected.
Evidence and prior work
- Discovered during independent conformance checks for Investigate an opt-in browser/WASM adapter for TypeScript validation #536 / Prototype browser/WASM validation in isolated workers #538. The portable reproduction in the browser branch also reproduces on untouched main. Native/WASM parity alone does not establish correct validation.
- Existing upstream report by Marc Rousavy (@mrousavy): Native API: Wtf8Decoder drops U+FEFF from string literals and template text TypeScript#65458. It documents
Wtf8Decoderdropping U+FEFF in compiler API string payloads, including around lone-surrogate segments. This TypeChat issue tracks the downstream effect and any viable correction using current public compiler APIs; it is not a duplicate upstream report. - TypeChat JSON Validator Incorrectly Rejects Valid Enums #69 and Support serialized enum values with Zod-style schema normalization #465 cover the earlier general serialized-enum support. That support must be preserved.
- Checked all-state TypeChat issues for Unicode, BOM, surrogate, enum, and the upstream issue number, plus all 13 open PRs and matching PR discussions; no separate matching bug or fix was found.
Acceptance criteria
- Exact declared string-enum values retain their UTF-16 code units and validate; altered values still fail.
- Add offline regressions covering high and low lone surrogates, BOM positions, ordinary and astral Unicode controls, nested/container/member enums, and constant-folded members where supported.
- Preserve numeric enums, branded strings, call/construct/nominal constraints, diagnostics, schema preflight, and repair behavior.
- Use supported compiler/public APIs without weakening validation, suppressing diagnostics, accepting arbitrary strings, evaluating schema code, globally patching decoders, or modifying installed dependencies.
- Keep a main-based core fix separate from the browser adapter. If a valid correction requires a compiler upgrade/fork or a broader API assumption, document the blocker for an owner decision.
Activity