Skip to content

Support serialized enum values with Zod-style schema normalization #465

Closed

Description

@robgruen

Follow-up to #69.

The TypeScript validator rejects valid serialized string-enum values such as "EUR", because it checks plain JSON literals against string-enum types. Literal unions, including an enum-derived ${CURRENCY} type, work.

Proposal

Use the enum-to-literal-union approach in the Zod schema generator (getZodSchemaAsTypeScript) as the basis for a JSON-facing normalization step for TypeScript enum schemas. Validate serialized member values while preserving the rest of the schema's checks.

Acceptance criteria

  • Valid enum values pass; unknown values and member names that differ from their values fail.
  • Cover nested objects, arrays, unions, and individual enum-member types.
  • Preserve existing numeric-enum behavior and document the supported scope for computed members.
  • Add regression tests and usage documentation.

Avoid blanket casts, diagnostic suppression, or executing schema code. This tracks native enum support separately from the documented workaround in #69.

Activity

  1. added
    Suggestion
    on Oct 15, 2026
  2. robgruen commented on Nov 6, 2026

    @robgruen
    ContributorAuthor

    /typechat reply

  3. github-actions commented on Nov 6, 2026

    @github-actions
    Contributor

    Your diagnosis matches the TypeScript validator: it generates an assignment of the JSON to the original schema type, so the plain string EUR is not accepted as a string-enum member. The Zod generator does emit enum values as a union of allowed literals, which is a useful representation pattern. Zod still validates through safeParse, so copying that representation alone is not a replacement for TypeScript validation.

    yushiwuzhengjianyuzi already has an open pull request for this issue with recursive normalization, tests, and usage documentation. Continuing that contribution is preferable to a duplicate implementation. The string-union workaround from sho31's original report remains available; deriving the allowed strings from an enum avoids repeating the values, but does not preserve the enum's distinct type identity.

    The existing review identifies two unresolved correctness problems. The recursive mapping can discard function and constructor signatures, potentially accepting JSON objects where those types are required, including inside promise-like types. The diagnostic change can also report missing properties from the wrong type when an application customizes createModuleTextFromJson, the hook that generates validation source code. Those findings directly affect your requirement to preserve the rest of the schema's checks. Resolving them in the existing pull request, while retaining your numeric-enum compatibility and computed-member documentation requirements, is the useful next step.

    Related contributions: #471.

    Automated response requested by the project owner. This does not accept work or authorize implementation.

  4. robgruen commented on Nov 23, 2026

    @robgruen
    ContributorAuthor

    /typechat propose

  5. github-actions commented on Nov 23, 2026

    @github-actions
    Contributor

    Work proposal P1

    Proposal for community discussion, not accepted work or permission to build.

    Recommendation

    I recommend continuing yushiwuzhengjianyuzi's pull request 471 with compiler-assisted JSON-facing normalization: a validation-only representation that accepts an enum's serialized string values. The inspected validator assigns JSON literals to the original schema type, which explains the rejection of EUR. Zod's generator shows how to represent allowed values as a literal union, a type that permits only specified values, but Zod still performs separate runtime validation. Borrowing that representation should not replace TypeScript's compiler checks.

    For the initial change, I recommend supporting compiler-resolved string-enum values in the default JSON validator, including individual members and nested structures, while preserving numeric enums, all unrelated checks, and the original schema and data. This avoids a new dependency and duplicate implementation. The literal-union workaround from sho31's earlier report remains valid for restricting values, but does not preserve the distinction between separate enum types with identical serialized strings. The tradeoff is that recursive normalization is more complex and needs careful compatibility review.

    The current PR diff includes safeguards and regression tests for the earlier callable, constructor, class-instance and custom-generic diagnostic concerns, so those earlier findings should not simply be repeated as current failures. Its nested test still lacks separate invalid-value cases for the lookup map and the successful result alternative. I would complete that coverage and obtain maintainer review of the compatibility protections; two TypeScript builds were also still running when their check statuses were inspected.

    I recommend accepting computed string members only when the compiler resolves them to string literals, without evaluating schema code, and leaving program translation and application-supplied source generators unchanged. Robgruen, do those limits cover your initial use case, or is there a specific computed declaration or custom-generator scenario that must be included? Can the contributor provide the missing rejection cases and completed compatibility evidence for the current PR revision?

    Changes since the previous proposal

    • This is the initial proposal. The frozen conversation contains no preceding public proposal, so these terms are a recommendation rather than an agreed revision.
    • The earlier informational reply discussed an older PR revision. The inspected revision now includes safeguards and regression tests for those findings, so this recommendation focuses on reviewing those changes and closing the remaining coverage gaps rather than proposing duplicate work.

    Direction and readiness

    The approved policy at trusted revision dddb7f7 supports this focused validation improvement through G1 and G2. Preserving unrelated compiler checks, public APIs and existing contributions serves G3; avoiding a new provider or schema-library dependency serves G4; regression coverage and usage documentation serve G5. The proposed boundaries respect N3 and N4 by retaining vendor independence and validation rather than suppressing errors or silently accepting unsupported types.

    This is an initial recommendation, not agreement or accepted work. The owner still needs to confirm that the default-validator and compiler-resolved computed-string limits cover the intended use case. PR 471 needs the missing independent rejection cases and maintainer review of its revised compatibility protections against the trusted baseline. Two inspected TypeScript build jobs were still running. The existing contribution should be reviewed and continued rather than replaced by duplicate implementation.

    Goal

    Validate serialized string-enum values against application-defined TypeScript schemas while retaining unrelated compiler checks and existing integrations.

    Scope

    • Continue yushiwuzhengjianyuzi's PR 471 as the existing contribution, completing compatibility review and focused missing regression coverage instead of creating duplicate work.
    • Add a private, compiler-assisted JSON-facing type representation for compiler-resolved string-enum values in the default TypeScript JSON-assignment generator, including nested structures and individual member types.
    • Constrain recursive normalization and diagnostic adaptation to preserving the original schema's non-enum requirements and meaningful type names. Keep original schema text, JSON payloads, public types and custom generators intact.
    • Document computed-member and generator limitations alongside the original literal-union workaround, and demonstrate numeric and non-enum baseline compatibility through deterministic regressions.

    Non-goals

    • Do not replace TypeScript's compiler validator with Zod, add a Zod dependency for this path, or change Python or existing Zod validation.
    • Do not execute schema code, reconstruct runtime enum objects, coerce data, suppress diagnostics or accept invalid data through blanket casts.
    • Do not change numeric-enum semantics, normalize unresolvable computed values or remove branded, callable, constructor or class-instance constraints.
    • Do not extend string-enum normalization to program translation or application-supplied module generators in this initial proposal.

    Acceptance criteria

    • For string-enum members with compiler-resolved literal values, valid serialized values succeed. Unknown values and names that differ from their values fail. An individual member type accepts only that member's value, not another member's value.
    • Independent positive and negative cases cover nested objects, arrays, tuples, optional and recursive properties, maps with arbitrary keys, and each union alternative. An invalid lookup entry or successful-result value fails even when every other field is valid.
    • Numeric-only schemas and schemas combining numeric and string enums retain the trusted baseline's pass/fail outcomes for declared, unknown and computed numeric values with the same compiler version. No numeric string coercion is introduced.
    • Unrelated required and excess properties, ordinary literal unions, branded strings, private or protected class-member constraints, callable and constructor requirements, Promise-like method requirements, primitive wrapper types and explicit object constraints retain their baseline outcomes in both enum-free and enum-bearing schemas.
    • Missing-property diagnostics identify the actual nested or generic target and its required properties. Internal helper aliases are hidden without changing property identifiers, quoted data values or literal-bearing type arguments.
    • Original schema text, validated JSON payloads, public validator types and schema-error checks remain unchanged apart from the intended default-path string-enum acceptance. Application-supplied source generators, including the program translator's generator, retain their existing behavior.
    • Normalization derives string-enum constants through the compiler and retains compiler type checking. It introduces no Zod dependency for this path, executes no schema code and uses no blanket casts or diagnostic suppression to bypass validation.
    • Usage documentation demonstrates values versus member names, preserves the literal-union workaround and its contributor credit, and states the compiler-resolved computed-string and default-generator limits. Deterministic regression results identify the exact PR revision, compiler and runtime versions, and any separate baseline failures.

    Open questions

    • Robgruen, does the default JSON-assignment validator cover your initial use case, with program translation and custom createModuleTextFromJson implementations left unchanged?
    • Are there specific computed string-enum declarations your use case requires beyond values the TypeScript compiler resolves to string literals? If so, please include a minimal schema in this issue.
    • Can the contributor supply independent invalid-value cases for lookup entries and the successful result alternative, plus completed compatibility evidence for the inspected PR revision against the trusted baseline, so a maintainer can review the revised safeguards?

    Evidence limits and assumptions

    • The complete frozen issue conversation, trusted source and read-only PR evidence were inspected. No code, reproductions, tests or installations were executed, and no runtime comparison with the trusted baseline was performed. Contributor-reported test passes were not independently verified.
    • Check statuses were inspected, but two TypeScript build jobs were still in progress and job logs were not inspected. Those statuses do not establish a completed compatibility review or approval of the current PR revision.

    Related contributions: #471.

    Not eligible for acceptance yet. Discuss the remaining questions and request /typechat propose again.

    Proposal run | Approved scope policy

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

    Suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions