1
1
// the tests in tests/graphql.js use this schema to ensure the integrity
2
2
// of the data in src/graphql/data/*.json
3
3
4
+ // JSON Schema type definitions for AJV validation
5
+ interface JSONSchema {
6
+ type ?: string
7
+ required ?: string [ ]
8
+ properties ?: Record < string , JSONSchema >
9
+ items ?: JSONSchema
10
+ pattern ?: string
11
+ }
12
+
13
+ interface ValidatorSchema extends JSONSchema {
14
+ type : 'object'
15
+ required : string [ ]
16
+ properties : Record < string , JSONSchema >
17
+ }
18
+
4
19
// PREVIEWS
5
- export const previewsValidator = {
20
+ export const previewsValidator : ValidatorSchema = {
6
21
type : 'object' ,
7
22
required : [
8
23
'title' ,
@@ -39,7 +54,7 @@ export const previewsValidator = {
39
54
}
40
55
41
56
// UPCOMING CHANGES
42
- export const upcomingChangesValidator = {
57
+ export const upcomingChangesValidator : ValidatorSchema = {
43
58
type : 'object' ,
44
59
required : [ 'location' , 'description' , 'reason' , 'date' , 'criticality' , 'owner' ] ,
45
60
properties : {
@@ -69,7 +84,7 @@ export const upcomingChangesValidator = {
69
84
70
85
// SCHEMAS
71
86
// many GraphQL schema members have these core properties
72
- const coreProps = {
87
+ const coreProps : JSONSchema = {
73
88
properties : {
74
89
name : {
75
90
type : 'string' ,
@@ -102,7 +117,7 @@ const coreProps = {
102
117
// some GraphQL schema members have the core properties plus an 'args' object
103
118
const corePropsPlusArgs = dup ( coreProps )
104
119
105
- corePropsPlusArgs . properties . args = {
120
+ corePropsPlusArgs . properties ! . args = {
106
121
type : 'array' ,
107
122
items : {
108
123
type : 'object' ,
@@ -111,24 +126,24 @@ corePropsPlusArgs.properties.args = {
111
126
}
112
127
113
128
// the args object can have defaultValue prop
114
- corePropsPlusArgs . properties . args . items . properties . defaultValue = {
129
+ corePropsPlusArgs . properties ! . args . items ! . properties ! . defaultValue = {
115
130
type : 'boolean' ,
116
131
}
117
132
118
133
const corePropsNoType = dup ( coreProps )
119
- delete corePropsNoType . properties . type
134
+ delete corePropsNoType . properties ! . type
120
135
121
136
const corePropsNoDescription = dup ( coreProps )
122
- delete corePropsNoDescription . properties . description
137
+ delete corePropsNoDescription . properties ! . description
123
138
124
139
// QUERIES
125
- const queries = dup ( corePropsPlusArgs )
140
+ const queries = dup ( corePropsPlusArgs ) as ValidatorSchema
126
141
127
142
queries . type = 'object'
128
143
queries . required = [ 'name' , 'type' , 'kind' , 'id' , 'href' , 'description' ]
129
144
130
145
// MUTATIONS
131
- const mutations = dup ( corePropsNoType )
146
+ const mutations = dup ( corePropsNoType ) as ValidatorSchema
132
147
133
148
mutations . type = 'object'
134
149
mutations . required = [ 'name' , 'kind' , 'id' , 'href' , 'description' , 'inputFields' , 'returnFields' ]
@@ -150,7 +165,7 @@ mutations.properties.returnFields = {
150
165
}
151
166
152
167
// OBJECTS
153
- const objects = dup ( corePropsNoType )
168
+ const objects = dup ( corePropsNoType ) as ValidatorSchema
154
169
155
170
objects . type = 'object'
156
171
objects . required = [ 'name' , 'kind' , 'id' , 'href' , 'description' , 'fields' ]
@@ -183,7 +198,7 @@ objects.properties.implements = {
183
198
}
184
199
185
200
// INTERFACES
186
- const interfaces = dup ( corePropsNoType )
201
+ const interfaces = dup ( corePropsNoType ) as ValidatorSchema
187
202
188
203
interfaces . type = 'object'
189
204
interfaces . required = [ 'name' , 'kind' , 'id' , 'href' , 'description' , 'fields' ]
@@ -197,7 +212,7 @@ interfaces.properties.fields = {
197
212
}
198
213
199
214
// ENUMS
200
- const enums = dup ( corePropsNoType )
215
+ const enums = dup ( corePropsNoType ) as ValidatorSchema
201
216
202
217
enums . type = 'object'
203
218
enums . required = [ 'name' , 'kind' , 'id' , 'href' , 'description' , 'values' ]
@@ -219,7 +234,7 @@ enums.properties.values = {
219
234
}
220
235
221
236
// UNIONS
222
- const unions = dup ( corePropsNoType )
237
+ const unions = dup ( corePropsNoType ) as ValidatorSchema
223
238
224
239
unions . type = 'object'
225
240
unions . required = [ 'name' , 'kind' , 'id' , 'href' , 'description' , 'possibleTypes' ]
@@ -244,7 +259,7 @@ unions.properties.possibleTypes = {
244
259
}
245
260
246
261
// INPUT OBJECTS
247
- const inputObjects = dup ( corePropsNoType )
262
+ const inputObjects = dup ( corePropsNoType ) as ValidatorSchema
248
263
249
264
inputObjects . type = 'object'
250
265
inputObjects . required = [ 'name' , 'kind' , 'id' , 'href' , 'description' , 'inputFields' ]
@@ -258,16 +273,29 @@ inputObjects.properties.inputFields = {
258
273
}
259
274
260
275
// SCALARS
261
- const scalars = dup ( corePropsNoType )
276
+ const scalars = dup ( corePropsNoType ) as ValidatorSchema
262
277
263
278
scalars . type = 'object'
264
279
scalars . required = [ 'name' , 'id' , 'href' , 'description' ]
265
280
266
- function dup ( obj ) {
281
+ // Deep clone utility function with proper typing
282
+ function dup < T > ( obj : T ) : T {
267
283
return JSON . parse ( JSON . stringify ( obj ) )
268
284
}
269
285
270
- export const schemaValidator = {
286
+ // Schema validator collection with proper typing
287
+ interface SchemaValidators {
288
+ queries : ValidatorSchema
289
+ mutations : ValidatorSchema
290
+ objects : ValidatorSchema
291
+ interfaces : ValidatorSchema
292
+ enums : ValidatorSchema
293
+ unions : ValidatorSchema
294
+ inputObjects : ValidatorSchema
295
+ scalars : ValidatorSchema
296
+ }
297
+
298
+ export const schemaValidator : SchemaValidators = {
271
299
queries,
272
300
mutations,
273
301
objects,
0 commit comments