Skip to content

Commit 9a24d17

Browse files
committed
fix zod version issue
1 parent 871e9b7 commit 9a24d17

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

packages/runtime/src/zod-utils.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import { z as Z } from 'zod';
99
* accidentally matching a less-ideal schema candidate.
1010
*
1111
* The helper uses a custom schema to find the candidate that results in the fewest unrecognized keys when parsing the data.
12+
*
13+
* The function uses `any` for parameter and return type to be compatible with various zod versions.
1214
*/
13-
export function smartUnion(z: typeof Z, candidates: Z.ZodSchema[]) {
15+
export function smartUnion(z: any, candidates: any[]): any {
1416
// strip `z.lazy`
15-
const processedCandidates = candidates.map((candidate) => unwrapLazy(z, candidate));
17+
const processedCandidates: Z.ZodSchema[] = candidates.map((candidate) => unwrapLazy(z, candidate));
1618

1719
if (processedCandidates.some((c) => !(c instanceof z.ZodObject || c instanceof z.ZodArray))) {
1820
// fall back to plain union if not all candidates are objects or arrays
@@ -22,11 +24,13 @@ export function smartUnion(z: typeof Z, candidates: Z.ZodSchema[]) {
2224
let resultData: any;
2325

2426
return z
25-
.custom((data) => {
27+
.custom((data: any) => {
2628
if (Array.isArray(data)) {
2729
const { data: result, success } = smartArrayUnion(
2830
z,
29-
processedCandidates.filter((c) => c instanceof z.ZodArray),
31+
processedCandidates.filter((c) => c instanceof z.ZodArray) as Array<
32+
Z.ZodArray<Z.ZodObject<Z.ZodRawShape>>
33+
>,
3034
data
3135
);
3236
if (success) {
@@ -36,7 +40,7 @@ export function smartUnion(z: typeof Z, candidates: Z.ZodSchema[]) {
3640
} else {
3741
const { data: result, success } = smartObjectUnion(
3842
z,
39-
processedCandidates.filter((c) => c instanceof z.ZodObject),
43+
processedCandidates.filter((c) => c instanceof z.ZodObject) as Z.ZodObject<Z.ZodRawShape>[],
4044
data
4145
);
4246
if (success) {

0 commit comments

Comments
 (0)