Skip to content

Commit 6ff3152

Browse files
committed
Prepare Zod to Valibot codemod for npm publishing
1 parent 2471d52 commit 6ff3152

5 files changed

Lines changed: 117 additions & 2 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to the library will be documented in this file.
4+
5+
## v0.1.0 (December 06, 2025)
6+
7+
- Initial release

codemod/zod-to-valibot/LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) Fabian Hiller and Elton Lobo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

codemod/zod-to-valibot/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Zod to Valibot Codemod
2+
3+
Official codemod for automatically converting Zod schemas to Valibot schemas.
4+
5+
## Usage
6+
7+
```bash
8+
npx zod-to-valibot src/**/*
9+
```
10+
11+
## Options
12+
13+
```bash
14+
# Dry run (preview changes)
15+
npx zod-to-valibot --dry src/**/*
16+
17+
# Verbose output
18+
npx zod-to-valibot --verbose=2 src/**/*
19+
20+
# Use a different parser (default is --parser=ts)
21+
npx zod-to-valibot --parser=babel src/**/*
22+
23+
# Use different extensions (default is --extensions=ts,tsx,js,jsx)
24+
npx zod-to-valibot --extensions=ts src/**/*
25+
```
26+
27+
For all available options, see [jscodeshift documentation](https://github.com/facebook/jscodeshift#options).
28+
29+
## What Gets Converted
30+
31+
The codemod converts Zod schemas to Valibot, including:
32+
33+
- Basic schemas (`string`, `number`, `boolean`, `date`, `bigint`)
34+
- Validation rules (`email`, `min`, `max`, etc.)
35+
- Coercion (`z.coerce.*``v.pipe(v.unknown(), v.toX())`)
36+
- Objects, arrays, unions, optionals, and more
37+
38+
## Support
39+
40+
For issues or questions, open an issue on the [Valibot repository](https://github.com/open-circle/valibot).

codemod/zod-to-valibot/cli.mjs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env node
2+
3+
import { execSync } from 'child_process';
4+
import path from 'path';
5+
import { fileURLToPath } from 'url';
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
const transformPath = path.join(__dirname, 'dist', 'index.mjs');
9+
const jscodeshift = path.join(__dirname, 'node_modules', '.bin', 'jscodeshift');
10+
11+
const args = process.argv.slice(2);
12+
const hasParserArg = args.some(
13+
(arg) => arg === '--parser' || arg.startsWith('--parser=')
14+
);
15+
const hasExtensionsArg = args.some((arg) => arg.startsWith('--extensions'));
16+
17+
const finalArgs = [...args];
18+
if (!hasParserArg) {
19+
finalArgs.unshift('--parser=ts');
20+
}
21+
if (!hasExtensionsArg) {
22+
finalArgs.unshift('--extensions=ts,tsx,js,jsx');
23+
}
24+
25+
if (args.length === 0) {
26+
console.log(`
27+
Usage: zod-to-valibot [options] <files>
28+
29+
Convert Zod schemas to Valibot schemas
30+
31+
Examples:
32+
zod-to-valibot src/**/*.ts
33+
zod-to-valibot --dry src/schemas.ts
34+
zod-to-valibot --no-babel src/**/*.{ts,tsx}
35+
36+
Common jscodeshift options:
37+
--dry Run without making changes
38+
--print Print output
39+
--verbose=2 Increase verbosity
40+
--parser=ts Specify parser (default: babel)
41+
42+
For all options, see: jscodeshift --help
43+
`);
44+
process.exit(0);
45+
}
46+
47+
try {
48+
const command = `"${jscodeshift}" -t "${transformPath}" ${finalArgs.join(' ')}`;
49+
execSync(command, { stdio: 'inherit' });
50+
} catch (error) {
51+
process.exit(1);
52+
}

codemod/zod-to-valibot/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "zod-to-valibot",
2+
"name": "@valibot/zod-to-valibot",
33
"description": "Official codemod for converting Zod schemas to Valibot",
44
"version": "0.1.0",
55
"license": "MIT",
@@ -18,9 +18,16 @@
1818
"type": "module",
1919
"main": "./dist/index.mjs",
2020
"types": "./dist/index.d.mts",
21+
"bin": {
22+
"zod-to-valibot": "./cli.mjs"
23+
},
2124
"files": [
22-
"dist"
25+
"dist",
26+
"cli.mjs"
2327
],
28+
"publishConfig": {
29+
"access": "public"
30+
},
2431
"scripts": {
2532
"build": "tsdown",
2633
"codemod": "jscodeshift",

0 commit comments

Comments
 (0)