@@ -185,23 +185,26 @@ describe.concurrent('zuifromJSONSchemaNext', () => {
185185 assert ( zSchema ) . toEqual ( expected )
186186 } )
187187
188- test ( 'should map DiscriminatedUnionSchema to ZodUnion' , ( ) => {
189- const jSchema = buildSchema ( {
190- anyOf : [
191- {
192- type : 'object' ,
193- properties : { type : { type : 'string' , const : 'A' } , a : { type : 'string' } } ,
194- required : [ 'type' , 'a' ] ,
195- } ,
196- {
197- type : 'object' ,
198- properties : { type : { type : 'string' , const : 'B' } , b : { type : 'number' } } ,
199- required : [ 'type' , 'b' ] ,
200- } ,
201- ] ,
202- } )
188+ test ( 'should map DiscriminatedUnionSchema to ZodDiscriminatedUnion' , ( ) => {
189+ const jSchema = buildSchema (
190+ {
191+ anyOf : [
192+ {
193+ type : 'object' ,
194+ properties : { type : { type : 'string' , const : 'A' } , a : { type : 'string' } } ,
195+ required : [ 'type' , 'a' ] ,
196+ } ,
197+ {
198+ type : 'object' ,
199+ properties : { type : { type : 'string' , const : 'B' } , b : { type : 'number' } } ,
200+ required : [ 'type' , 'b' ] ,
201+ } ,
202+ ] ,
203+ } ,
204+ { def : { typeName : 'ZodDiscriminatedUnion' , discriminator : 'type' } } ,
205+ )
203206 const zSchema = fromJSONSchema ( jSchema )
204- const expected = z . union ( [
207+ const expected = z . discriminatedUnion ( 'type' , [
205208 z . object ( { type : z . literal ( 'A' ) , a : z . string ( ) } ) ,
206209 z . object ( { type : z . literal ( 'B' ) , b : z . number ( ) } ) ,
207210 ] )
@@ -662,7 +665,7 @@ describe.concurrent('zuifromJSONSchemaNext', () => {
662665 expect ( getTypescriptType ( original ) ) . toBe ( getTypescriptType ( restored ) )
663666 } )
664667
665- test ( 'should preserve discriminated union with descriptions' , ( ) => {
668+ test ( 'should preserve union with descriptions' , ( ) => {
666669 const original = z
667670 . union ( [
668671 z . object ( { type : z . literal ( 'success' ) , data : z . string ( ) . describe ( 'Success data' ) } ) ,
@@ -676,6 +679,20 @@ describe.concurrent('zuifromJSONSchemaNext', () => {
676679 expect ( restored . _def . description ) . toBe ( 'API Response' )
677680 } )
678681
682+ test ( 'should preserve discriminated union with descriptions' , ( ) => {
683+ const original = z
684+ . discriminatedUnion ( 'type' , [
685+ z . object ( { type : z . literal ( 'success' ) , data : z . string ( ) . describe ( 'Success data' ) } ) ,
686+ z . object ( { type : z . literal ( 'error' ) , message : z . string ( ) . describe ( 'Error message' ) } ) ,
687+ ] )
688+ . describe ( 'API Response' )
689+
690+ const restored = roundTrip ( original )
691+
692+ expect ( getTypescriptType ( original ) ) . toBe ( getTypescriptType ( restored ) )
693+ expect ( restored . _def . description ) . toBe ( 'API Response' )
694+ } )
695+
679696 test ( 'should preserve nullable fields' , ( ) => {
680697 const original = z . object ( {
681698 name : z . string ( ) ,
@@ -930,7 +947,7 @@ describe.concurrent('zuifromJSONSchemaNext', () => {
930947 expect ( getTypescriptType ( original ) ) . toBe ( getTypescriptType ( restored ) )
931948 } )
932949
933- test ( 'should preserve complex discriminated union structure' , ( ) => {
950+ test ( 'should preserve complex union structure' , ( ) => {
934951 const original = z . union ( [
935952 z . object ( {
936953 kind : z . literal ( 'circle' ) ,
@@ -951,6 +968,24 @@ describe.concurrent('zuifromJSONSchemaNext', () => {
951968 expect ( getTypescriptType ( original ) ) . toBe ( getTypescriptType ( restored ) )
952969 } )
953970
971+ test ( 'should preserve discriminated union of objects with optional fields' , ( ) => {
972+ const original = z . discriminatedUnion ( 'type' , [
973+ z . object ( {
974+ type : z . literal ( 'A' ) ,
975+ value : z . string ( ) ,
976+ extra : z . number ( ) . optional ( ) ,
977+ } ) ,
978+ z . object ( {
979+ type : z . literal ( 'B' ) ,
980+ data : z . boolean ( ) ,
981+ meta : z . string ( ) . optional ( ) ,
982+ } ) ,
983+ ] )
984+ const restored = roundTrip ( original )
985+
986+ expect ( getTypescriptType ( original ) ) . toBe ( getTypescriptType ( restored ) )
987+ } )
988+
954989 test ( 'should preserve union of objects with optional fields' , ( ) => {
955990 const original = z . union ( [
956991 z . object ( {
0 commit comments