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