@@ -488,6 +488,93 @@ describe('Unions', () => {
488488 )
489489 ) . toThrowError ( SchemaError ) ;
490490 } ) ;
491+
492+ describe ( 'Array unions' , ( ) => {
493+ it ( 'should return an empty array as default value' , ( ) => {
494+ const arrays = z . object ( {
495+ tags : z . union ( [
496+ z . object ( { id : z . number ( ) , num : z . number ( ) } ) . array ( ) ,
497+ z . object ( { id : z . string ( ) , name : z . string ( ) } ) . array ( )
498+ ] )
499+ } ) ;
500+
501+ const adapter = zod ( arrays ) ;
502+ expect ( adapter . defaults ) . toEqual ( { tags : [ ] } ) ;
503+ } ) ;
504+ } ) ;
505+
506+ describe ( 'Discriminated unions and intersections' , ( ) => {
507+ enum ProfileType {
508+ STUDENT = 'STUDENT' ,
509+ FACULTY = 'FACULTY' ,
510+ STAFF = 'STAFF'
511+ }
512+
513+ const studentZSchema = z . object ( {
514+ yearOfStudy : z . number ( ) . min ( 1 ) ,
515+ branch : z . string ( ) . min ( 2 ) ,
516+ department : z . string ( ) . min ( 2 ) ,
517+ studentId : z . string ( ) . min ( 2 ) ,
518+ clubs : z . array ( z . string ( ) ) . optional ( )
519+ } ) ;
520+
521+ const facultyZSchema = z . object ( {
522+ department : z . string ( ) . min ( 2 ) ,
523+ branch : z . string ( ) . min ( 2 ) ,
524+ designation : z . string ( ) . min ( 2 ) ,
525+ facultyId : z . string ( ) . min ( 2 )
526+ } ) ;
527+
528+ const staffZSchema = z . object ( {
529+ department : z . string ( ) . min ( 2 ) ,
530+ branch : z . string ( ) . min ( 2 ) ,
531+ designation : z . string ( ) . min ( 2 ) ,
532+ staffId : z . string ( ) . min ( 2 )
533+ } ) ;
534+
535+ const profileSchema = z . discriminatedUnion ( 'type' , [
536+ z . object ( {
537+ type : z . literal ( ProfileType . STUDENT ) ,
538+ typeData : studentZSchema
539+ } ) ,
540+ z . object ( {
541+ type : z . literal ( ProfileType . FACULTY ) ,
542+ typeData : facultyZSchema
543+ } ) ,
544+ z . object ( {
545+ type : z . literal ( ProfileType . STAFF ) ,
546+ typeData : staffZSchema
547+ } )
548+ ] ) ;
549+
550+ const UserProfileZodSchema = z
551+ . object ( {
552+ name : z . string ( ) . min ( 2 ) ,
553+ email : z . string ( ) . email ( ) ,
554+ type : z . nativeEnum ( ProfileType )
555+ } )
556+ . and ( profileSchema ) ;
557+
558+ it ( 'should merge properties of object unions together' , ( ) => {
559+ const adapter = zod ( UserProfileZodSchema ) ;
560+
561+ expect ( adapter . defaults ) . toEqual ( {
562+ name : '' ,
563+ email : '' ,
564+ type : ProfileType . STUDENT ,
565+ typeData : {
566+ yearOfStudy : 0 ,
567+ branch : '' ,
568+ department : '' ,
569+ studentId : '' ,
570+ clubs : undefined ,
571+ designation : '' ,
572+ facultyId : '' ,
573+ staffId : ''
574+ }
575+ } ) ;
576+ } ) ;
577+ } ) ;
491578} ) ;
492579
493580describe ( 'Default value types' , ( ) => {
@@ -534,76 +621,3 @@ describe('Default value types', () => {
534621 } ) ;
535622 } ) ;
536623} ) ;
537-
538- describe ( 'Discriminated unions and intersections' , ( ) => {
539- enum ProfileType {
540- STUDENT = 'STUDENT' ,
541- FACULTY = 'FACULTY' ,
542- STAFF = 'STAFF'
543- }
544-
545- const studentZSchema = z . object ( {
546- yearOfStudy : z . number ( ) . min ( 1 ) ,
547- branch : z . string ( ) . min ( 2 ) ,
548- department : z . string ( ) . min ( 2 ) ,
549- studentId : z . string ( ) . min ( 2 ) ,
550- clubs : z . array ( z . string ( ) ) . optional ( )
551- } ) ;
552-
553- const facultyZSchema = z . object ( {
554- department : z . string ( ) . min ( 2 ) ,
555- branch : z . string ( ) . min ( 2 ) ,
556- designation : z . string ( ) . min ( 2 ) ,
557- facultyId : z . string ( ) . min ( 2 )
558- } ) ;
559-
560- const staffZSchema = z . object ( {
561- department : z . string ( ) . min ( 2 ) ,
562- branch : z . string ( ) . min ( 2 ) ,
563- designation : z . string ( ) . min ( 2 ) ,
564- staffId : z . string ( ) . min ( 2 )
565- } ) ;
566-
567- const profileSchema = z . discriminatedUnion ( 'type' , [
568- z . object ( {
569- type : z . literal ( ProfileType . STUDENT ) ,
570- typeData : studentZSchema
571- } ) ,
572- z . object ( {
573- type : z . literal ( ProfileType . FACULTY ) ,
574- typeData : facultyZSchema
575- } ) ,
576- z . object ( {
577- type : z . literal ( ProfileType . STAFF ) ,
578- typeData : staffZSchema
579- } )
580- ] ) ;
581-
582- const UserProfileZodSchema = z
583- . object ( {
584- name : z . string ( ) . min ( 2 ) ,
585- email : z . string ( ) . email ( ) ,
586- type : z . nativeEnum ( ProfileType )
587- } )
588- . and ( profileSchema ) ;
589-
590- it ( 'should merge properties of object unions together' , ( ) => {
591- const adapter = zod ( UserProfileZodSchema ) ;
592-
593- expect ( adapter . defaults ) . toEqual ( {
594- name : '' ,
595- email : '' ,
596- type : ProfileType . STUDENT ,
597- typeData : {
598- yearOfStudy : 0 ,
599- branch : '' ,
600- department : '' ,
601- studentId : '' ,
602- clubs : undefined ,
603- designation : '' ,
604- facultyId : '' ,
605- staffId : ''
606- }
607- } ) ;
608- } ) ;
609- } ) ;
0 commit comments