zod-v3-to-v4 1.6.1 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -272,11 +272,52 @@ function convertNameToTopLevelApiAndWrapInUnion(node, options) {
|
|
|
272
272
|
.filter((expression) => names.includes(expression.getName()))
|
|
273
273
|
.forEach((expression) => {
|
|
274
274
|
const parent = expression.getFirstAncestorByKind(SyntaxKind.CallExpression);
|
|
275
|
+
if (!parent) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
275
278
|
const text = expression.getExpression().getText();
|
|
279
|
+
// Check if the method call has arguments (e.g., .ip({ version: "v4" }))
|
|
280
|
+
const args = parent.getArguments();
|
|
281
|
+
if (args.length > 0) {
|
|
282
|
+
// Try to extract version from the arguments
|
|
283
|
+
const firstArg = args[0];
|
|
284
|
+
if (firstArg?.isKind(SyntaxKind.ObjectLiteralExpression)) {
|
|
285
|
+
const versionProperty = firstArg.getProperties().find((prop) => {
|
|
286
|
+
if (prop.isKind(SyntaxKind.PropertyAssignment)) {
|
|
287
|
+
const name = prop.getName();
|
|
288
|
+
return name === "version" || name === '"version"';
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
});
|
|
292
|
+
if (versionProperty?.isKind(SyntaxKind.PropertyAssignment)) {
|
|
293
|
+
const initializer = versionProperty.getInitializer();
|
|
294
|
+
if (initializer?.isKind(SyntaxKind.StringLiteral)) {
|
|
295
|
+
const version = initializer.getLiteralValue();
|
|
296
|
+
if (version === "v4" && nameToWrap === "ip") {
|
|
297
|
+
parent.replaceWithText(`${text}.ipv4()`);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
else if (version === "v6" && nameToWrap === "ip") {
|
|
301
|
+
parent.replaceWithText(`${text}.ipv6()`);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
else if (version === "v4" && nameToWrap === "cidr") {
|
|
305
|
+
parent.replaceWithText(`${text}.cidrv4()`);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
else if (version === "v6" && nameToWrap === "cidr") {
|
|
309
|
+
parent.replaceWithText(`${text}.cidrv6()`);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// Default behavior: create union
|
|
276
317
|
const unionText = renames
|
|
277
318
|
.map(({ name }) => `${text}.${name}()`)
|
|
278
319
|
.join(", ");
|
|
279
|
-
parent
|
|
320
|
+
parent.replaceWithText(`${zodName}.union([${unionText}])`);
|
|
280
321
|
});
|
|
281
322
|
convertNameToTopLevelApi(callExpression, {
|
|
282
323
|
zodName,
|
|
@@ -130,7 +130,7 @@ export function convertDeprecatedErrorKeysToErrorFunction(node) {
|
|
|
130
130
|
export function convertZodErrorToTreeifyError(sourceFile, zodName) {
|
|
131
131
|
sourceFile
|
|
132
132
|
.getDescendantsOfKind(SyntaxKind.CallExpression)
|
|
133
|
-
.filter((expression) => isZodReference(zodName, ["
|
|
133
|
+
.filter((expression) => isZodReference(zodName, ["ZodType", "ZodError"], expression))
|
|
134
134
|
.filter((expression) => {
|
|
135
135
|
const argsCount = expression.getArguments().length;
|
|
136
136
|
const methodCalled = expression.getExpression().getLastChild();
|
|
@@ -150,7 +150,7 @@ export function convertZodErrorToTreeifyError(sourceFile, zodName) {
|
|
|
150
150
|
});
|
|
151
151
|
sourceFile
|
|
152
152
|
.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression)
|
|
153
|
-
.filter((expression) => isZodReference(zodName, ["
|
|
153
|
+
.filter((expression) => isZodReference(zodName, ["ZodType", "ZodError"], expression))
|
|
154
154
|
.filter((expression) => {
|
|
155
155
|
const looksLikeZodErrorFormErrors = expression.getName() === "formErrors";
|
|
156
156
|
const looksLikeZodErrorFieldErrors = expression.getName() === "fieldErrors";
|
|
@@ -164,7 +164,7 @@ export function convertZodErrorToTreeifyError(sourceFile, zodName) {
|
|
|
164
164
|
export function convertZodErrorAddIssueToDirectPushes(sourceFile, zodName) {
|
|
165
165
|
sourceFile
|
|
166
166
|
.getDescendantsOfKind(SyntaxKind.CallExpression)
|
|
167
|
-
.filter((expression) => isZodReference(zodName, ["
|
|
167
|
+
.filter((expression) => isZodReference(zodName, ["ZodType", "ZodError"], expression))
|
|
168
168
|
.filter((expression) => {
|
|
169
169
|
const argsCount = expression.getArguments().length;
|
|
170
170
|
const methodCalled = expression.getExpression().getLastChild();
|
package/dist/migrate.js
CHANGED
|
@@ -23,7 +23,7 @@ export function migrateZodV3ToV4(sourceFile, options = {}) {
|
|
|
23
23
|
}
|
|
24
24
|
const parentType = node.getFirstAncestorByKind(SyntaxKind.QualifiedName);
|
|
25
25
|
if (parentType?.getText().endsWith("ZodSchema")) {
|
|
26
|
-
parentType?.getRight().replaceWithText("
|
|
26
|
+
parentType?.getRight().replaceWithText("ZodType");
|
|
27
27
|
}
|
|
28
28
|
const parentStatement = node.getParentWhile(isZodNode) || node;
|
|
29
29
|
if (!isZodNode(parentStatement)) {
|