File tree Expand file tree Collapse file tree 2 files changed +46
-5
lines changed Expand file tree Collapse file tree 2 files changed +46
-5
lines changed Original file line number Diff line number Diff line change @@ -57,13 +57,21 @@ export function schemaInfo(
57
57
) as JSONSchema7 [ ] )
58
58
: undefined ;
59
59
60
- const properties =
61
- schema . properties && types . includes ( 'object' )
62
- ? ( Object . fromEntries (
63
- Object . entries ( schema . properties ) . filter ( ( [ , value ] ) => typeof value !== 'boolean' )
64
- ) as { [ key : string ] : JSONSchema7 } )
60
+ const additionalProperties =
61
+ types . includes ( 'object' ) && typeof schema . additionalProperties == 'object'
62
+ ? schemaInfo ( schema . additionalProperties , isOptional , path )
65
63
: undefined ;
66
64
65
+ const mergedProperties = types . includes ( 'object' )
66
+ ? { ...additionalProperties ?. properties , ...schema . properties }
67
+ : undefined ;
68
+
69
+ const properties = mergedProperties
70
+ ? ( Object . fromEntries (
71
+ Object . entries ( mergedProperties ) . filter ( ( [ , value ] ) => typeof value !== 'boolean' )
72
+ ) as { [ key : string ] : JSONSchema7 } )
73
+ : undefined ;
74
+
67
75
const union = unionInfo ( schema ) ?. filter ( ( u ) => u . type !== 'null' && u . const !== null ) ;
68
76
69
77
return {
Original file line number Diff line number Diff line change @@ -628,6 +628,39 @@ describe('Zod', () => {
628
628
expect ( ( ) => zod ( schema ) ) . toThrow ( Error ) ;
629
629
} ) ;
630
630
631
+ describe . only ( 'with z.record' , ( ) => {
632
+ it ( 'should work with additionalProperties for records' , async ( ) => {
633
+ const schema = z . object ( {
634
+ id : z . string ( ) ,
635
+ options : z . record (
636
+ z . string ( ) ,
637
+ z . object ( {
638
+ label : z . string ( ) . refine ( ( value ) => value . length > 0 , {
639
+ message : 'Label is required'
640
+ } )
641
+ } )
642
+ )
643
+ } ) ;
644
+
645
+ console . dir ( zod ( schema ) . jsonSchema , { depth : 10 } ) ; //debug
646
+
647
+ let row = {
648
+ id : '1' ,
649
+ options : {
650
+ '1' : {
651
+ label : 'Option 1'
652
+ } ,
653
+ '2' : {
654
+ label : ''
655
+ }
656
+ }
657
+ } ;
658
+
659
+ const form = await superValidate ( row , zod ( schema ) ) ;
660
+ console . dir ( form , { depth : 10 } ) ; //debug
661
+ } ) ;
662
+ } ) ;
663
+
631
664
schemaTest ( zod ( schema ) ) ;
632
665
} ) ;
633
666
You can’t perform that action at this time.
0 commit comments