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' ;
102import type { JSONSchema7 } from 'json-schema' ;
113import {
124 type AdapterOptions ,
@@ -32,68 +24,55 @@ export const zodToJSONSchema = (...params: Parameters<typeof zodToJson>) => {
3224 return zodToJson ( ...params ) as JSONSchema7 ;
3325} ;
3426
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
3733> ;
34+ export type ZodObjectTypes = ZodObjectType ;
3835
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 ;
4238
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 > (
6440 schema : T ,
6541 data : unknown ,
6642 errorMap : ZodErrorMap | undefined
67- ) : Promise < ValidationResult < Infer < T > > > {
43+ ) : Promise < ValidationResult < Infer < T , 'zod' > > > {
6844 const result = await schema . safeParseAsync ( data , { errorMap } ) ;
6945 if ( result . success ) {
7046 return {
71- data : result . data as Infer < T > ,
47+ data : result . data as Infer < T , 'zod' > ,
7248 success : true
7349 } ;
7450 }
51+
7552 return {
7653 issues : result . error . issues . map ( ( { message, path } ) => ( { message, path } ) ) ,
7754 success : false
7855 } ;
7956}
8057
81- function _zod < T extends ZodValidation < ZodObjectTypes > > (
58+ function _zod < T extends ZodValidation > (
8259 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' > > {
8562 return createAdapter ( {
8663 superFormValidationLibrary : 'zod' ,
87- validate : async ( data ) => validate ( schema , data , options ?. errorMap ) ,
64+ validate : async ( data ) => {
65+ return validate ( schema , data , options ?. errorMap ) ;
66+ } ,
8867 jsonSchema : options ?. jsonSchema ?? zodToJSONSchema ( schema , options ?. config ) ,
8968 defaults : options ?. defaults
9069 } ) ;
9170}
9271
93- function _zodClient < T extends ZodValidation < ZodObjectTypes > > (
72+ function _zodClient < T extends ZodValidation > (
9473 schema : T ,
9574 options ?: { errorMap ?: ZodErrorMap }
96- ) : ClientValidationAdapter < Infer < T > , InferIn < T > > {
75+ ) : ClientValidationAdapter < Infer < T , 'zod' > , InferIn < T , 'zod' > > {
9776 return {
9877 superFormValidationLibrary : 'zod' ,
9978 validate : async ( data ) => validate ( schema , data , options ?. errorMap )
0 commit comments