@@ -9,10 +9,12 @@ import { z as Z } from 'zod';
9
9
* accidentally matching a less-ideal schema candidate.
10
10
*
11
11
* 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.
12
14
*/
13
- export function smartUnion ( z : typeof Z , candidates : Z . ZodSchema [ ] ) {
15
+ export function smartUnion ( z : any , candidates : any [ ] ) : any {
14
16
// strip `z.lazy`
15
- const processedCandidates = candidates . map ( ( candidate ) => unwrapLazy ( z , candidate ) ) ;
17
+ const processedCandidates : Z . ZodSchema [ ] = candidates . map ( ( candidate ) => unwrapLazy ( z , candidate ) ) ;
16
18
17
19
if ( processedCandidates . some ( ( c ) => ! ( c instanceof z . ZodObject || c instanceof z . ZodArray ) ) ) {
18
20
// 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[]) {
22
24
let resultData : any ;
23
25
24
26
return z
25
- . custom ( ( data ) => {
27
+ . custom ( ( data : any ) => {
26
28
if ( Array . isArray ( data ) ) {
27
29
const { data : result , success } = smartArrayUnion (
28
30
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
+ > ,
30
34
data
31
35
) ;
32
36
if ( success ) {
@@ -36,7 +40,7 @@ export function smartUnion(z: typeof Z, candidates: Z.ZodSchema[]) {
36
40
} else {
37
41
const { data : result , success } = smartObjectUnion (
38
42
z ,
39
- processedCandidates . filter ( ( c ) => c instanceof z . ZodObject ) ,
43
+ processedCandidates . filter ( ( c ) => c instanceof z . ZodObject ) as Z . ZodObject < Z . ZodRawShape > [ ] ,
40
44
data
41
45
) ;
42
46
if ( success ) {
0 commit comments