1
- import type { AnyZodObject , ZodDefault , ZodEffects , ZodType , ZodTypeDef , ZodUnion } from 'zod' ;
1
+ import type {
2
+ AnyZodObject ,
3
+ ZodDefault ,
4
+ ZodEffects ,
5
+ ZodErrorMap ,
6
+ ZodType ,
7
+ ZodTypeDef ,
8
+ ZodUnion
9
+ } from 'zod' ;
2
10
import type { JSONSchema7 } from 'json-schema' ;
3
11
import {
4
12
type AdapterOptions ,
@@ -54,9 +62,10 @@ export type ZodValidation<T extends ZodObjectTypes> =
54
62
55
63
async function validate < T extends ZodValidation < ZodObjectTypes > > (
56
64
schema : T ,
57
- data : unknown
65
+ data : unknown ,
66
+ errorMap : ZodErrorMap | undefined
58
67
) : Promise < ValidationResult < Infer < T > > > {
59
- const result = await schema . safeParseAsync ( data ) ;
68
+ const result = await schema . safeParseAsync ( data , { errorMap } ) ;
60
69
if ( result . success ) {
61
70
return {
62
71
data : result . data as Infer < T > ,
@@ -71,22 +80,23 @@ async function validate<T extends ZodValidation<ZodObjectTypes>>(
71
80
72
81
function _zod < T extends ZodValidation < ZodObjectTypes > > (
73
82
schema : T ,
74
- options ?: AdapterOptions < Infer < T > > & { config ?: Partial < Options > }
83
+ options ?: AdapterOptions < Infer < T > > & { errorMap ?: ZodErrorMap ; config ?: Partial < Options > }
75
84
) : ValidationAdapter < Infer < T > , InferIn < T > > {
76
85
return createAdapter ( {
77
86
superFormValidationLibrary : 'zod' ,
78
- validate : async ( data ) => validate ( schema , data ) ,
87
+ validate : async ( data ) => validate ( schema , data , options ?. errorMap ) ,
79
88
jsonSchema : options ?. jsonSchema ?? zodToJSONSchema ( schema , options ?. config ) ,
80
89
defaults : options ?. defaults
81
90
} ) ;
82
91
}
83
92
84
93
function _zodClient < T extends ZodValidation < ZodObjectTypes > > (
85
- schema : T
94
+ schema : T ,
95
+ options ?: { errorMap ?: ZodErrorMap }
86
96
) : ClientValidationAdapter < Infer < T > , InferIn < T > > {
87
97
return {
88
98
superFormValidationLibrary : 'zod' ,
89
- validate : async ( data ) => validate ( schema , data )
99
+ validate : async ( data ) => validate ( schema , data , options ?. errorMap )
90
100
} ;
91
101
}
92
102
0 commit comments