Skip to content

Commit 4363c2f

Browse files
Merge pull request #1262 from paoloricciuti/fix-strict-migration
fix: properly migrate `strict` and `passthrough`
2 parents 21e203c + 2e8f9bf commit 4363c2f

File tree

14 files changed

+865
-169
lines changed

14 files changed

+865
-169
lines changed

codemod/zod-to-valibot/__testfixtures__/object-extend/input.ts

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,94 @@ const Schema21 = Schema20.extend({bar: z.string()});
3333
const Schema22 = z.object({foo: z.string()}).extend({bar: z.number()}).catchall(z.null());
3434
const Schema23 = Schema2.extend({bar: z.string()}).catchall(z.null());
3535

36+
// i don't know which crazy person would do this, but it is valid
37+
// and we support it so we might as well test it
38+
const Schema24 = Schema23.extend(...[Schema22.shape]);
39+
3640
// ------------ Expected transform ------------
3741
// // plain
38-
// const Schema1 = v.object({...{foo: v.string()}, ...{bar: v.number()}});
42+
// const Schema1 = v.object({
43+
// foo: v.string(),
44+
// bar: v.number()
45+
// });
3946
// const Schema2 = v.object({foo: v.number()});
40-
// const Schema3 = v.object({...Schema2.entries, ...{bar: v.string()}});
47+
// const Schema3 = v.object({
48+
// ...Schema2.entries,
49+
// bar: v.string()
50+
// });
4151

4252
// // passthrough
43-
// const Schema4 = v.looseObject({...{foo: v.string()}, ...{bar: v.number()}});
53+
// const Schema4 = v.object({
54+
// ...v.looseObject({foo: v.string()}).entries,
55+
// bar: v.number()
56+
// });
4457
// const Schema5 = v.looseObject({foo: v.number()});
45-
// const Schema6 = v.looseObject({...Schema5.entries, ...{bar: v.string()}});
46-
// const Schema7 = v.looseObject({...{foo: v.string()}, ...{bar: v.number()}});
47-
// const Schema8 = v.looseObject({...Schema2.entries, ...{bar: v.string()}});
58+
// const Schema6 = v.object({
59+
// ...Schema5.entries,
60+
// bar: v.string()
61+
// });
62+
// const Schema7 = v.looseObject({
63+
// ...v.looseObject({foo: v.string()}).entries,
64+
// bar: v.number()
65+
// });
66+
// const Schema8 = v.looseObject({
67+
// ...Schema2.entries,
68+
// bar: v.string()
69+
// });
4870

4971
// // strict
50-
// const Schema9 = v.strictObject({...{foo: v.string()}, ...{bar: v.number()}});
72+
// const Schema9 = v.object({
73+
// ...v.strictObject({foo: v.string()}).entries,
74+
// bar: v.number()
75+
// });
5176
// const Schema10 = v.strictObject({foo: v.number()});
52-
// const Schema11 = v.strictObject({...Schema10.entries, ...{bar: v.string()}});
53-
// const Schema12 = v.strictObject({...{foo: v.string()}, ...{bar: v.number()}});
54-
// const Schema13 = v.strictObject({...Schema2.entries, ...{bar: v.string()}});
77+
// const Schema11 = v.object({
78+
// ...Schema10.entries,
79+
// bar: v.string()
80+
// });
81+
// const Schema12 = v.strictObject({
82+
// ...v.strictObject({foo: v.string()}).entries,
83+
// bar: v.number()
84+
// });
85+
// const Schema13 = v.strictObject({
86+
// ...Schema2.entries,
87+
// bar: v.string()
88+
// });
5589

5690
// // strip
57-
// const Schema14 = v.object({...{foo: v.string()}, ...{bar: v.number()}});
91+
// const Schema14 = v.object({
92+
// foo: v.string(),
93+
// bar: v.number()
94+
// });
5895
// const Schema15 = v.object({foo: v.number()});
59-
// const Schema16 = v.object({...Schema15.entries, ...{bar: v.string()}});
60-
// const Schema17 = v.object({...{foo: v.string()}, ...{bar: v.number()}});
61-
// const Schema18 = v.object({...Schema2.entries, ...{bar: v.string()}});
96+
// const Schema16 = v.object({
97+
// ...Schema15.entries,
98+
// bar: v.string()
99+
// });
100+
// const Schema17 = v.object({
101+
// foo: v.string(),
102+
// bar: v.number()
103+
// });
104+
// const Schema18 = v.object({
105+
// ...Schema2.entries,
106+
// bar: v.string()
107+
// });
62108

63109
// // catchall
64-
// const Schema19 = v.objectWithRest({...{foo: v.string()}, ...{bar: v.number()}}, v.null());
110+
// const Schema19 = v.object({
111+
// ...v.objectWithRest({foo: v.string()}, v.null()).entries,
112+
// bar: v.number()
113+
// });
65114
// const Schema20 = v.objectWithRest({foo: v.number()}, v.null());
66-
// const Schema21 = v.objectWithRest({...Schema20.entries, ...{bar: v.string()}}, Schema20.rest);
67-
// const Schema22 = v.objectWithRest({...{foo: v.string()}, ...{bar: v.number()}}, v.null());
68-
// const Schema23 = v.objectWithRest({...Schema2.entries, ...{bar: v.string()}}, v.null());
115+
// const Schema21 = v.object({
116+
// ...Schema20.entries,
117+
// bar: v.string()
118+
// });
119+
// const Schema22 = v.objectWithRest({
120+
// foo: v.string(),
121+
// bar: v.number()
122+
// }, v.null());
123+
// const Schema23 = v.objectWithRest({
124+
// ...Schema2.entries,
125+
// bar: v.string()
126+
// }, v.null());
Lines changed: 171 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,203 @@
11
import * as v from "valibot";
22

33
// plain
4-
const Schema1 = v.extend(v.object({foo: v.string()}), {bar: v.number()});
4+
const Schema1 = v.object({
5+
foo: v.string(),
6+
bar: v.number()
7+
});
58
const Schema2 = v.object({foo: v.number()});
6-
const Schema3 = v.extend(Schema2, {bar: v.string()});
9+
const Schema3 = v.object({
10+
.../*@valibot-migrate we can't detect if Schema2 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
11+
Schema2.entries,
12+
13+
bar: v.string()
14+
});
715

816
// passthrough
9-
const Schema4 = v.extend(v.looseObject({foo: v.string()}), {bar: v.number()});
17+
const Schema4 = v.object({
18+
...v.looseObject({foo: v.string()}).entries,
19+
bar: v.number()
20+
});
1021
const Schema5 = v.looseObject({foo: v.number()});
11-
const Schema6 = v.extend(Schema5, {bar: v.string()});
12-
const Schema7 = v.pipe(v.extend(v.object({foo: v.string()}), {bar: v.number()}), v.passthrough());
13-
const Schema8 = v.pipe(v.extend(Schema2, {bar: v.string()}), v.passthrough());
22+
const Schema6 = v.object({
23+
.../*@valibot-migrate we can't detect if Schema5 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
24+
Schema5.entries,
25+
26+
bar: v.string()
27+
});
28+
const Schema7 = v.looseObject({
29+
...v.looseObject({foo: v.string()}).entries,
30+
bar: v.number()
31+
});
32+
const Schema8 = v.looseObject({
33+
.../*@valibot-migrate we can't detect if Schema2 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
34+
Schema2.entries,
35+
36+
bar: v.string()
37+
});
1438

1539
// strict
16-
const Schema9 = v.extend(v.strictObject({foo: v.string()}), {bar: v.number()});
40+
const Schema9 = v.object({
41+
...v.strictObject({foo: v.string()}).entries,
42+
bar: v.number()
43+
});
1744
const Schema10 = v.strictObject({foo: v.number()});
18-
const Schema11 = v.extend(Schema10, {bar: v.string()});
19-
const Schema12 = v.pipe(v.extend(v.object({foo: v.string()}), {bar: v.number()}), v.strict());
20-
const Schema13 = v.pipe(v.extend(Schema2, {bar: v.string()}), v.strict());
45+
const Schema11 = v.object({
46+
.../*@valibot-migrate we can't detect if Schema10 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
47+
Schema10.entries,
48+
49+
bar: v.string()
50+
});
51+
const Schema12 = v.strictObject({
52+
...v.strictObject({foo: v.string()}).entries,
53+
bar: v.number()
54+
});
55+
const Schema13 = v.strictObject({
56+
.../*@valibot-migrate we can't detect if Schema2 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
57+
Schema2.entries,
58+
59+
bar: v.string()
60+
});
2161

2262
// strip
23-
const Schema14 = v.extend(v.object({foo: v.string()}), {bar: v.number()});
63+
const Schema14 = v.object({
64+
foo: v.string(),
65+
bar: v.number()
66+
});
2467
const Schema15 = v.object({foo: v.number()});
25-
const Schema16 = v.extend(Schema15, {bar: v.string()});
26-
const Schema17 = v.pipe(
27-
v.extend(v.object({foo: v.string()}), {bar: v.number()}),
28-
v.strict(),
29-
v.strip()
30-
);
31-
const Schema18 = v.pipe(v.extend(Schema2, {bar: v.string()}), v.strict(), v.strip());
68+
const Schema16 = v.object({
69+
.../*@valibot-migrate we can't detect if Schema15 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
70+
Schema15.entries,
71+
72+
bar: v.string()
73+
});
74+
const Schema17 = v.object({
75+
foo: v.string(),
76+
bar: v.number()
77+
});
78+
const Schema18 = v.object({
79+
.../*@valibot-migrate we can't detect if Schema2 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
80+
Schema2.entries,
81+
82+
bar: v.string()
83+
});
3284

3385
// catchall
34-
const Schema19 = v.extend(v.objectWithRest({foo: v.string()}, v.null()), {bar: v.number()});
86+
const Schema19 = v.object({
87+
...v.objectWithRest({foo: v.string()}, v.null()).entries,
88+
bar: v.number()
89+
});
3590
const Schema20 = v.objectWithRest({foo: v.number()}, v.null());
36-
const Schema21 = v.extend(Schema20, {bar: v.string()});
37-
const Schema22 = v.pipe(
38-
v.extend(v.object({foo: v.string()}), {bar: v.number()}),
39-
v.catchall(v.null())
40-
);
41-
const Schema23 = v.pipe(v.extend(Schema2, {bar: v.string()}), v.catchall(v.null()));
91+
const Schema21 = v.object({
92+
.../*@valibot-migrate we can't detect if Schema20 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
93+
Schema20.entries,
94+
95+
bar: v.string()
96+
});
97+
const Schema22 = v.objectWithRest({
98+
foo: v.string(),
99+
bar: v.number()
100+
}, v.null());
101+
const Schema23 = v.objectWithRest({
102+
.../*@valibot-migrate we can't detect if Schema2 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
103+
Schema2.entries,
104+
105+
bar: v.string()
106+
}, v.null());
107+
108+
// i don't know which crazy person would do this, but it is valid
109+
// and we support it so we might as well test it
110+
const Schema24 = v.object({
111+
.../*@valibot-migrate we can't detect if Schema23 has a `pipe` operator, if it does you might need to migrate this by hand otherwise it will loose it's pipeline*/
112+
Schema23.entries,
113+
114+
...[Schema22.entries]
115+
});
42116

43117
// ------------ Expected transform ------------
44118
// // plain
45-
// const Schema1 = v.object({...{foo: v.string()}, ...{bar: v.number()}});
119+
// const Schema1 = v.object({
120+
// foo: v.string(),
121+
// bar: v.number()
122+
// });
46123
// const Schema2 = v.object({foo: v.number()});
47-
// const Schema3 = v.object({...Schema2.entries, ...{bar: v.string()}});
124+
// const Schema3 = v.object({
125+
// ...Schema2.entries,
126+
// bar: v.string()
127+
// });
48128

49129
// // passthrough
50-
// const Schema4 = v.looseObject({...{foo: v.string()}, ...{bar: v.number()}});
130+
// const Schema4 = v.object({
131+
// ...v.looseObject({foo: v.string()}).entries,
132+
// bar: v.number()
133+
// });
51134
// const Schema5 = v.looseObject({foo: v.number()});
52-
// const Schema6 = v.looseObject({...Schema5.entries, ...{bar: v.string()}});
53-
// const Schema7 = v.looseObject({...{foo: v.string()}, ...{bar: v.number()}});
54-
// const Schema8 = v.looseObject({...Schema2.entries, ...{bar: v.string()}});
135+
// const Schema6 = v.object({
136+
// ...Schema5.entries,
137+
// bar: v.string()
138+
// });
139+
// const Schema7 = v.looseObject({
140+
// ...v.looseObject({foo: v.string()}).entries,
141+
// bar: v.number()
142+
// });
143+
// const Schema8 = v.looseObject({
144+
// ...Schema2.entries,
145+
// bar: v.string()
146+
// });
55147

56148
// // strict
57-
// const Schema9 = v.strictObject({...{foo: v.string()}, ...{bar: v.number()}});
149+
// const Schema9 = v.object({
150+
// ...v.strictObject({foo: v.string()}).entries,
151+
// bar: v.number()
152+
// });
58153
// const Schema10 = v.strictObject({foo: v.number()});
59-
// const Schema11 = v.strictObject({...Schema10.entries, ...{bar: v.string()}});
60-
// const Schema12 = v.strictObject({...{foo: v.string()}, ...{bar: v.number()}});
61-
// const Schema13 = v.strictObject({...Schema2.entries, ...{bar: v.string()}});
154+
// const Schema11 = v.object({
155+
// ...Schema10.entries,
156+
// bar: v.string()
157+
// });
158+
// const Schema12 = v.strictObject({
159+
// ...v.strictObject({foo: v.string()}).entries,
160+
// bar: v.number()
161+
// });
162+
// const Schema13 = v.strictObject({
163+
// ...Schema2.entries,
164+
// bar: v.string()
165+
// });
62166

63167
// // strip
64-
// const Schema14 = v.object({...{foo: v.string()}, ...{bar: v.number()}});
168+
// const Schema14 = v.object({
169+
// foo: v.string(),
170+
// bar: v.number()
171+
// });
65172
// const Schema15 = v.object({foo: v.number()});
66-
// const Schema16 = v.object({...Schema15.entries, ...{bar: v.string()}});
67-
// const Schema17 = v.object({...{foo: v.string()}, ...{bar: v.number()}});
68-
// const Schema18 = v.object({...Schema2.entries, ...{bar: v.string()}});
173+
// const Schema16 = v.object({
174+
// ...Schema15.entries,
175+
// bar: v.string()
176+
// });
177+
// const Schema17 = v.object({
178+
// foo: v.string(),
179+
// bar: v.number()
180+
// });
181+
// const Schema18 = v.object({
182+
// ...Schema2.entries,
183+
// bar: v.string()
184+
// });
69185

70186
// // catchall
71-
// const Schema19 = v.objectWithRest({...{foo: v.string()}, ...{bar: v.number()}}, v.null());
187+
// const Schema19 = v.object({
188+
// ...v.objectWithRest({foo: v.string()}, v.null()).entries,
189+
// bar: v.number()
190+
// });
72191
// const Schema20 = v.objectWithRest({foo: v.number()}, v.null());
73-
// const Schema21 = v.objectWithRest({...Schema20.entries, ...{bar: v.string()}}, Schema20.rest);
74-
// const Schema22 = v.objectWithRest({...{foo: v.string()}, ...{bar: v.number()}}, v.null());
75-
// const Schema23 = v.objectWithRest({...Schema2.entries, ...{bar: v.string()}}, v.null());
192+
// const Schema21 = v.object({
193+
// ...Schema20.entries,
194+
// bar: v.string()
195+
// });
196+
// const Schema22 = v.objectWithRest({
197+
// foo: v.string(),
198+
// bar: v.number()
199+
// }, v.null());
200+
// const Schema23 = v.objectWithRest({
201+
// ...Schema2.entries,
202+
// bar: v.string()
203+
// }, v.null());

0 commit comments

Comments
 (0)