Skip to content

feat: add Zod v4 output format - #96

Closed
mnkj0021 wants to merge 1 commit into
google:mainfrom
mnkj0021:feat/95-zod4-format
Closed

mnkj0021 wants to merge 1 commit into
google:mainfrom
mnkj0021:feat/95-zod4-format

Conversation

@mnkj0021

Copy link
Copy Markdown

Fixes #95

Summary

  • add a separate --format zod4 mode while keeping --format zod backward-compatible with Zod v3
  • emit Zod v4-compatible readonly union tuples, enum record types, and single-parameter ZodArray annotations
  • keep runtime schema expressions shared between Zod v3 and v4
  • document both CLI formats and add focused regression coverage

Validation

  • cargo test
  • git diff --check
  • generated zod4 output type-checks with zod@4 + TypeScript
  • existing zod output type-checks with zod@3 + TypeScript
  • cargo run -- --help exposes zod4 as a format option

Comment thread examples/zod4_compat.cddl
@@ -0,0 +1,3 @@
Choice = text / int

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.

All tests in tests/typescript.rs and tests/zod.rs use insta::assert_snapshot! against the shared CDDL fixtures in examples/.

Adding a separate examples/zod4_compat.cddl with manual assert!(stdout.contains(...)) checks has two drawbacks:

  1. Choice = text / int only exercises 1 of the 6 z.ZodUnion code paths (visit_type), leaving the other 5 (visit_array, bounded array occurrences, optional TypeGroupname, optional InlineGroup, and visit_group) untested for zod4.
  2. Every other file in examples/ is snapshot-tested across both tests/typescript.rs and tests/zod.rs.

Could we instead add tests/zod4.rs (mirroring tests/zod.rs with insta::assert_snapshot! over the existing examples/ fixtures) and drop examples/zod4_compat.cddl?

Comment thread src/engines/zod.rs
write!(self.stdout, "z.ZodEnum<{{");
for type2 in t.type_choices.iter().map(|choice| &choice.type1.type2) {
if let cddl::ast::Type2::TextValue { value, .. } = type2 {
write!(self.stdout, "\"{}\":\"{}\";", value, value);

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.

Suggested change
write!(self.stdout, "\"{}\":\"{}\";", value, value);
write!(self.stdout, "\"{}\":\"{}\",", value, value);

Nit: Use , rather than ; to stay consistent with z.ZodObject<{...}> member separators elsewhere in src/engines/zod.rs (and with the original output in #90).

Comment thread src/engines/zod.rs
Comment on lines +49 to +54
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Version {
V3,
V4,
}

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.

Nit: Rather than keeping enum Version private and adding a dedicated with_writers_zod4 method, consider making pub enum Version { V3, V4 } (line 50) and pub fn with_writers_for_version(stdout: Stdout, stderr: Stderr, version: Version) (line 95) public (with with_writers defaulting to Version::V3). That keeps the Engine API extensible without adding a new constructor per version.

sadym-chromium added a commit that referenced this pull request Sep 29, 2026
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 (#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
@sadym-chromium

Copy link
Copy Markdown
Collaborator

please note that the original issue is already addressed: #97

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