1
- import type {
2
- AnyZodObject ,
3
- ZodDefault ,
4
- ZodEffects ,
5
- ZodErrorMap ,
6
- ZodType ,
7
- ZodTypeDef ,
8
- ZodUnion
9
- } from 'zod' ;
1
+ import { type ZodErrorMap , type ZodType , type ZodTypeDef } from 'zod' ;
10
2
import type { JSONSchema7 } from 'json-schema' ;
11
3
import {
12
4
type AdapterOptions ,
@@ -32,68 +24,55 @@ export const zodToJSONSchema = (...params: Parameters<typeof zodToJson>) => {
32
24
return zodToJson ( ...params ) as JSONSchema7 ;
33
25
} ;
34
26
35
- type ZodObjectUnion < T extends AnyZodObject > = ZodUnion <
36
- [ ZodValidation < T > , ZodValidation < T > , ...ZodValidation < T > [ ] ]
27
+ // allows for any object schema
28
+ // allows `undefined` in the input type to account for ZodDefault
29
+ export type ZodObjectType = ZodType <
30
+ Record < string , unknown > ,
31
+ ZodTypeDef ,
32
+ Record < string , unknown > | undefined
37
33
> ;
34
+ export type ZodObjectTypes = ZodObjectType ;
38
35
39
- export type ZodObjectType = ZodType < Record < string , unknown > , ZodTypeDef , Record < string , unknown > > ;
40
-
41
- export type ZodObjectTypes = AnyZodObject | ZodObjectUnion < AnyZodObject > | ZodObjectType ;
36
+ // left in for compatibility reasons
37
+ export type ZodValidation < T extends ZodObjectTypes = ZodObjectTypes > = T ;
42
38
43
- export type ZodValidation < T extends ZodObjectTypes > =
44
- | T
45
- | ZodEffects < T >
46
- | ZodEffects < ZodEffects < T > >
47
- | ZodEffects < ZodEffects < ZodEffects < T > > >
48
- | ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > >
49
- | ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > > >
50
- | ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > > > >
51
- | ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > > > > >
52
- | ZodDefault < T >
53
- | ZodDefault < ZodEffects < T > >
54
- | ZodDefault < ZodEffects < ZodEffects < T > > >
55
- | ZodDefault < ZodEffects < ZodEffects < ZodEffects < T > > > >
56
- | ZodDefault < ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > > >
57
- | ZodDefault < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > > > >
58
- | ZodDefault < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > > > > >
59
- | ZodDefault <
60
- ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < ZodEffects < T > > > > > > >
61
- > ;
62
-
63
- async function validate < T extends ZodValidation < ZodObjectTypes > > (
39
+ async function validate < T extends ZodValidation > (
64
40
schema : T ,
65
41
data : unknown ,
66
42
errorMap : ZodErrorMap | undefined
67
- ) : Promise < ValidationResult < Infer < T > > > {
43
+ ) : Promise < ValidationResult < Infer < T , 'zod' > > > {
68
44
const result = await schema . safeParseAsync ( data , { errorMap } ) ;
69
45
if ( result . success ) {
70
46
return {
71
- data : result . data as Infer < T > ,
47
+ data : result . data as Infer < T , 'zod' > ,
72
48
success : true
73
49
} ;
74
50
}
51
+
75
52
return {
76
53
issues : result . error . issues . map ( ( { message, path } ) => ( { message, path } ) ) ,
77
54
success : false
78
55
} ;
79
56
}
80
57
81
- function _zod < T extends ZodValidation < ZodObjectTypes > > (
58
+ function _zod < T extends ZodValidation > (
82
59
schema : T ,
83
- options ?: AdapterOptions < Infer < T > > & { errorMap ?: ZodErrorMap ; config ?: Partial < Options > }
84
- ) : ValidationAdapter < Infer < T > , InferIn < T > > {
60
+ options ?: AdapterOptions < Infer < T , 'zod' > > & { errorMap ?: ZodErrorMap ; config ?: Partial < Options > }
61
+ ) : ValidationAdapter < Infer < T , 'zod' > , InferIn < T , 'zod' > > {
85
62
return createAdapter ( {
86
63
superFormValidationLibrary : 'zod' ,
87
- validate : async ( data ) => validate ( schema , data , options ?. errorMap ) ,
64
+ validate : async ( data ) => {
65
+ return validate ( schema , data , options ?. errorMap ) ;
66
+ } ,
88
67
jsonSchema : options ?. jsonSchema ?? zodToJSONSchema ( schema , options ?. config ) ,
89
68
defaults : options ?. defaults
90
69
} ) ;
91
70
}
92
71
93
- function _zodClient < T extends ZodValidation < ZodObjectTypes > > (
72
+ function _zodClient < T extends ZodValidation > (
94
73
schema : T ,
95
74
options ?: { errorMap ?: ZodErrorMap }
96
- ) : ClientValidationAdapter < Infer < T > , InferIn < T > > {
75
+ ) : ClientValidationAdapter < Infer < T , 'zod' > , InferIn < T , 'zod' > > {
97
76
return {
98
77
superFormValidationLibrary : 'zod' ,
99
78
validate : async ( data ) => validate ( schema , data , options ?. errorMap )
0 commit comments