Skip to content

feat!: switch to Zod v4 types - #97

Merged
sadym-chromium merged 2 commits into
mainfrom
sadym/95
Sep 29, 2026
Merged

sadym-chromium merged 2 commits into
mainfrom
sadym/95

Conversation

@sadym-chromium

@sadym-chromium sadym-chromium commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Alternative to #96

  • Emit Zod v4-compatible TypeScript type annotations in --format zod
    and document the --format <type-script|zod> CLI flag in README.md.
  • Commit 4cc5d36 (fix: generate valid zod v3 types for enums, unions, and non-empty arrays #92) switched enum, union, and non-empty array type
    annotations to Zod v3 syntax, breaking compilation against Zod v4
    even though --format zod already targets Zod v4 (e.g., z.ZodURL /
    z.url()).
  • Emit z.ZodUnion<readonly [...]>, record-based
    z.ZodEnum<{ [K]: K }>, and single-parameter z.ZodArray<T>.

Fixes #95

- Add `--format zod4` to emit Zod v4-compatible TypeScript type
  annotations while keeping `--format zod` for Zod v3 compatibility.
- Zod v3 and Zod v4 have mutually exclusive generic type constraints
  for unions, string enums, and non-empty arrays, preventing generated
  schemas with explicit type annotations from compiling under Zod v4.
- In `zod4` mode, emit `z.ZodUnion<readonly [...]>`, record-based
  `z.ZodEnum<{ [K]: K }>`, and single-parameter `z.ZodArray<T>`.

Fixes #95

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. In src/engines/zod.rs visit_identifier, make the uri output depend on the version: V3 should emit z.ZodString / z.string().url(), and V4 should keep z.ZodURL / z.url(). The README now says --format zod targets Zod v3, but z.url() and z.ZodURL only exist in Zod v4, so v3 output breaks for any CDDL that uses uri (see prelude_text_types).

  2. In tests/zod4.rs, don't copy the whole test! macro and file list from tests/zod.rs. Instead, add a version argument to one shared macro (or add a zod4_ variant inside tests/zod.rs). With two copies, it's easy to add a new example to one format and forget the other.

  3. In src/main.rs, merge the EngineType::Zod and EngineType::Zod4 arms into one: pick the Version first, then call with_writers_and_version once. As written, the preamble/visit/postamble sequence is copied, and the two copies can drift apart.

  4. Add Debug to the derives on the new public pub enum Version, or implement Default returning V3, to match other public types. with_writers could then call Version::default() instead of hard-coding V3.

@sadym-chromium

Copy link
Copy Markdown
Collaborator Author
  1. In src/engines/zod.rs visit_identifier, make the uri output depend on the version: V3 should emit z.ZodString / z.string().url(), and V4 should keep z.ZodURL / z.url(). The README now says --format zod targets Zod v3, but z.url() and z.ZodURL only exist in Zod v4, so v3 output breaks for any CDDL that uses uri (see prelude_text_types).
  2. In tests/zod4.rs, don't copy the whole test! macro and file list from tests/zod.rs. Instead, add a version argument to one shared macro (or add a zod4_ variant inside tests/zod.rs). With two copies, it's easy to add a new example to one format and forget the other.
  3. In src/main.rs, merge the EngineType::Zod and EngineType::Zod4 arms into one: pick the Version first, then call with_writers_and_version once. As written, the preamble/visit/postamble sequence is copied, and the two copies can drift apart.
  4. Add Debug to the derives on the new public pub enum Version, or implement Default returning V3, to match other public types. with_writers could then call Version::default() instead of hard-coding V3.

I dropped support of zod v3 completely. PTAL

@sadym-chromium sadym-chromium changed the title feat: add --format zod4 for Zod v4 type annotations feat!: emit Zod v4 types for enums, unions, and non-empty arrays Sep 29, 2026
@sadym-chromium sadym-chromium changed the title feat!: emit Zod v4 types for enums, unions, and non-empty arrays feat!: switch to Zod v4 types Sep 29, 2026
@sadym-chromium
sadym-chromium enabled auto-merge (squash) September 29, 2026 14:52
@sadym-chromium
sadym-chromium merged commit 716b788 into main Sep 29, 2026
7 checks passed
@sadym-chromium
sadym-chromium deleted the sadym/95 branch September 29, 2026 14:57
sadym-chromium added a commit that referenced this pull request Sep 30, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.2.0](cddlconv-v0.1.12...cddlconv-v0.2.0)
(2026-09-29)


### ⚠ BREAKING CHANGES

* switch to Zod v4 types
([#97](#97))

### Features

* switch to Zod v4 types
([#97](#97))
([716b788](716b788)),
refs [#95](#95)

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --format zod4 to support Zod v4 TypeScript type annotations

2 participants