@@ -4,63 +4,68 @@ import { dedent } from 'ts-dedent';
4
4
import { plugin } from '../src/index' ;
5
5
6
6
describe ( 'zod' , ( ) => {
7
- it . each ( [
8
- [
9
- 'non-null and defined' ,
10
- {
11
- textSchema : /* GraphQL */ `
12
- input PrimitiveInput {
13
- a: ID!
14
- b: String!
15
- c: Boolean!
16
- d: Int!
17
- e: Float!
18
- }
19
- ` ,
20
- wantContains : [
21
- 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
22
- 'a: z.string()' ,
23
- 'b: z.string()' ,
24
- 'c: z.boolean()' ,
25
- 'd: z.number()' ,
26
- 'e: z.number()' ,
27
- ] ,
28
- scalars : {
29
- ID : 'string' ,
30
- } ,
31
- } ,
32
- ] ,
33
- [
34
- 'nullish' ,
35
- {
36
- textSchema : /* GraphQL */ `
37
- input PrimitiveInput {
38
- a: ID
39
- b: String
40
- c: Boolean
41
- d: Int
42
- e: Float
43
- z: String! # no defined check
44
- }
45
- ` ,
46
- wantContains : [
47
- 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
48
- // alphabet order
49
- 'a: z.string().nullish(),' ,
50
- 'b: z.string().nullish(),' ,
51
- 'c: z.boolean().nullish(),' ,
52
- 'd: z.number().nullish(),' ,
53
- 'e: z.number().nullish(),' ,
54
- ] ,
55
- scalars : {
56
- ID : 'string' ,
57
- } ,
58
- } ,
59
- ] ,
60
- [
61
- 'array' ,
62
- {
63
- textSchema : /* GraphQL */ `
7
+ it ( "non-null and defined" , async ( ) => {
8
+ const schema = buildSchema ( /* GraphQL */ `
9
+ input PrimitiveInput {
10
+ a: ID!
11
+ b: String!
12
+ c: Boolean!
13
+ d: Int!
14
+ e: Float!
15
+ }
16
+ ` ) ;
17
+ const scalars = {
18
+ ID : 'string' ,
19
+ }
20
+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
21
+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
22
+
23
+ const wantContains = [
24
+ 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
25
+ 'a: z.string()' ,
26
+ 'b: z.string()' ,
27
+ 'c: z.boolean()' ,
28
+ 'd: z.number()' ,
29
+ 'e: z.number()' ,
30
+ ]
31
+
32
+ for ( const wantContain of wantContains )
33
+ expect ( result . content ) . toContain ( wantContain ) ;
34
+ } )
35
+
36
+ it ( "nullish" , async ( ) => {
37
+ const schema = buildSchema ( /* GraphQL */ `
38
+ input PrimitiveInput {
39
+ a: ID
40
+ b: String
41
+ c: Boolean
42
+ d: Int
43
+ e: Float
44
+ z: String! # no defined check
45
+ }
46
+ ` ) ;
47
+ const scalars = {
48
+ ID : 'string' ,
49
+ }
50
+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
51
+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
52
+
53
+ const wantContains = [
54
+ 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
55
+ // alphabet order
56
+ 'a: z.string().nullish(),' ,
57
+ 'b: z.string().nullish(),' ,
58
+ 'c: z.boolean().nullish(),' ,
59
+ 'd: z.number().nullish(),' ,
60
+ 'e: z.number().nullish(),' ,
61
+ ]
62
+
63
+ for ( const wantContain of wantContains )
64
+ expect ( result . content ) . toContain ( wantContain ) ;
65
+ } )
66
+
67
+ it ( "array" , async ( ) => {
68
+ const schema = buildSchema ( /* GraphQL */ `
64
69
input ArrayInput {
65
70
a: [String]
66
71
b: [String!]
@@ -69,23 +74,27 @@ describe('zod', () => {
69
74
e: [[String]!]
70
75
f: [[String]!]!
71
76
}
72
- ` ,
73
- wantContains : [
74
- 'export function ArrayInputSchema(): z.ZodObject<Properties<ArrayInput>>' ,
75
- 'a: z.array(z.string().nullable()).nullish(),' ,
76
- 'b: z.array(z.string()).nullish(),' ,
77
- 'c: z.array(z.string()),' ,
78
- 'd: z.array(z.array(z.string().nullable()).nullish()).nullish(),' ,
79
- 'e: z.array(z.array(z.string().nullable())).nullish(),' ,
80
- 'f: z.array(z.array(z.string().nullable()))' ,
81
- ] ,
82
- scalars : undefined ,
83
- } ,
84
- ] ,
85
- [
86
- 'ref input object' ,
87
- {
88
- textSchema : /* GraphQL */ `
77
+ ` ) ;
78
+ const scalars = undefined
79
+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
80
+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
81
+
82
+ const wantContains = [
83
+ 'export function ArrayInputSchema(): z.ZodObject<Properties<ArrayInput>>' ,
84
+ 'a: z.array(z.string().nullable()).nullish(),' ,
85
+ 'b: z.array(z.string()).nullish(),' ,
86
+ 'c: z.array(z.string()),' ,
87
+ 'd: z.array(z.array(z.string().nullable()).nullish()).nullish(),' ,
88
+ 'e: z.array(z.array(z.string().nullable())).nullish(),' ,
89
+ 'f: z.array(z.array(z.string().nullable()))' ,
90
+ ]
91
+
92
+ for ( const wantContain of wantContains )
93
+ expect ( result . content ) . toContain ( wantContain ) ;
94
+ } )
95
+
96
+ it ( "ref input object" , async ( ) => {
97
+ const schema = buildSchema ( /* GraphQL */ `
89
98
input AInput {
90
99
b: BInput!
91
100
}
@@ -95,59 +104,71 @@ describe('zod', () => {
95
104
input CInput {
96
105
a: AInput!
97
106
}
98
- ` ,
99
- wantContains : [
100
- 'export function AInputSchema(): z.ZodObject<Properties<AInput>>' ,
101
- 'b: z.lazy(() => BInputSchema())' ,
102
- 'export function BInputSchema(): z.ZodObject<Properties<BInput>>' ,
103
- 'c: z.lazy(() => CInputSchema())' ,
104
- 'export function CInputSchema(): z.ZodObject<Properties<CInput>>' ,
105
- 'a: z.lazy(() => AInputSchema())' ,
106
- ] ,
107
- scalars : undefined ,
108
- } ,
109
- ] ,
110
- [
111
- 'nested input object' ,
112
- {
113
- textSchema : /* GraphQL */ `
107
+ ` ) ;
108
+ const scalars = undefined
109
+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
110
+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
111
+
112
+ const wantContains = [
113
+ 'export function AInputSchema(): z.ZodObject<Properties<AInput>>' ,
114
+ 'b: z.lazy(() => BInputSchema())' ,
115
+ 'export function BInputSchema(): z.ZodObject<Properties<BInput>>' ,
116
+ 'c: z.lazy(() => CInputSchema())' ,
117
+ 'export function CInputSchema(): z.ZodObject<Properties<CInput>>' ,
118
+ 'a: z.lazy(() => AInputSchema())' ,
119
+ ]
120
+
121
+ for ( const wantContain of wantContains )
122
+ expect ( result . content ) . toContain ( wantContain ) ;
123
+ } )
124
+
125
+ it ( 'nested input object' , async ( ) => {
126
+ const schema = buildSchema ( /* GraphQL */ `
114
127
input NestedInput {
115
128
child: NestedInput
116
129
childrens: [NestedInput]
117
130
}
118
- ` ,
119
- wantContains : [
120
- 'export function NestedInputSchema(): z.ZodObject<Properties<NestedInput>>' ,
121
- 'child: z.lazy(() => NestedInputSchema().nullish()),' ,
122
- 'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()' ,
123
- ] ,
124
- scalars : undefined ,
125
- } ,
126
- ] ,
127
- [
128
- 'enum' ,
129
- {
130
- textSchema : /* GraphQL */ `
131
+ ` ) ;
132
+ const scalars = undefined
133
+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
134
+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
135
+
136
+ const wantContains = [
137
+ 'export function NestedInputSchema(): z.ZodObject<Properties<NestedInput>>' ,
138
+ 'child: z.lazy(() => NestedInputSchema().nullish()),' ,
139
+ 'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()' ,
140
+ ]
141
+
142
+ for ( const wantContain of wantContains )
143
+ expect ( result . content ) . toContain ( wantContain ) ;
144
+ } )
145
+
146
+ it ( 'enum' , async ( ) => {
147
+ const schema = buildSchema ( /* GraphQL */ `
131
148
enum PageType {
132
149
PUBLIC
133
150
BASIC_AUTH
134
151
}
135
152
input PageInput {
136
153
pageType: PageType!
137
154
}
138
- ` ,
139
- wantContains : [
140
- 'export const PageTypeSchema = z.nativeEnum(PageType)' ,
141
- 'export function PageInputSchema(): z.ZodObject<Properties<PageInput>>' ,
142
- 'pageType: PageTypeSchema' ,
143
- ] ,
144
- scalars : undefined ,
145
- } ,
146
- ] ,
147
- [
148
- 'camelcase' ,
149
- {
150
- textSchema : /* GraphQL */ `
155
+ ` ) ;
156
+ const scalars = undefined
157
+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
158
+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
159
+
160
+ const wantContains = [
161
+ 'export const PageTypeSchema = z.nativeEnum(PageType)' ,
162
+ 'export function PageInputSchema(): z.ZodObject<Properties<PageInput>>' ,
163
+ 'pageType: PageTypeSchema' ,
164
+ ]
165
+
166
+ for ( const wantContain of wantContains )
167
+ expect ( result . content ) . toContain ( wantContain ) ;
168
+ } )
169
+
170
+ it ( 'camelcase' , async ( ) => {
171
+ const schema = buildSchema ( /* GraphQL */ `
151
172
input HTTPInput {
152
173
method: HTTPMethod
153
174
url: URL!
@@ -159,24 +180,21 @@ describe('zod', () => {
159
180
}
160
181
161
182
scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
162
- ` ,
163
- wantContains : [
164
- 'export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>>' ,
165
- 'export const HttpMethodSchema = z.nativeEnum(HttpMethod)' ,
166
- 'method: HttpMethodSchema' ,
167
- 'url: definedNonNullAnySchema' ,
168
- ] ,
169
- scalars : undefined ,
170
- } ,
171
- ] ,
172
- ] ) ( '%s' , async ( _ , { textSchema, wantContains, scalars } ) => {
173
- const schema = buildSchema ( textSchema ) ;
183
+ ` ) ;
184
+ const scalars = undefined
174
185
const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
175
186
expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
176
187
188
+ const wantContains = [
189
+ 'export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>>' ,
190
+ 'export const HttpMethodSchema = z.nativeEnum(HttpMethod)' ,
191
+ 'method: HttpMethodSchema' ,
192
+ 'url: definedNonNullAnySchema' ,
193
+ ]
194
+
177
195
for ( const wantContain of wantContains )
178
196
expect ( result . content ) . toContain ( wantContain ) ;
179
- } ) ;
197
+ } )
180
198
181
199
it ( 'with scalars' , async ( ) => {
182
200
const schema = buildSchema ( /* GraphQL */ `
@@ -973,7 +991,7 @@ describe('zod', () => {
973
991
author: Author
974
992
title: String
975
993
}
976
-
994
+
977
995
interface Author {
978
996
books: [Book]
979
997
name: String
@@ -1007,19 +1025,19 @@ describe('zod', () => {
1007
1025
title: String!
1008
1026
author: Author!
1009
1027
}
1010
-
1028
+
1011
1029
type Textbook implements Book {
1012
1030
title: String!
1013
1031
author: Author!
1014
1032
courses: [String!]!
1015
1033
}
1016
-
1034
+
1017
1035
type ColoringBook implements Book {
1018
1036
title: String!
1019
1037
author: Author!
1020
1038
colors: [String!]!
1021
1039
}
1022
-
1040
+
1023
1041
type Author {
1024
1042
books: [Book!]
1025
1043
name: String
0 commit comments