Skip to content

Commit f521ea1

Browse files
authored
Fix required tag interpretation for map fields (#15)
By default map fields are considered nullable. With 'required', they are not nullable but can be empty. But the current implementation enforced 'required' maps to be non-empty.
1 parent 3da7737 commit f521ea1

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

zod.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ forParts:
576576
} else {
577577
if valValue != "" {
578578
switch valName {
579-
case "required":
580579
case "min":
581580
validateStr.WriteString(fmt.Sprintf(".min(%s)", valValue))
582581
case "max":
@@ -713,7 +712,6 @@ forParts:
713712
switch valName {
714713
case "omitempty":
715714
case "required":
716-
refines = append(refines, ".refine((val) => Object.keys(val).length > 0, 'Empty map')")
717715
case "dive":
718716
break forParts
719717

zod_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ func TestMapWithValidations(t *testing.T) {
13301330
}
13311331
assert.Equal(t,
13321332
`export const RequiredSchema = z.object({
1333-
Map: z.record(z.string(), z.string()).refine((val) => Object.keys(val).length > 0, 'Empty map'),
1333+
Map: z.record(z.string(), z.string()),
13341334
})
13351335
export type Required = z.infer<typeof RequiredSchema>
13361336
@@ -1720,7 +1720,7 @@ export const UserSchema = z.object({
17201720
PostOptional: PostSchema.optional(),
17211721
PostOptionalNullable: PostSchema.optional().nullable(),
17221722
Metadata: z.record(z.string(), z.string()).nullable(),
1723-
MetadataLength: z.record(z.string(), z.string()).refine((val) => Object.keys(val).length > 0, 'Empty map').refine((val) => Object.keys(val).length >= 1, 'Map too small').refine((val) => Object.keys(val).length <= 10, 'Map too large'),
1723+
MetadataLength: z.record(z.string(), z.string()).refine((val) => Object.keys(val).length >= 1, 'Map too small').refine((val) => Object.keys(val).length <= 10, 'Map too large'),
17241724
MetadataOptional: z.record(z.string(), z.string()).optional(),
17251725
MetadataOptionalNullable: z.record(z.string(), z.string()).optional().nullable(),
17261726
ExtendedProps: z.any(),

0 commit comments

Comments
 (0)