Skip to content

fix(to-valibot): support z.brand() in codemod (#1501) - #1542

Open
xianjianlf2 wants to merge 2 commits into
open-circle:mainfrom
xianjianlf2:fix/codemod-brand-1501
Open

fix(to-valibot): support z.brand() in codemod (#1501)#1542
xianjianlf2 wants to merge 2 commits into
open-circle:mainfrom
xianjianlf2:fix/codemod-brand-1501

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Jul 16, 2026

Copy link
Copy Markdown

Overview

The @valibot/zod-to-valibot codemod had no handler for Zod's .brand() method. Because the method went unrecognized, it was left in place as a call on the transformed schema, producing invalid output like v.string()("Name") (a schema object called as a function).

Fixes #1501

Fix

Added a brand method handler that maps schema.brand(name) into a pipe action, mirroring how the existing transform method is handled. It uses the shared addToPipe helper so the brand is appended to any existing pipe:

z.string().brand("StationNumber")   ->   v.pipe(v.string(), v.brand("StationNumber"))

The handler is wired into ZOD_METHODS (constants), methods/index.ts, and the schemas-and-links dispatcher.

Tests

Added a brand test fixture (__testfixtures__/brand/) covering a basic brand, a brand after a validator chain, a brand on a number schema, and a brand on a linked schema, and registered it in test-setup.test.ts. The full codemod suite passes (72/72).

Summary by CodeRabbit

  • New Features
    • Added support for converting Zod branded schemas into equivalent Valibot branded schemas.
    • Supports brands on strings, UUID-validated values, constrained numbers, linked schemas, and type-only string literals.
    • Brand names can be inferred from generic type parameters when explicit arguments are not provided.
    • Branding is preserved through existing schema validation pipelines.
  • Tests
    • Added coverage for multiple branded schema patterns and included the new brand conversion scenario in the transformation test suite.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. fix A smaller enhancement or bug fix labels Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: bd8f6dab-4f9b-44e9-952b-f68f055d1a1e

📥 Commits

Reviewing files that changed from the base of the PR and between 5d399a9 and 20ea02b.

📒 Files selected for processing (8)
  • codemod/zod-to-valibot/__testfixtures__/brand/input.ts
  • codemod/zod-to-valibot/__testfixtures__/brand/output.ts
  • codemod/zod-to-valibot/src/test-setup.test.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/constants.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/brand.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/index.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/index.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/schemas-and-links.ts
🚧 Files skipped from review as they are similar to previous changes (8)
  • codemod/zod-to-valibot/src/test-setup.test.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/index.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/constants.ts
  • codemod/zod-to-valibot/testfixtures/brand/output.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/schemas-and-links.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/index.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/brand.ts
  • codemod/zod-to-valibot/testfixtures/brand/input.ts

Walkthrough

The Zod-to-Valibot codemod now recognizes the brand method and transforms it into a Valibot brand pipeline step. It supports explicit brand arguments and generic string literal type parameters. Fixtures and test registration cover strings, UUID chains, constrained numbers, derived schemas, and type-only brands.

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes adding z.brand() support to the Zod-to-Valibot codemod.
Linked Issues check ✅ Passed The PR fixes issue #1501 by converting Zod brand calls to v.pipe(..., v.brand(...)) and covers explicit and type-only brand names.
Out of Scope Changes check ✅ Passed The changes are limited to brand transformation logic, registration, exports, and focused test fixtures.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/brand.ts`:
- Around line 4-8: Add JSDoc immediately above the exported transformBrand
function describing its purpose, and add the // `@__NO_SIDE_EFFECTS__` annotation
required for this pure factory transformation. Keep the function signature and
implementation unchanged.
- Around line 1-2: Update the relative ESM specifiers in brand.ts,
brand/index.ts, and methods/index.ts to include the .ts extension: use
../../helpers.ts, ./brand.ts, and ./brand.ts respectively. Leave the jscodeshift
package import unchanged.
- Around line 4-20: Update transformBrand to handle Zod’s type-only brand<...>()
form instead of forwarding only runtime args: preserve the brand name by
converting the type argument to the required Valibot name, or explicitly reject
unsupported type-only branding. Add a fixture covering
z.string().brand<"UserId">() and verify the resulting transform behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2ecf73f0-45e6-4781-ac57-4211df4fe6eb

📥 Commits

Reviewing files that changed from the base of the PR and between 32247b3 and de44991.

📒 Files selected for processing (8)
  • codemod/zod-to-valibot/__testfixtures__/brand/input.ts
  • codemod/zod-to-valibot/__testfixtures__/brand/output.ts
  • codemod/zod-to-valibot/src/test-setup.test.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/constants.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/brand.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/index.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/methods/index.ts
  • codemod/zod-to-valibot/src/transform/schemas-and-links/schemas-and-links.ts

Comment thread codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/brand.ts Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 8 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread codemod/zod-to-valibot/src/transform/schemas-and-links/methods/brand/brand.ts Outdated
@xianjianlf2

Copy link
Copy Markdown
Author

Addressed the review feedback and rebased the branch onto the latest main in b8efed5.

Changes:

  • Added .ts extensions for the new brand transformer exports/imports. In methods/index.ts this has to be ./brand/index.ts because brand is a directory, not a sibling brand.ts file.
  • Added JSDoc and // @__NO_SIDE_EFFECTS__ for transformBrand.
  • Preserved Zod's type-only string-literal brand form, so z.string().brand<"UserId">() now transforms to v.pipe(v.string(), v.brand("UserId")) instead of v.brand().
  • Added a brand fixture for that type-only case.

Local checks:

  • pnpm --filter @valibot/zod-to-valibot test (72 tests passed)
  • pnpm --filter @valibot/zod-to-valibot build
  • git diff --check

@xianjianlf2
xianjianlf2 force-pushed the fix/codemod-brand-1501 branch from b8efed5 to 63c87f3 Compare August 3, 2026 04:20
The codemod did not recognize Zod's .brand() method, so
z.string().brand("X") was emitted as the broken v.string()("X").
Add a brand method handler that maps it into a pipe action,
producing v.pipe(v.string(), v.brand("X")), matching how the
existing transform method is handled.

Fixes open-circle#1501
@xianjianlf2
xianjianlf2 force-pushed the fix/codemod-brand-1501 branch from 63c87f3 to 20ea02b Compare August 7, 2026 04:31
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix A smaller enhancement or bug fix size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codemod breaks z.brand

1 participant