@@ -4,178 +4,162 @@ import dedent from 'ts-dedent';
4
4
import { plugin } from '../src/index' ;
5
5
6
6
describe ( 'myzod' , ( ) => {
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(): myzod.Type<PrimitiveInput> {' ,
22
- 'a: myzod.string()' ,
23
- 'b: myzod.string()' ,
24
- 'c: myzod.boolean()' ,
25
- 'd: myzod.number()' ,
26
- 'e: myzod.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(): myzod.Type<PrimitiveInput> {' ,
48
- // alphabet order
49
- 'a: myzod.string().optional().nullable(),' ,
50
- 'b: myzod.string().optional().nullable(),' ,
51
- 'c: myzod.boolean().optional().nullable(),' ,
52
- 'd: myzod.number().optional().nullable(),' ,
53
- 'e: myzod.number().optional().nullable(),' ,
54
- ] ,
55
- scalars : {
56
- ID : 'string' ,
57
- } ,
58
- } ,
59
- ] ,
60
- [
61
- 'array' ,
62
- {
63
- textSchema : /* GraphQL */ `
64
- input ArrayInput {
65
- a: [String]
66
- b: [String!]
67
- c: [String!]!
68
- d: [[String]]
69
- e: [[String]!]
70
- f: [[String]!]!
71
- }
72
- ` ,
73
- wantContains : [
74
- 'export function ArrayInputSchema(): myzod.Type<ArrayInput> {' ,
75
- 'a: myzod.array(myzod.string().nullable()).optional().nullable(),' ,
76
- 'b: myzod.array(myzod.string()).optional().nullable(),' ,
77
- 'c: myzod.array(myzod.string()),' ,
78
- 'd: myzod.array(myzod.array(myzod.string().nullable()).optional().nullable()).optional().nullable(),' ,
79
- 'e: myzod.array(myzod.array(myzod.string().nullable())).optional().nullable(),' ,
80
- 'f: myzod.array(myzod.array(myzod.string().nullable()))' ,
81
- ] ,
82
- scalars : undefined ,
83
- } ,
84
- ] ,
85
- [
86
- 'ref input object' ,
87
- {
88
- textSchema : /* GraphQL */ `
89
- input AInput {
90
- b: BInput!
91
- }
92
- input BInput {
93
- c: CInput!
94
- }
95
- input CInput {
96
- a: AInput!
97
- }
98
- ` ,
99
- wantContains : [
100
- 'export function AInputSchema(): myzod.Type<AInput> {' ,
101
- 'b: myzod.lazy(() => BInputSchema())' ,
102
- 'export function BInputSchema(): myzod.Type<BInput> {' ,
103
- 'c: myzod.lazy(() => CInputSchema())' ,
104
- 'export function CInputSchema(): myzod.Type<CInput> {' ,
105
- 'a: myzod.lazy(() => AInputSchema())' ,
106
- ] ,
107
- scalars : undefined ,
108
- } ,
109
- ] ,
110
- [
111
- 'nested input object' ,
112
- {
113
- textSchema : /* GraphQL */ `
114
- input NestedInput {
115
- child: NestedInput
116
- childrens: [NestedInput]
117
- }
118
- ` ,
119
- wantContains : [
120
- 'export function NestedInputSchema(): myzod.Type<NestedInput> {' ,
121
- 'child: myzod.lazy(() => NestedInputSchema().optional().nullable()),' ,
122
- 'childrens: myzod.array(myzod.lazy(() => NestedInputSchema().nullable())).optional().nullable()' ,
123
- ] ,
124
- scalars : undefined ,
7
+ it ( 'non-null and defined' , async ( ) => {
8
+ const schema = buildSchema ( `
9
+ input PrimitiveInput {
10
+ a: ID!
11
+ b: String!
12
+ c: Boolean!
13
+ d: Int!
14
+ e: Float!
15
+ }
16
+ ` ) ;
17
+ const result = await plugin ( schema , [ ] , {
18
+ schema : 'myzod' ,
19
+ scalars : {
20
+ ID : 'string' ,
125
21
} ,
126
- ] ,
127
- [
128
- 'enum' ,
129
- {
130
- textSchema : /* GraphQL */ `
131
- enum PageType {
132
- PUBLIC
133
- BASIC_AUTH
134
- }
135
- input PageInput {
136
- pageType: PageType!
137
- }
138
- ` ,
139
- wantContains : [
140
- 'export const PageTypeSchema = myzod.enum(PageType)' ,
141
- 'export function PageInputSchema(): myzod.Type<PageInput> {' ,
142
- 'pageType: PageTypeSchema' ,
143
- ] ,
144
- scalars : undefined ,
22
+ } , { } ) ;
23
+ expect ( result . prepend ) . toContain ( 'import * as myzod from \'myzod\'' ) ;
24
+ expect ( result . content ) . toContain ( 'export function PrimitiveInputSchema(): myzod.Type<PrimitiveInput> {' ) ;
25
+ expect ( result . content ) . toContain ( 'a: myzod.string()' ) ;
26
+ expect ( result . content ) . toContain ( 'b: myzod.string()' ) ;
27
+ expect ( result . content ) . toContain ( 'c: myzod.boolean()' ) ;
28
+ expect ( result . content ) . toContain ( 'd: myzod.number()' ) ;
29
+ expect ( result . content ) . toContain ( 'e: myzod.number()' ) ;
30
+ } ) ;
31
+
32
+ it ( 'nullish' , async ( ) => {
33
+ const schema = buildSchema ( `
34
+ input PrimitiveInput {
35
+ a: ID
36
+ b: String
37
+ c: Boolean
38
+ d: Int
39
+ e: Float
40
+ z: String! # no defined check
41
+ }
42
+ ` ) ;
43
+ const result = await plugin ( schema , [ ] , {
44
+ schema : 'myzod' ,
45
+ scalars : {
46
+ ID : 'string' ,
145
47
} ,
146
- ] ,
147
- [
148
- 'camelcase' ,
149
- {
150
- textSchema : /* GraphQL */ `
151
- input HTTPInput {
152
- method: HTTPMethod
153
- url: URL!
154
- }
48
+ } , { } ) ;
49
+ expect ( result . prepend ) . toContain ( 'import * as myzod from \'myzod\'' ) ;
50
+ expect ( result . content ) . toContain ( 'export function PrimitiveInputSchema(): myzod.Type<PrimitiveInput> {' ) ;
51
+ expect ( result . content ) . toContain ( 'a: myzod.string().optional().nullable()' ) ;
52
+ expect ( result . content ) . toContain ( 'b: myzod.string().optional().nullable()' ) ;
53
+ expect ( result . content ) . toContain ( 'c: myzod.boolean().optional().nullable()' ) ;
54
+ expect ( result . content ) . toContain ( 'd: myzod.number().optional().nullable()' ) ;
55
+ expect ( result . content ) . toContain ( 'e: myzod.number().optional().nullable()' ) ;
56
+ } ) ;
155
57
156
- enum HTTPMethod {
157
- GET
158
- POST
159
- }
58
+ it ( 'array' , async ( ) => {
59
+ const schema = buildSchema ( `
60
+ input ArrayInput {
61
+ a: [String]
62
+ b: [String!]
63
+ c: [String!]!
64
+ d: [[String]]
65
+ e: [[String]!]
66
+ f: [[String]!]!
67
+ }
68
+ ` ) ;
69
+ const result = await plugin ( schema , [ ] , {
70
+ schema : 'myzod' ,
71
+ } , { } ) ;
72
+ expect ( result . prepend ) . toContain ( 'import * as myzod from \'myzod\'' ) ;
73
+ expect ( result . content ) . toContain ( 'export function ArrayInputSchema(): myzod.Type<ArrayInput> {' ) ;
74
+ expect ( result . content ) . toContain ( 'a: myzod.array(myzod.string().nullable()).optional().nullable()' ) ;
75
+ expect ( result . content ) . toContain ( 'b: myzod.array(myzod.string()).optional().nullable()' ) ;
76
+ expect ( result . content ) . toContain ( 'c: myzod.array(myzod.string())' ) ;
77
+ expect ( result . content ) . toContain ( 'd: myzod.array(myzod.array(myzod.string().nullable()).optional().nullable()).optional().nullable()' ) ;
78
+ expect ( result . content ) . toContain ( 'e: myzod.array(myzod.array(myzod.string().nullable())).optional().nullable()' ) ;
79
+ expect ( result . content ) . toContain ( 'f: myzod.array(myzod.array(myzod.string().nullable()))' ) ;
80
+ } ) ;
160
81
161
- scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
162
- ` ,
163
- wantContains : [
164
- 'export function HttpInputSchema(): myzod.Type<HttpInput> {' ,
165
- 'export const HttpMethodSchema = myzod.enum(HttpMethod)' ,
166
- 'method: HttpMethodSchema' ,
167
- 'url: definedNonNullAnySchema' ,
168
- ] ,
169
- scalars : undefined ,
170
- } ,
171
- ] ,
172
- ] ) ( '%s' , async ( _ , { textSchema, wantContains, scalars } ) => {
173
- const schema = buildSchema ( textSchema ) ;
174
- const result = await plugin ( schema , [ ] , { schema : 'myzod' , scalars } , { } ) ;
82
+ it ( 'ref input object' , async ( ) => {
83
+ const schema = buildSchema ( `
84
+ input AInput {
85
+ b: BInput!
86
+ }
87
+ input BInput {
88
+ c: CInput!
89
+ }
90
+ input CInput {
91
+ a: AInput!
92
+ }
93
+ ` ) ;
94
+ const result = await plugin ( schema , [ ] , {
95
+ schema : 'myzod' ,
96
+ } , { } ) ;
175
97
expect ( result . prepend ) . toContain ( 'import * as myzod from \'myzod\'' ) ;
98
+ expect ( result . content ) . toContain ( 'export function AInputSchema(): myzod.Type<AInput> {' ) ;
99
+ expect ( result . content ) . toContain ( 'b: myzod.lazy(() => BInputSchema())' ) ;
100
+ expect ( result . content ) . toContain ( 'export function BInputSchema(): myzod.Type<BInput> {' ) ;
101
+ expect ( result . content ) . toContain ( 'c: myzod.lazy(() => CInputSchema())' ) ;
102
+ expect ( result . content ) . toContain ( 'export function CInputSchema(): myzod.Type<CInput> {' ) ;
103
+ expect ( result . content ) . toContain ( 'a: myzod.lazy(() => AInputSchema())' ) ;
104
+ } ) ;
176
105
177
- for ( const wantContain of wantContains )
178
- expect ( result . content ) . toContain ( wantContain ) ;
106
+ it ( 'nested input object' , async ( ) => {
107
+ const schema = buildSchema ( `
108
+ input NestedInput {
109
+ child: NestedInput
110
+ childrens: [NestedInput]
111
+ }
112
+ ` ) ;
113
+ const result = await plugin ( schema , [ ] , {
114
+ schema : 'myzod' ,
115
+ } , { } ) ;
116
+ expect ( result . prepend ) . toContain ( 'import * as myzod from \'myzod\'' ) ;
117
+ expect ( result . content ) . toContain ( 'export function NestedInputSchema(): myzod.Type<NestedInput> {' ) ;
118
+ expect ( result . content ) . toContain ( 'child: myzod.lazy(() => NestedInputSchema().optional().nullable())' ) ;
119
+ expect ( result . content ) . toContain ( 'childrens: myzod.array(myzod.lazy(() => NestedInputSchema().nullable())).optional().nullable()' ) ;
120
+ } ) ;
121
+
122
+ it ( 'enum' , async ( ) => {
123
+ const schema = buildSchema ( `
124
+ enum PageType {
125
+ PUBLIC
126
+ BASIC_AUTH
127
+ }
128
+ input PageInput {
129
+ pageType: PageType!
130
+ }
131
+ ` ) ;
132
+ const result = await plugin ( schema , [ ] , {
133
+ schema : 'myzod' ,
134
+ } , { } ) ;
135
+ expect ( result . prepend ) . toContain ( 'import * as myzod from \'myzod\'' ) ;
136
+ expect ( result . content ) . toContain ( 'export const PageTypeSchema = myzod.enum(PageType)' ) ;
137
+ expect ( result . content ) . toContain ( 'export function PageInputSchema(): myzod.Type<PageInput> {' ) ;
138
+ expect ( result . content ) . toContain ( 'pageType: PageTypeSchema' ) ;
139
+ } ) ;
140
+
141
+ it ( 'camelcase' , async ( ) => {
142
+ const schema = buildSchema ( `
143
+ input HTTPInput {
144
+ method: HTTPMethod
145
+ url: URL!
146
+ }
147
+
148
+ enum HTTPMethod {
149
+ GET
150
+ POST
151
+ }
152
+
153
+ scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
154
+ ` ) ;
155
+ const result = await plugin ( schema , [ ] , {
156
+ schema : 'myzod' ,
157
+ } , { } ) ;
158
+ expect ( result . prepend ) . toContain ( 'import * as myzod from \'myzod\'' ) ;
159
+ expect ( result . content ) . toContain ( 'export function HttpInputSchema(): myzod.Type<HttpInput> {' ) ;
160
+ expect ( result . content ) . toContain ( 'export const HttpMethodSchema = myzod.enum(HttpMethod)' ) ;
161
+ expect ( result . content ) . toContain ( 'method: HttpMethodSchema' ) ;
162
+ expect ( result . content ) . toContain ( 'url: definedNonNullAnySchema' ) ;
179
163
} ) ;
180
164
181
165
it ( 'with scalars' , async ( ) => {
@@ -873,7 +857,7 @@ describe('myzod', () => {
873
857
author: Author
874
858
title: String
875
859
}
876
-
860
+
877
861
interface Author {
878
862
books: [Book]
879
863
name: String
@@ -906,19 +890,19 @@ describe('myzod', () => {
906
890
title: String!
907
891
author: Author!
908
892
}
909
-
893
+
910
894
type Textbook implements Book {
911
895
title: String!
912
896
author: Author!
913
897
courses: [String!]!
914
898
}
915
-
899
+
916
900
type ColoringBook implements Book {
917
901
title: String!
918
902
author: Author!
919
903
colors: [String!]!
920
904
}
921
-
905
+
922
906
type Author {
923
907
books: [Book!]
924
908
name: String
0 commit comments