@@ -3,15 +3,13 @@ import {
3
3
ZodArray ,
4
4
ZodBoolean ,
5
5
type ZodError ,
6
- ZodLiteral ,
7
6
ZodNullable ,
8
7
ZodNumber ,
9
8
ZodObject ,
10
9
ZodOptional ,
11
10
type ZodRawShape ,
12
11
ZodString ,
13
12
type ZodType ,
14
- ZodUnion ,
15
13
z ,
16
14
} from "zod" ;
17
15
@@ -23,8 +21,10 @@ export type FieldErrors<
23
21
export type InputProp = {
24
22
"aria-required" : boolean ;
25
23
name : string ;
26
- type : "text" | "number" | "checkbox" | "email" ;
27
- } ;
24
+ } & (
25
+ | { type : "text" | "checkbox" | "email" }
26
+ | { type : "number" ; min ?: number ; max ?: number }
27
+ ) ;
28
28
29
29
export const formNameInputProps = {
30
30
type : "hidden" ,
@@ -207,11 +207,15 @@ function getInputProp<T extends ZodType>(
207
207
fieldValidator : T ,
208
208
name : string | number | symbol ,
209
209
) {
210
+ const inputInfo = getInputInfo < T > ( fieldValidator ) ;
211
+
210
212
const inputProp : InputProp = {
211
213
name : String ( name ) ,
212
214
"aria-required" :
213
215
! fieldValidator . isOptional ( ) && ! fieldValidator . isNullable ( ) ,
214
- type : getInputInfo < T > ( fieldValidator ) . type ,
216
+ type : inputInfo . type ,
217
+ min : inputInfo . min ,
218
+ max : inputInfo . max ,
215
219
} ;
216
220
217
221
return inputProp ;
@@ -240,6 +244,8 @@ function getInputInfo<T extends ZodType>(fieldValidator: T): {
240
244
type : InputProp [ "type" ] ;
241
245
isArray : boolean ;
242
246
isOptional : boolean ;
247
+ min ?: number ;
248
+ max ?: number ;
243
249
} {
244
250
let resolvedType = fieldValidator ;
245
251
let isArray = false ;
@@ -261,7 +267,20 @@ function getInputInfo<T extends ZodType>(fieldValidator: T): {
261
267
262
268
const type = getInputType ( resolvedType ) ;
263
269
264
- return { type, isArray, isOptional } ;
270
+ const result : ReturnType < typeof getInputInfo > = { type, isArray, isOptional } ;
271
+
272
+ if ( type === "number" && resolvedType instanceof ZodNumber ) {
273
+ for ( const check of resolvedType . _def . checks ) {
274
+ if ( check . kind === "min" ) {
275
+ result . min = check . value + ( check . inclusive ? 0 : 1 ) ;
276
+ }
277
+ if ( check . kind === "max" ) {
278
+ result . max = check . value - ( check . inclusive ? 0 : 1 ) ;
279
+ }
280
+ }
281
+ }
282
+
283
+ return result ;
265
284
}
266
285
267
286
export async function validateForm < T extends ZodRawShape > ( {
0 commit comments