@@ -610,12 +610,19 @@ describe('valibot', () => {
610
610
` )
611
611
} ) ;
612
612
} )
613
- it ( 'correctly reference generated union types' , async ( ) => {
613
+ it ( 'generate union types with single element ' , async ( ) => {
614
614
const schema = buildSchema ( /* GraphQL */ `
615
+ type Square {
616
+ size: Int
617
+ }
615
618
type Circle {
616
619
radius: Int
617
620
}
618
- union Shape = Circle
621
+ union Shape = Circle | Square
622
+
623
+ type Geometry {
624
+ shape: Shape
625
+ }
619
626
` ) ;
620
627
621
628
const result = await plugin (
@@ -631,6 +638,13 @@ describe('valibot', () => {
631
638
expect ( result . content ) . toMatchInlineSnapshot ( `
632
639
"
633
640
641
+ export function SquareSchema(): v.GenericSchema<Square> {
642
+ return v.object({
643
+ __typename: v.optional(v.literal('Square')),
644
+ size: v.nullish(v.number())
645
+ })
646
+ }
647
+
634
648
export function CircleSchema(): v.GenericSchema<Circle> {
635
649
return v.object({
636
650
__typename: v.optional(v.literal('Circle')),
@@ -639,24 +653,24 @@ describe('valibot', () => {
639
653
}
640
654
641
655
export function ShapeSchema() {
642
- return CircleSchema()
656
+ return v.union([CircleSchema(), SquareSchema()])
657
+ }
658
+
659
+ export function GeometrySchema(): v.GenericSchema<Geometry> {
660
+ return v.object({
661
+ __typename: v.optional(v.literal('Geometry')),
662
+ shape: v.nullish(ShapeSchema())
663
+ })
643
664
}
644
665
"
645
666
` )
646
667
} ) ;
647
- it ( 'generate enum union types' , async ( ) => {
668
+ it ( 'correctly reference generated union types' , async ( ) => {
648
669
const schema = buildSchema ( /* GraphQL */ `
649
- enum PageType {
650
- PUBLIC
651
- BASIC_AUTH
652
- }
653
-
654
- enum MethodType {
655
- GET
656
- POST
670
+ type Circle {
671
+ radius: Int
657
672
}
658
-
659
- union AnyType = PageType | MethodType
673
+ union Shape = Circle
660
674
` ) ;
661
675
662
676
const result = await plugin (
@@ -671,29 +685,33 @@ describe('valibot', () => {
671
685
672
686
expect ( result . content ) . toMatchInlineSnapshot ( `
673
687
"
674
- export const PageTypeSchema = v.enum_(PageType);
675
688
676
- export const MethodTypeSchema = v.enum_(MethodType);
689
+ export function CircleSchema(): v.GenericSchema<Circle> {
690
+ return v.object({
691
+ __typename: v.optional(v.literal('Circle')),
692
+ radius: v.nullish(v.number())
693
+ })
694
+ }
677
695
678
- export function AnyTypeSchema () {
679
- return v.union([PageTypeSchema, MethodTypeSchema] )
696
+ export function ShapeSchema () {
697
+ return CircleSchema( )
680
698
}
681
699
"
682
700
` )
683
701
} ) ;
684
- it ( 'generate union types with single element, export as const ' , async ( ) => {
702
+ it ( 'generate enum union types' , async ( ) => {
685
703
const schema = buildSchema ( /* GraphQL */ `
686
- type Square {
687
- size: Int
688
- }
689
- type Circle {
690
- radius: Int
704
+ enum PageType {
705
+ PUBLIC
706
+ BASIC_AUTH
691
707
}
692
- union Shape = Circle | Square
693
708
694
- type Geometry {
695
- shape: Shape
709
+ enum MethodType {
710
+ GET
711
+ POST
696
712
}
713
+
714
+ union AnyType = PageType | MethodType
697
715
` ) ;
698
716
699
717
const result = await plugin (
@@ -702,41 +720,23 @@ describe('valibot', () => {
702
720
{
703
721
schema : 'valibot' ,
704
722
withObjectType : true ,
705
- validationSchemaExportType : 'const' ,
706
723
} ,
707
724
{ } ,
708
725
) ;
709
726
710
727
expect ( result . content ) . toMatchInlineSnapshot ( `
711
728
"
729
+ export const PageTypeSchema = v.enum_(PageType);
712
730
713
- export function CircleSchema(): v.GenericSchema<Circle> {
714
- return v.object({
715
- __typename: v.optional(v.literal('Circle')),
716
- radius: v.nullish(v.number())
717
- })
718
- }
719
-
720
- export function SquareSchema(): v.GenericSchema<Square> {
721
- return v.object({
722
- __typename: v.optional(v.literal('Square')),
723
- size: v.nullish(v.number())
724
- })
725
- }
726
-
727
- export function ShapeSchema() {
728
- return v.union([CircleSchema(), SquareSchema()])
729
- }
731
+ export const MethodTypeSchema = v.enum_(MethodType);
730
732
731
- export function GeometrySchema(): v.GenericSchema<Geometry> {
732
- return v.object({
733
- __typename: v.optional(v.literal('Geometry')),
734
- shape: v.nullish(ShapeSchema())
735
- })
733
+ export function AnyTypeSchema() {
734
+ return v.union([PageTypeSchema, MethodTypeSchema])
736
735
}
737
736
"
738
737
` )
739
738
} ) ;
739
+ it . todo ( 'generate union types with single element, export as const' )
740
740
it ( 'with object arguments' , async ( ) => {
741
741
const schema = buildSchema ( /* GraphQL */ `
742
742
type MyType {
@@ -778,4 +778,166 @@ describe('valibot', () => {
778
778
"
779
779
` )
780
780
} ) ;
781
+ describe ( 'with InterfaceType' , ( ) => {
782
+ it ( 'not generate if withObjectType false' , async ( ) => {
783
+ const schema = buildSchema ( /* GraphQL */ `
784
+ interface User {
785
+ id: ID!
786
+ name: String
787
+ }
788
+ ` ) ;
789
+ const result = await plugin (
790
+ schema ,
791
+ [ ] ,
792
+ {
793
+ schema : 'valibot' ,
794
+ withObjectType : false ,
795
+ } ,
796
+ { } ,
797
+ ) ;
798
+ expect ( result . content ) . not . toContain ( 'export function UserSchema(): v.GenericSchema<User>' ) ;
799
+ } ) ;
800
+ it ( 'generate if withObjectType true' , async ( ) => {
801
+ const schema = buildSchema ( /* GraphQL */ `
802
+ interface Book {
803
+ title: String
804
+ }
805
+ ` ) ;
806
+ const result = await plugin (
807
+ schema ,
808
+ [ ] ,
809
+ {
810
+ schema : 'valibot' ,
811
+ withObjectType : true ,
812
+ } ,
813
+ { } ,
814
+ ) ;
815
+ expect ( result . content ) . toMatchInlineSnapshot ( `
816
+ "
817
+
818
+ export function BookSchema(): v.GenericSchema<Book> {
819
+ return v.object({
820
+ title: v.nullish(v.string())
821
+ })
822
+ }
823
+ "
824
+ ` )
825
+ } ) ;
826
+ it ( 'generate interface type contains interface type' , async ( ) => {
827
+ const schema = buildSchema ( /* GraphQL */ `
828
+ interface Book {
829
+ author: Author
830
+ title: String
831
+ }
832
+
833
+ interface Author {
834
+ books: [Book]
835
+ name: String
836
+ }
837
+ ` ) ;
838
+ const result = await plugin (
839
+ schema ,
840
+ [ ] ,
841
+ {
842
+ schema : 'valibot' ,
843
+ withObjectType : true ,
844
+ } ,
845
+ { } ,
846
+ ) ;
847
+ expect ( result . content ) . toMatchInlineSnapshot ( `
848
+ "
849
+
850
+ export function BookSchema(): v.GenericSchema<Book> {
851
+ return v.object({
852
+ author: v.nullish(AuthorSchema()),
853
+ title: v.nullish(v.string())
854
+ })
855
+ }
856
+
857
+ export function AuthorSchema(): v.GenericSchema<Author> {
858
+ return v.object({
859
+ books: v.nullish(v.array(v.nullable(BookSchema()))),
860
+ name: v.nullish(v.string())
861
+ })
862
+ }
863
+ "
864
+ ` )
865
+ } ) ;
866
+ it ( 'generate object type contains interface type' , async ( ) => {
867
+ const schema = buildSchema ( /* GraphQL */ `
868
+ interface Book {
869
+ title: String!
870
+ author: Author!
871
+ }
872
+
873
+ type Textbook implements Book {
874
+ title: String!
875
+ author: Author!
876
+ courses: [String!]!
877
+ }
878
+
879
+ type ColoringBook implements Book {
880
+ title: String!
881
+ author: Author!
882
+ colors: [String!]!
883
+ }
884
+
885
+ type Author {
886
+ books: [Book!]
887
+ name: String
888
+ }
889
+ ` ) ;
890
+ const result = await plugin (
891
+ schema ,
892
+ [ ] ,
893
+ {
894
+ schema : 'valibot' ,
895
+ withObjectType : true ,
896
+ } ,
897
+ { } ,
898
+ ) ;
899
+ expect ( result . content ) . toMatchInlineSnapshot ( `
900
+ "
901
+
902
+ export function BookSchema(): v.GenericSchema<Book> {
903
+ return v.object({
904
+ title: v.string(),
905
+ author: AuthorSchema()
906
+ })
907
+ }
908
+
909
+ export function TextbookSchema(): v.GenericSchema<Textbook> {
910
+ return v.object({
911
+ __typename: v.optional(v.literal('Textbook')),
912
+ title: v.string(),
913
+ author: AuthorSchema(),
914
+ courses: v.array(v.string())
915
+ })
916
+ }
917
+
918
+ export function ColoringBookSchema(): v.GenericSchema<ColoringBook> {
919
+ return v.object({
920
+ __typename: v.optional(v.literal('ColoringBook')),
921
+ title: v.string(),
922
+ author: AuthorSchema(),
923
+ colors: v.array(v.string())
924
+ })
925
+ }
926
+
927
+ export function AuthorSchema(): v.GenericSchema<Author> {
928
+ return v.object({
929
+ __typename: v.optional(v.literal('Author')),
930
+ books: v.nullish(v.array(BookSchema())),
931
+ name: v.nullish(v.string())
932
+ })
933
+ }
934
+ "
935
+ ` )
936
+ } ) ;
937
+ } )
938
+ it . todo ( 'properly generates custom directive values' )
939
+ it . todo ( 'exports as const instead of func' )
940
+ it . todo ( 'generate both input & type, export as const' )
941
+ it . todo ( 'issue #394' )
942
+ it . todo ( 'issue #394' )
781
943
} )
0 commit comments