Skip to content

Commit 2471d52

Browse files
committed
Refactor coercion schemas to use new Valibot methods
1 parent 1d9bedc commit 2471d52

File tree

14 files changed

+87
-140
lines changed

14 files changed

+87
-140
lines changed
Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import * as v from "valibot";
22

3-
const Schema1 = v.pipe(v.any(), v.transform(input => {
4-
try {
5-
return BigInt(input);
6-
} catch {
7-
return input;
8-
}
9-
}), v.bigint());
10-
const Schema2 = v.pipe(v.any(), v.transform(input => {
11-
try {
12-
return BigInt(input);
13-
} catch {
14-
return input;
15-
}
16-
}), v.bigint());
17-
const Schema3 = v.pipe(v.any(), v.transform(input => {
18-
try {
19-
return BigInt(input);
20-
} catch {
21-
return input;
22-
}
23-
}), v.bigint(), v.gtValue(1n));
24-
const Schema4 = v.pipe(v.any(), v.transform(input => {
25-
try {
26-
return BigInt(input);
27-
} catch {
28-
return input;
29-
}
30-
}), v.bigint(), v.ltValue(2n));
3+
const Schema1 = v.pipe(v.unknown(), v.toBigint());
4+
const Schema2 = v.pipe(v.unknown(), v.toBigint());
5+
const Schema3 = v.pipe(v.unknown(), v.toBigint(), v.gtValue(1n));
6+
const Schema4 = v.pipe(v.unknown(), v.toBigint(), v.ltValue(2n));
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import * as v from "valibot";
22

3-
const Schema1 = v.pipe(v.unknown(), v.transform(Boolean), v.boolean());
4-
const Schema2 = v.pipe(v.unknown(), v.transform(Boolean), v.boolean());
3+
const Schema1 = v.pipe(v.unknown(), v.toBoolean());
4+
const Schema2 = v.pipe(v.unknown(), v.toBoolean());
Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import * as v from "valibot";
22

3-
const Schema1 = v.pipe(v.any(), v.transform(input => {
4-
try {
5-
return new Date(input);
6-
} catch {
7-
return input;
8-
}
9-
}), v.date());
10-
const Schema2 = v.pipe(v.any(), v.transform(input => {
11-
try {
12-
return new Date(input);
13-
} catch {
14-
return input;
15-
}
16-
}), v.date());
17-
const Schema3 = v.pipe(v.any(), v.transform(input => {
18-
try {
19-
return new Date(input);
20-
} catch {
21-
return input;
22-
}
23-
}), v.date(), v.minValue(new Date("1/10/23")));
24-
const Schema4 = v.pipe(v.any(), v.transform(input => {
25-
try {
26-
return new Date(input);
27-
} catch {
28-
return input;
29-
}
30-
}), v.date(), v.maxValue(new Date("2023-01-10")));
3+
const Schema1 = v.pipe(v.unknown(), v.toDate());
4+
const Schema2 = v.pipe(v.unknown(), v.toDate());
5+
const Schema3 = v.pipe(v.unknown(), v.toDate(), v.minValue(new Date("1/10/23")));
6+
const Schema4 = v.pipe(v.unknown(), v.toDate(), v.maxValue(new Date("2023-01-10")));
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as v from "valibot";
22

3-
const Schema1 = v.pipe(v.unknown(), v.transform(Number), v.number());
4-
const Schema2 = v.pipe(v.unknown(), v.transform(Number), v.number());
5-
const Schema3 = v.pipe(v.unknown(), v.transform(Number), v.number(), v.finite());
6-
const Schema4 = v.pipe(v.unknown(), v.transform(Number), v.number(), v.multipleOf(12));
3+
const Schema1 = v.pipe(v.unknown(), v.toNumber());
4+
const Schema2 = v.pipe(v.unknown(), v.toNumber());
5+
const Schema3 = v.pipe(v.unknown(), v.toNumber(), v.finite());
6+
const Schema4 = v.pipe(v.unknown(), v.toNumber(), v.multipleOf(12));
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as v from "valibot";
22

3-
const Schema1 = v.pipe(v.unknown(), v.transform(String), v.string());
4-
const Schema2 = v.pipe(v.unknown(), v.transform(String), v.string());
5-
const Schema3 = v.pipe(v.unknown(), v.transform(String), v.string(), v.email());
6-
const Schema4 = v.pipe(v.unknown(), v.transform(String), v.string(), v.url());
3+
const Schema1 = v.pipe(v.unknown(), v.toString());
4+
const Schema2 = v.pipe(v.unknown(), v.toString());
5+
const Schema3 = v.pipe(v.unknown(), v.toString(), v.email());
6+
const Schema4 = v.pipe(v.unknown(), v.toString(), v.url());

codemod/zod-to-valibot/__testfixtures__/schema-options/output.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const schema7Description = v.getDescription(Schema7);
1919
const Schema8 = v.pipe(v.literal("hello"), v.description('some description (literal "hello")'));
2020
const schema8Description = v.getDescription(Schema8);
2121
// coerce
22-
const Schema9 = v.pipe(v.unknown(), v.transform(String), v.string());
22+
const Schema9 = v.pipe(v.unknown(), v.toString());
2323
const Schema10 = v.string();
2424
// errorMap - supported by Valibot but incompatible
2525
const Schema11 = v.string();
@@ -42,23 +42,17 @@ const Schema18 = v.literal("bot", 'must be "bot" (msg)');
4242
const Schema19 = v.string("must be a string (msg)");
4343
const Schema20 = v.literal("bot", 'must be "bot" (msg)');
4444
// coerce + invalid_type_error
45-
const Schema21 = v.pipe(v.unknown(), v.transform(String), v.string("must be a string"));
45+
const Schema21 = v.pipe(v.unknown(), v.toString("must be a string"));
4646
const Schema22 = v.string("must be a string");
4747
// coerce + description
48-
const Schema23 = v.pipe(
49-
v.unknown(),
50-
v.transform(String),
51-
v.string(),
52-
v.description("some description")
53-
);
48+
const Schema23 = v.pipe(v.unknown(), v.toString(), v.description("some description"));
5449
const schema23Description = v.getDescription(Schema23);
5550
const Schema24 = v.pipe(v.string(), v.description("some description"));
5651
const schema24Description = v.getDescription(Schema24);
5752
// coerce + invalid_type_error + description
5853
const Schema25 = v.pipe(
5954
v.unknown(),
60-
v.transform(String),
61-
v.string("must be a string"),
55+
v.toString("must be a string"),
6256
v.description("some description")
6357
);
6458
const schema25Description = v.getDescription(Schema25);

codemod/zod-to-valibot/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
"@types/node": "^24.10.1",
3535
"tsdown": "^0.16.6",
3636
"typescript": "^5.9.3",
37+
"valibot": "workspace:*",
3738
"vite-plugin-node-polyfills": "^0.24.0",
38-
"vitest": "^4.0.13"
39+
"vitest": "^4.0.13",
40+
"zod": "^3.25.76"
3941
}
4042
}

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

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import j from 'jscodeshift';
22
import {
33
getDescription,
4+
getOptions,
45
getSchemaComps,
56
getSchemaWithOptionalDescription,
7+
getTransformedMsgs,
68
} from '../helpers';
79

810
export function transformBigint(
@@ -18,45 +20,28 @@ export function transformBigint(
1820
coerceSchema
1921
);
2022
if (coerce) {
23+
const optionsArg =
24+
args.length > 0 && args[args.length - 1]?.type === 'ObjectExpression'
25+
? args[args.length - 1]
26+
: null;
27+
const msgs = getTransformedMsgs(getOptions(optionsArg));
2128
return j.callExpression(
2229
j.memberExpression(j.identifier(valibotIdentifier), j.identifier('pipe')),
2330
[
2431
j.callExpression(
2532
j.memberExpression(
2633
j.identifier(valibotIdentifier),
27-
j.identifier('any')
34+
j.identifier('unknown')
2835
),
2936
[]
3037
),
3138
j.callExpression(
3239
j.memberExpression(
3340
j.identifier(valibotIdentifier),
34-
j.identifier('transform')
41+
j.identifier('toBigint')
3542
),
36-
[
37-
j.arrowFunctionExpression(
38-
[j.identifier('input')],
39-
j.blockStatement([
40-
j.tryStatement(
41-
j.blockStatement([
42-
j.returnStatement(
43-
j.callExpression(j.identifier('BigInt'), [
44-
j.identifier('input'),
45-
])
46-
),
47-
]),
48-
j.catchClause(
49-
null,
50-
null,
51-
j.blockStatement([j.returnStatement(j.identifier('input'))])
52-
)
53-
),
54-
]),
55-
false
56-
),
57-
]
43+
msgs.filter((m) => m !== null)
5844
),
59-
baseSchema,
6045
...(description
6146
? [getDescription(valibotIdentifier, description)]
6247
: []),

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import j from 'jscodeshift';
22
import {
33
getDescription,
4+
getOptions,
45
getSchemaComps,
56
getSchemaWithOptionalDescription,
7+
getTransformedMsgs,
68
} from '../helpers';
79

810
export function transformBoolean(
@@ -18,6 +20,11 @@ export function transformBoolean(
1820
coerceSchema
1921
);
2022
if (coerce) {
23+
const optionsArg =
24+
args.length > 0 && args[args.length - 1]?.type === 'ObjectExpression'
25+
? args[args.length - 1]
26+
: null;
27+
const msgs = getTransformedMsgs(getOptions(optionsArg));
2128
return j.callExpression(
2229
j.memberExpression(j.identifier(valibotIdentifier), j.identifier('pipe')),
2330
[
@@ -31,11 +38,10 @@ export function transformBoolean(
3138
j.callExpression(
3239
j.memberExpression(
3340
j.identifier(valibotIdentifier),
34-
j.identifier('transform')
41+
j.identifier('toBoolean')
3542
),
36-
[j.identifier('Boolean')]
43+
msgs.filter((m) => m !== null)
3744
),
38-
baseSchema,
3945
...(description
4046
? [getDescription(valibotIdentifier, description)]
4147
: []),

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

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import j from 'jscodeshift';
22
import {
33
getDescription,
4+
getOptions,
45
getSchemaComps,
56
getSchemaWithOptionalDescription,
7+
getTransformedMsgs,
68
} from '../helpers';
79

810
export function transformDate(
@@ -18,45 +20,28 @@ export function transformDate(
1820
coerceSchema
1921
);
2022
if (coerce) {
23+
const optionsArg =
24+
args.length > 0 && args[args.length - 1]?.type === 'ObjectExpression'
25+
? args[args.length - 1]
26+
: null;
27+
const msgs = getTransformedMsgs(getOptions(optionsArg));
2128
return j.callExpression(
2229
j.memberExpression(j.identifier(valibotIdentifier), j.identifier('pipe')),
2330
[
2431
j.callExpression(
2532
j.memberExpression(
2633
j.identifier(valibotIdentifier),
27-
j.identifier('any')
34+
j.identifier('unknown')
2835
),
2936
[]
3037
),
3138
j.callExpression(
3239
j.memberExpression(
3340
j.identifier(valibotIdentifier),
34-
j.identifier('transform')
41+
j.identifier('toDate')
3542
),
36-
[
37-
j.arrowFunctionExpression(
38-
[j.identifier('input')],
39-
j.blockStatement([
40-
j.tryStatement(
41-
j.blockStatement([
42-
j.returnStatement(
43-
j.newExpression(j.identifier('Date'), [
44-
j.identifier('input'),
45-
])
46-
),
47-
]),
48-
j.catchClause(
49-
null,
50-
null,
51-
j.blockStatement([j.returnStatement(j.identifier('input'))])
52-
)
53-
),
54-
]),
55-
false
56-
),
57-
]
43+
msgs.filter((m) => m !== null)
5844
),
59-
baseSchema,
6045
...(description
6146
? [getDescription(valibotIdentifier, description)]
6247
: []),

0 commit comments

Comments
 (0)