Skip to content

Commit 04bcbae

Browse files
Merge pull request #1261 from paoloricciuti/fix-or-migration
feat: add `or` migration
2 parents c39d0c4 + 17df5ca commit 04bcbae

File tree

8 files changed

+27
-0
lines changed

8 files changed

+27
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { z } from "zod";
2+
3+
const Schema1 = z.string().or(z.number());
4+
const Schema2 = z.string().or(z.number()).or(z.boolean());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as v from "valibot";
2+
3+
const Schema1 = v.union([v.string(), v.number()]);
4+
const Schema2 = v.union([v.union([v.string(), v.number()]), v.boolean()]);

codemod/zod-to-valibot/src/test-setup.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ defineTests(transform, [
5151
'object-strict',
5252
'object-strip',
5353
'optional-schema',
54+
'or',
5455
'parsing',
5556
'readonly',
5657
'record-schema',

codemod/zod-to-valibot/src/transform/schemas-and-links/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export const ZOD_METHODS = [
144144
'keyof',
145145
'omit',
146146
'optional',
147+
'or',
147148
'merge',
148149
'nullable',
149150
'nullish',

codemod/zod-to-valibot/src/transform/schemas-and-links/methods/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from './nullable';
1111
export * from './nullish';
1212
export * from './omit';
1313
export * from './optional';
14+
export * from './or';
1415
export * from './parse';
1516
export * from './parseAsync';
1617
export * from './partial';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './or';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import j from 'jscodeshift';
2+
3+
export function transformOr(
4+
valibotIdentifier: string,
5+
schemaExp: j.CallExpression | j.MemberExpression | j.Identifier,
6+
inputArgs: j.CallExpression['arguments']
7+
) {
8+
return j.callExpression(
9+
j.memberExpression(j.identifier(valibotIdentifier), j.identifier('union')),
10+
[j.arrayExpression([schemaExp, ...inputArgs])]
11+
);
12+
}

codemod/zod-to-valibot/src/transform/schemas-and-links/schemas-and-links.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
transformNullish,
2424
transformOmit,
2525
transformOptional as transformOptionalMethod,
26+
transformOr,
2627
transformParse,
2728
transformParseAsync,
2829
transformPartial,
@@ -359,6 +360,8 @@ function toValibotMethodExp(
359360
return transformKeyof(...args);
360361
case 'omit':
361362
return transformOmit(...args);
363+
case 'or':
364+
return transformOr(...args);
362365
case 'optional':
363366
return transformOptionalMethod(...args);
364367
case 'merge':

0 commit comments

Comments
 (0)