Skip to content

TypeScript validator rejects string enum values with a BOM after a lone surrogate #541

In review

Description

@robgruen

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

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

  1. added
    Bug
    Language: TypeScript
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugLanguage: TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions