@@ -10,91 +10,104 @@ const fs = require('fs').promises;
10
10
const expect = chai . expect ;
11
11
12
12
const swaggerPath = path . join ( __dirname , 'pets-request.yaml' ) ;
13
+ let schema ;
13
14
14
15
describe ( 'oai3 - request tests' , function ( ) {
15
- let schema ;
16
16
before ( function ( ) {
17
17
schema = schemaValidatorGenerator . buildSchemaSync ( swaggerPath , { } ) ;
18
18
} ) ;
19
19
describe ( 'check headers' , function ( ) {
20
20
let schemaEndpoint ;
21
21
before ( function ( ) {
22
- schemaEndpoint = schema [ '/pet' ] [ ' post' ] ;
22
+ schemaEndpoint = schema [ '/pet' ] . post ;
23
23
} ) ;
24
24
it ( 'valid headers' , function ( ) {
25
25
// parameters match
26
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
27
- headers : { 'public-key' : '1.0'
28
- } ,
26
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
27
+ query : { } ,
28
+ headers : { 'public-key' : '1.0' } ,
29
29
path : { } ,
30
- files : undefined } ) ;
30
+ files : undefined
31
+ } ) ;
31
32
expect ( schemaEndpoint . parameters . errors ) . to . be . equal ( null ) ;
32
33
expect ( isParametersMatch ) . to . be . true ;
33
34
} ) ;
34
35
it ( 'missing required header' , function ( ) {
35
36
// parameters match
36
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
37
- headers : { 'host' : 'test' } ,
37
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
38
+ query : { } ,
39
+ headers : { host : 'test' } ,
38
40
path : { } ,
39
- files : undefined } ) ;
41
+ files : undefined
42
+ } ) ;
40
43
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( [ {
41
- ' dataPath' : '.headers' ,
42
- ' keyword' : 'required' ,
43
- ' message' : "should have required property 'public-key'" ,
44
- ' params' : {
45
- ' missingProperty' : 'public-key'
44
+ dataPath : '.headers' ,
45
+ keyword : 'required' ,
46
+ message : "should have required property 'public-key'" ,
47
+ params : {
48
+ missingProperty : 'public-key'
46
49
} ,
47
- ' schemaPath' : '#/properties/headers/required'
50
+ schemaPath : '#/properties/headers/required'
48
51
} ] ) ;
49
52
expect ( isParametersMatch ) . to . be . false ;
50
53
} ) ;
51
54
52
55
it ( 'invalid type for headers' , function ( ) {
53
56
// parameters match
54
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
57
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
58
+ query : { } ,
55
59
headers : 3 ,
56
60
path : { } ,
57
- files : undefined } ) ;
61
+ files : undefined
62
+ } ) ;
58
63
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( [ {
59
- ' dataPath' : '.headers' ,
60
- ' keyword' : 'type' ,
61
- ' message' : 'should be object' ,
62
- ' params' : {
63
- ' type' : 'object'
64
+ dataPath : '.headers' ,
65
+ keyword : 'type' ,
66
+ message : 'should be object' ,
67
+ params : {
68
+ type : 'object'
64
69
} ,
65
- ' schemaPath' : '#/properties/headers/type'
70
+ schemaPath : '#/properties/headers/type'
66
71
} ] ) ;
67
72
expect ( isParametersMatch ) . to . be . false ;
68
73
} ) ;
69
74
70
75
it ( 'invalid format for headers' , function ( ) {
71
76
// parameters match
72
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
73
- headers : { 'public-key' : '1.0' ,
74
- 'header_uuid' : '321' } ,
77
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
78
+ query : { } ,
79
+ headers : {
80
+ 'public-key' : '1.0' ,
81
+ header_uuid : '321'
82
+ } ,
75
83
path : { } ,
76
- files : undefined } ) ;
84
+ files : undefined
85
+ } ) ;
77
86
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( [
78
87
{
79
- ' dataPath' : '.headers.header_uuid' ,
80
- ' keyword' : 'format' ,
81
- ' message' : 'should match format "uuid"' ,
82
- ' params' : {
83
- ' format' : 'uuid'
88
+ dataPath : '.headers.header_uuid' ,
89
+ keyword : 'format' ,
90
+ message : 'should match format "uuid"' ,
91
+ params : {
92
+ format : 'uuid'
84
93
} ,
85
- ' schemaPath' : '#/properties/headers/properties/header_uuid/format'
94
+ schemaPath : '#/properties/headers/properties/header_uuid/format'
86
95
}
87
96
] ) ;
88
97
expect ( isParametersMatch ) . to . be . false ;
89
98
} ) ;
90
99
91
100
it ( 'valid format for headers' , function ( ) {
92
101
// parameters match
93
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
94
- headers : { 'public-key' : '1.0' ,
95
- 'header_uuid' : uuid ( ) } ,
102
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
103
+ query : { } ,
104
+ headers : {
105
+ 'public-key' : '1.0' ,
106
+ header_uuid : uuid ( )
107
+ } ,
96
108
path : { } ,
97
- files : undefined } ) ;
109
+ files : undefined
110
+ } ) ;
98
111
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( null ) ;
99
112
expect ( isParametersMatch ) . to . be . true ;
100
113
} ) ;
@@ -103,71 +116,75 @@ describe('oai3 - request tests', function () {
103
116
describe ( 'check queries' , function ( ) {
104
117
let schemaEndpoint ;
105
118
before ( function ( ) {
106
- schemaEndpoint = schema [ '/pets-query' ] [ ' get' ] ;
119
+ schemaEndpoint = schema [ '/pets-query' ] . get ;
107
120
} ) ;
108
121
it ( 'valid query' , function ( ) {
109
- let isParametersMatch = schemaEndpoint . parameters . validate ( {
122
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
110
123
query : { page : '1' } ,
111
124
headers : { } ,
112
125
path : { } ,
113
- files : undefined } ) ;
126
+ files : undefined
127
+ } ) ;
114
128
expect ( schemaEndpoint . parameters . errors ) . to . be . equal ( null ) ;
115
129
expect ( isParametersMatch ) . to . be . true ;
116
130
} ) ;
117
131
it ( 'missing required query' , function ( ) {
118
- let isParametersMatch = schemaEndpoint . parameters . validate ( {
132
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
119
133
query : { wrong_query : 'nothing' } ,
120
134
headers : { } ,
121
135
path : { } ,
122
- files : undefined } ) ;
136
+ files : undefined
137
+ } ) ;
123
138
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( [
124
139
{
125
- ' dataPath' : '.query' ,
126
- ' keyword' : 'additionalProperties' ,
127
- ' message' : 'should NOT have additional properties' ,
128
- ' params' : {
129
- ' additionalProperty' : 'wrong_query'
140
+ dataPath : '.query' ,
141
+ keyword : 'additionalProperties' ,
142
+ message : 'should NOT have additional properties' ,
143
+ params : {
144
+ additionalProperty : 'wrong_query'
130
145
} ,
131
- ' schemaPath' : '#/properties/query/additionalProperties'
146
+ schemaPath : '#/properties/query/additionalProperties'
132
147
} ,
133
148
{
134
- ' dataPath' : '.query' ,
135
- ' keyword' : 'required' ,
136
- ' message' : "should have required property 'page'" ,
137
- ' params' : {
138
- ' missingProperty' : 'page'
149
+ dataPath : '.query' ,
150
+ keyword : 'required' ,
151
+ message : "should have required property 'page'" ,
152
+ params : {
153
+ missingProperty : 'page'
139
154
} ,
140
- ' schemaPath' : '#/properties/query/required'
155
+ schemaPath : '#/properties/query/required'
141
156
}
142
157
] ) ;
143
158
expect ( isParametersMatch ) . to . be . false ;
144
159
} ) ;
145
160
146
161
it ( 'valid format query' , function ( ) {
147
- let isParametersMatch = schemaEndpoint . parameters . validate ( {
148
- query : { page : '1' , ' query_uuid' : uuid ( ) } ,
162
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
163
+ query : { page : '1' , query_uuid : uuid ( ) } ,
149
164
headers : { } ,
150
165
path : { } ,
151
- files : undefined } ) ;
166
+ files : undefined
167
+ } ) ;
152
168
expect ( schemaEndpoint . parameters . errors ) . to . be . equal ( null ) ;
153
169
expect ( isParametersMatch ) . to . be . true ;
154
170
} ) ;
155
171
156
172
it ( 'invalid format query' , function ( ) {
157
- let isParametersMatch = schemaEndpoint . parameters . validate ( {
158
- query : { page : '1' , ' query_uuid' : 321 } ,
173
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
174
+ query : { page : '1' , query_uuid : 321 } ,
159
175
headers : { } ,
160
176
path : { } ,
161
- files : undefined } ) ;
177
+ files : undefined
178
+ } ) ;
162
179
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( [
163
180
{
164
- ' dataPath' : '.query.query_uuid' ,
165
- ' keyword' : 'format' ,
166
- ' message' : 'should match format "uuid"' ,
167
- ' params' : {
168
- ' format' : 'uuid'
181
+ dataPath : '.query.query_uuid' ,
182
+ keyword : 'format' ,
183
+ message : 'should match format "uuid"' ,
184
+ params : {
185
+ format : 'uuid'
169
186
} ,
170
- ' schemaPath' : '#/properties/query/properties/query_uuid/format'
187
+ schemaPath : '#/properties/query/properties/query_uuid/format'
171
188
}
172
189
] ) ;
173
190
expect ( isParametersMatch ) . to . be . false ;
@@ -182,11 +199,12 @@ describe('oai3 - request tests', function () {
182
199
// Validate that the test case does not contain parameters set
183
200
expect ( jsonSchema . paths [ path ] [ method ] . parameters || [ ] ) . to . be . empty ;
184
201
185
- let isParametersMatch = endpointSchema . parameters . validate ( {
202
+ const isParametersMatch = endpointSchema . parameters . validate ( {
186
203
query : { page : '1' } ,
187
204
headers : { } ,
188
205
path : { } ,
189
- files : undefined } ) ;
206
+ files : undefined
207
+ } ) ;
190
208
expect ( endpointSchema . parameters . errors ) . to . be . eql ( [
191
209
{
192
210
dataPath : '.query' ,
@@ -204,74 +222,81 @@ describe('oai3 - request tests', function () {
204
222
205
223
describe ( 'check path' , function ( ) {
206
224
it ( 'valid path param' , function ( ) {
207
- let schemaEndpoint = schema [ '/pets-path/:name' ] [ ' get' ] ;
225
+ const schemaEndpoint = schema [ '/pets-path/:name' ] . get ;
208
226
// parameters match
209
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
210
- headers : { 'public-key' : '1.0'
211
- } ,
227
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
228
+ query : { } ,
229
+ headers : { 'public-key' : '1.0' } ,
212
230
path : { name : 'kitty' } ,
213
- files : undefined } ) ;
231
+ files : undefined
232
+ } ) ;
214
233
expect ( schemaEndpoint . parameters . errors ) . to . be . equal ( null ) ;
215
234
expect ( isParametersMatch ) . to . be . true ;
216
235
} ) ;
217
236
it ( 'missing required path' , function ( ) {
218
- let schemaEndpoint = schema [ '/pets-path/:name' ] [ ' get' ] ;
237
+ const schemaEndpoint = schema [ '/pets-path/:name' ] . get ;
219
238
220
239
// parameters match
221
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
222
- headers : { 'host' : 'test' } ,
240
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
241
+ query : { } ,
242
+ headers : { host : 'test' } ,
223
243
path : { namee : 'kitty' } ,
224
- files : undefined } ) ;
244
+ files : undefined
245
+ } ) ;
225
246
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( [
226
247
{
227
- ' dataPath' : '.path' ,
228
- ' keyword' : 'additionalProperties' ,
229
- ' message' : 'should NOT have additional properties' ,
230
- ' params' : {
231
- ' additionalProperty' : 'namee'
248
+ dataPath : '.path' ,
249
+ keyword : 'additionalProperties' ,
250
+ message : 'should NOT have additional properties' ,
251
+ params : {
252
+ additionalProperty : 'namee'
232
253
} ,
233
- ' schemaPath' : '#/properties/path/additionalProperties'
254
+ schemaPath : '#/properties/path/additionalProperties'
234
255
} ,
235
256
{
236
- ' dataPath' : '.path' ,
237
- ' keyword' : 'required' ,
238
- ' message' : "should have required property 'name'" ,
239
- ' params' : {
240
- ' missingProperty' : 'name'
257
+ dataPath : '.path' ,
258
+ keyword : 'required' ,
259
+ message : "should have required property 'name'" ,
260
+ params : {
261
+ missingProperty : 'name'
241
262
} ,
242
- ' schemaPath' : '#/properties/path/required'
263
+ schemaPath : '#/properties/path/required'
243
264
}
244
265
] ) ;
245
266
expect ( isParametersMatch ) . to . be . false ;
246
267
} ) ;
247
268
248
269
it ( 'valid path param format' , function ( ) {
249
- let schemaEndpoint = schema [ '/pets/:pet_id' ] [ ' get' ] ;
270
+ const schemaEndpoint = schema [ '/pets/:pet_id' ] . get ;
250
271
// parameters match
251
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
272
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
273
+ query : { } ,
252
274
headers : { } ,
253
275
path : { pet_id : uuid ( ) } ,
254
- files : undefined } ) ;
276
+ files : undefined
277
+ } ) ;
255
278
expect ( schemaEndpoint . parameters . errors ) . to . be . equal ( null ) ;
256
279
expect ( isParametersMatch ) . to . be . true ;
257
280
} ) ;
258
281
259
282
it ( 'invalid path param format' , function ( ) {
260
- let schemaEndpoint = schema [ '/pets/:pet_id' ] [ ' get' ] ;
283
+ const schemaEndpoint = schema [ '/pets/:pet_id' ] . get ;
261
284
// parameters match
262
- let isParametersMatch = schemaEndpoint . parameters . validate ( { query : { } ,
285
+ const isParametersMatch = schemaEndpoint . parameters . validate ( {
286
+ query : { } ,
263
287
headers : { } ,
264
288
path : { pet_id : 321 } ,
265
- files : undefined } ) ;
289
+ files : undefined
290
+ } ) ;
266
291
expect ( schemaEndpoint . parameters . errors ) . to . be . eql ( [
267
292
{
268
- ' dataPath' : '.path.pet_id' ,
269
- ' keyword' : 'format' ,
270
- ' message' : 'should match format "uuid"' ,
271
- ' params' : {
272
- ' format' : 'uuid'
293
+ dataPath : '.path.pet_id' ,
294
+ keyword : 'format' ,
295
+ message : 'should match format "uuid"' ,
296
+ params : {
297
+ format : 'uuid'
273
298
} ,
274
- ' schemaPath' : '#/properties/path/properties/pet_id/format'
299
+ schemaPath : '#/properties/path/properties/pet_id/format'
275
300
}
276
301
] ) ;
277
302
expect ( isParametersMatch ) . to . be . false ;
@@ -284,125 +309,127 @@ describe('oai3 - request tests', function () {
284
309
describe ( 'check body' , function ( ) {
285
310
describe ( 'simple body' , function ( ) {
286
311
it ( 'valid simple body' , function ( ) {
287
- let schemaEndpoint = schema [ '/dog' ] [ ' post' ] ;
288
- let isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( {
289
- ' bark' : 'hav hav'
312
+ const schemaEndpoint = schema [ '/dog' ] . post ;
313
+ const isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( {
314
+ bark : 'hav hav'
290
315
} ) ;
291
316
expect ( schemaEndpoint . body [ 'application/json' ] . errors ) . to . be . equal ( null ) ;
292
317
expect ( isBodysMatch ) . to . be . true ;
293
318
} ) ;
294
319
it ( 'missing required field in simple body' , function ( ) {
295
- let schemaEndpoint = schema [ '/dog' ] [ ' post' ] ;
320
+ const schemaEndpoint = schema [ '/dog' ] . post ;
296
321
297
- let isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( {
298
- ' fur' : 'hav hav'
322
+ const isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( {
323
+ fur : 'hav hav'
299
324
} ) ;
300
325
301
326
expect ( schemaEndpoint . body [ 'application/json' ] . errors ) . to . be . eql ( [
302
327
{
303
- ' dataPath' : '' ,
304
- ' keyword' : 'required' ,
305
- ' message' : "should have required property 'bark'" ,
306
- ' params' : {
307
- ' missingProperty' : 'bark'
328
+ dataPath : '' ,
329
+ keyword : 'required' ,
330
+ message : "should have required property 'bark'" ,
331
+ params : {
332
+ missingProperty : 'bark'
308
333
} ,
309
- ' schemaPath' : '#/required'
334
+ schemaPath : '#/required'
310
335
}
311
336
] ) ;
312
337
expect ( isBodysMatch ) . to . be . false ;
313
338
} ) ;
314
339
it ( 'invalid field type in simple body' , function ( ) {
315
- let schemaEndpoint = schema [ '/dog' ] [ ' post' ] ;
340
+ const schemaEndpoint = schema [ '/dog' ] . post ;
316
341
317
- let isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( {
318
- ' bark' : 111
342
+ const isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( {
343
+ bark : 111
319
344
} ) ;
320
345
321
346
expect ( schemaEndpoint . body [ 'application/json' ] . errors ) . to . be . eql ( [
322
347
{
323
- ' dataPath' : '.bark' ,
324
- ' keyword' : 'type' ,
325
- ' message' : 'should be string' ,
326
- ' params' : {
327
- ' type' : 'string'
348
+ dataPath : '.bark' ,
349
+ keyword : 'type' ,
350
+ message : 'should be string' ,
351
+ params : {
352
+ type : 'string'
328
353
} ,
329
- ' schemaPath' : '#/properties/bark/type'
354
+ schemaPath : '#/properties/bark/type'
330
355
}
331
356
] ) ;
332
357
expect ( isBodysMatch ) . to . be . false ;
333
358
} ) ;
334
359
it ( 'valid body - quantitive test' , function ( ) {
335
- let schemaEndpoint = schema [ '/many-body-fields' ] [ 'post' ] ;
336
-
337
- let isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( { 'fieldNum1' : 1 ,
338
- 'fieldNum2' : 2 ,
339
- 'fieldNum3' : 3 ,
340
- 'fieldStr1' : 'name1' ,
341
- 'fieldStr2' : 'name2' ,
342
- 'fieldStr3' : 'name3' } ) ;
360
+ const schemaEndpoint = schema [ '/many-body-fields' ] . post ;
361
+
362
+ const isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( {
363
+ fieldNum1 : 1 ,
364
+ fieldNum2 : 2 ,
365
+ fieldNum3 : 3 ,
366
+ fieldStr1 : 'name1' ,
367
+ fieldStr2 : 'name2' ,
368
+ fieldStr3 : 'name3'
369
+ } ) ;
343
370
344
371
expect ( schemaEndpoint . body [ 'application/json' ] . errors ) . to . be . eql ( null ) ;
345
372
expect ( isBodysMatch ) . to . be . true ;
346
373
} ) ;
347
374
it ( 'invalid body - quantitive test' , function ( ) {
348
- let schemaEndpoint = schema [ '/many-body-fields' ] [ ' post' ] ;
375
+ const schemaEndpoint = schema [ '/many-body-fields' ] . post ;
349
376
350
- let isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( { ' fieldNum1' : 'name1' , ' fieldNum2' : 'name2' , ' fieldNum3' : 'name3' , ' fieldStr1' : 1 , ' fieldStr2' : 2 , ' fieldStr3' : 3 } ) ;
377
+ const isBodysMatch = schemaEndpoint . body [ 'application/json' ] . validate ( { fieldNum1 : 'name1' , fieldNum2 : 'name2' , fieldNum3 : 'name3' , fieldStr1 : 1 , fieldStr2 : 2 , fieldStr3 : 3 } ) ;
351
378
352
379
expect ( schemaEndpoint . body [ 'application/json' ] . errors ) . to . be . eql ( [
353
380
{
354
- ' keyword' : 'type' ,
355
- ' dataPath' : '.fieldNum1' ,
356
- ' schemaPath' : '#/properties/fieldNum1/type' ,
357
- ' params' : {
358
- ' type' : 'number'
381
+ keyword : 'type' ,
382
+ dataPath : '.fieldNum1' ,
383
+ schemaPath : '#/properties/fieldNum1/type' ,
384
+ params : {
385
+ type : 'number'
359
386
} ,
360
- ' message' : 'should be number'
387
+ message : 'should be number'
361
388
} ,
362
389
{
363
- ' keyword' : 'type' ,
364
- ' dataPath' : '.fieldNum2' ,
365
- ' schemaPath' : '#/properties/fieldNum2/type' ,
366
- ' params' : {
367
- ' type' : 'number'
390
+ keyword : 'type' ,
391
+ dataPath : '.fieldNum2' ,
392
+ schemaPath : '#/properties/fieldNum2/type' ,
393
+ params : {
394
+ type : 'number'
368
395
} ,
369
- ' message' : 'should be number'
396
+ message : 'should be number'
370
397
} ,
371
398
{
372
- ' keyword' : 'type' ,
373
- ' dataPath' : '.fieldNum3' ,
374
- ' schemaPath' : '#/properties/fieldNum3/type' ,
375
- ' params' : {
376
- ' type' : 'number'
399
+ keyword : 'type' ,
400
+ dataPath : '.fieldNum3' ,
401
+ schemaPath : '#/properties/fieldNum3/type' ,
402
+ params : {
403
+ type : 'number'
377
404
} ,
378
- ' message' : 'should be number'
405
+ message : 'should be number'
379
406
} ,
380
407
{
381
- ' keyword' : 'type' ,
382
- ' dataPath' : '.fieldStr1' ,
383
- ' schemaPath' : '#/properties/fieldStr1/type' ,
384
- ' params' : {
385
- ' type' : 'string'
408
+ keyword : 'type' ,
409
+ dataPath : '.fieldStr1' ,
410
+ schemaPath : '#/properties/fieldStr1/type' ,
411
+ params : {
412
+ type : 'string'
386
413
} ,
387
- ' message' : 'should be string'
414
+ message : 'should be string'
388
415
} ,
389
416
{
390
- ' keyword' : 'type' ,
391
- ' dataPath' : '.fieldStr2' ,
392
- ' schemaPath' : '#/properties/fieldStr2/type' ,
393
- ' params' : {
394
- ' type' : 'string'
417
+ keyword : 'type' ,
418
+ dataPath : '.fieldStr2' ,
419
+ schemaPath : '#/properties/fieldStr2/type' ,
420
+ params : {
421
+ type : 'string'
395
422
} ,
396
- ' message' : 'should be string'
423
+ message : 'should be string'
397
424
} ,
398
425
{
399
- ' keyword' : 'type' ,
400
- ' dataPath' : '.fieldStr3' ,
401
- ' schemaPath' : '#/properties/fieldStr3/type' ,
402
- ' params' : {
403
- ' type' : 'string'
426
+ keyword : 'type' ,
427
+ dataPath : '.fieldStr3' ,
428
+ schemaPath : '#/properties/fieldStr3/type' ,
429
+ params : {
430
+ type : 'string'
404
431
} ,
405
- ' message' : 'should be string'
432
+ message : 'should be string'
406
433
}
407
434
] ) ;
408
435
expect ( isBodysMatch ) . to . be . false ;
@@ -411,79 +438,79 @@ describe('oai3 - request tests', function () {
411
438
describe ( 'anyOf body' , function ( ) {
412
439
let schemaEndpoint ;
413
440
before ( function ( ) {
414
- schemaEndpoint = schema [ '/pet-any-of' ] [ ' post' ] . body [ 'application/json' ] ;
441
+ schemaEndpoint = schema [ '/pet-any-of' ] . post . body [ 'application/json' ] ;
415
442
} ) ;
416
443
it ( 'valid full body' , function ( ) {
417
- let isMatch = schemaEndpoint . validate (
444
+ const isMatch = schemaEndpoint . validate (
418
445
{
419
- ' bark' : 'hav hav' ,
420
- ' fur' : 1
446
+ bark : 'hav hav' ,
447
+ fur : 1
421
448
} ) ;
422
449
expect ( schemaEndpoint . errors ) . to . be . equal ( null ) ;
423
450
expect ( isMatch ) . to . be . true ;
424
451
} ) ;
425
452
426
453
it ( 'missing required field in body' , function ( ) {
427
- let isMatch = schemaEndpoint . validate ( { } ) ;
454
+ const isMatch = schemaEndpoint . validate ( { } ) ;
428
455
expect ( schemaEndpoint . errors ) . to . be . eql ( [
429
456
{
430
- ' dataPath' : '' ,
431
- ' keyword' : 'required' ,
432
- ' message' : "should have required property 'bark'" ,
433
- ' params' : {
434
- ' missingProperty' : 'bark'
457
+ dataPath : '' ,
458
+ keyword : 'required' ,
459
+ message : "should have required property 'bark'" ,
460
+ params : {
461
+ missingProperty : 'bark'
435
462
} ,
436
- ' schemaPath' : '#/anyOf/0/required'
463
+ schemaPath : '#/anyOf/0/required'
437
464
} ,
438
465
{
439
- ' dataPath' : '' ,
440
- ' keyword' : 'required' ,
441
- ' message' : "should have required property 'fur'" ,
442
- ' params' : {
443
- ' missingProperty' : 'fur'
466
+ dataPath : '' ,
467
+ keyword : 'required' ,
468
+ message : "should have required property 'fur'" ,
469
+ params : {
470
+ missingProperty : 'fur'
444
471
} ,
445
- ' schemaPath' : '#/anyOf/1/required'
472
+ schemaPath : '#/anyOf/1/required'
446
473
} ,
447
474
{
448
- ' dataPath' : '' ,
449
- ' keyword' : 'anyOf' ,
450
- ' message' : 'should match some schema in anyOf' ,
451
- ' params' : { } ,
452
- ' schemaPath' : '#/anyOf'
475
+ dataPath : '' ,
476
+ keyword : 'anyOf' ,
477
+ message : 'should match some schema in anyOf' ,
478
+ params : { } ,
479
+ schemaPath : '#/anyOf'
453
480
}
454
481
] ) ;
455
482
expect ( isMatch ) . to . be . false ;
456
483
} ) ;
457
484
it ( 'invalid field type in body' , function ( ) {
458
- let isMatch = schemaEndpoint . validate ( {
459
- ' bark' : 111 ,
460
- ' fur' : 'wrong'
485
+ const isMatch = schemaEndpoint . validate ( {
486
+ bark : 111 ,
487
+ fur : 'wrong'
461
488
} ) ;
462
489
expect ( schemaEndpoint . errors ) . to . be . eql ( [
463
490
{
464
- ' dataPath' : '.bark' ,
465
- ' keyword' : 'type' ,
466
- ' message' : 'should be string' ,
467
- ' params' : {
468
- ' type' : 'string'
491
+ dataPath : '.bark' ,
492
+ keyword : 'type' ,
493
+ message : 'should be string' ,
494
+ params : {
495
+ type : 'string'
469
496
} ,
470
- ' schemaPath' : '#/anyOf/0/properties/bark/type'
497
+ schemaPath : '#/anyOf/0/properties/bark/type'
471
498
} ,
472
499
{
473
- ' dataPath' : '.fur' ,
474
- ' keyword' : 'pattern' ,
475
- ' message' : 'should match pattern "^\\d+$"' ,
476
- ' params' : {
477
- ' pattern' : '^\\d+$'
500
+ dataPath : '.fur' ,
501
+ keyword : 'pattern' ,
502
+ message : 'should match pattern "^\\d+$"' ,
503
+ params : {
504
+ pattern : '^\\d+$'
478
505
} ,
479
- ' schemaPath' : '#/anyOf/1/properties/fur/pattern'
506
+ schemaPath : '#/anyOf/1/properties/fur/pattern'
480
507
} ,
481
508
{
482
- ' dataPath' : '' ,
483
- ' keyword' : 'anyOf' ,
484
- ' message' : 'should match some schema in anyOf' ,
485
- ' params' : { } ,
486
- ' schemaPath' : '#/anyOf'
509
+ dataPath : '' ,
510
+ keyword : 'anyOf' ,
511
+ message : 'should match some schema in anyOf' ,
512
+ params : { } ,
513
+ schemaPath : '#/anyOf'
487
514
}
488
515
] ) ;
489
516
expect ( isMatch ) . to . be . false ;
@@ -493,56 +520,56 @@ describe('oai3 - request tests', function () {
493
520
let schemaEndpoint ;
494
521
495
522
before ( function ( ) {
496
- schemaEndpoint = schema [ '/pet-all-of' ] [ ' post' ] . body [ 'application/json' ] ;
523
+ schemaEndpoint = schema [ '/pet-all-of' ] . post . body [ 'application/json' ] ;
497
524
} ) ;
498
525
it ( 'valid full body' , function ( ) {
499
- let isMatch = schemaEndpoint . validate ( {
500
- ' bark' : 'hav hav' ,
501
- ' fur' : '11'
526
+ const isMatch = schemaEndpoint . validate ( {
527
+ bark : 'hav hav' ,
528
+ fur : '11'
502
529
} ) ;
503
530
expect ( schemaEndpoint . errors ) . to . be . equal ( null ) ;
504
531
expect ( isMatch ) . to . be . true ;
505
532
} ) ;
506
533
it ( 'missing required field in body' , function ( ) {
507
- let isMatch = schemaEndpoint . validate ( {
508
- ' bark' : 'hav hav'
534
+ const isMatch = schemaEndpoint . validate ( {
535
+ bark : 'hav hav'
509
536
} ) ;
510
537
expect ( schemaEndpoint . errors ) . to . be . eql ( [
511
538
{
512
- ' dataPath' : '' ,
513
- ' keyword' : 'required' ,
514
- ' message' : "should have required property 'fur'" ,
515
- ' params' : {
516
- ' missingProperty' : 'fur'
539
+ dataPath : '' ,
540
+ keyword : 'required' ,
541
+ message : "should have required property 'fur'" ,
542
+ params : {
543
+ missingProperty : 'fur'
517
544
} ,
518
- ' schemaPath' : '#/allOf/1/required'
545
+ schemaPath : '#/allOf/1/required'
519
546
}
520
547
] ) ;
521
548
expect ( isMatch ) . to . be . false ;
522
549
} ) ;
523
550
it ( 'invalid field type in body' , function ( ) {
524
- let isMatch = schemaEndpoint . validate ( {
525
- ' bark' : 111 ,
526
- ' fur' : 'wrong'
551
+ const isMatch = schemaEndpoint . validate ( {
552
+ bark : 111 ,
553
+ fur : 'wrong'
527
554
} ) ;
528
555
expect ( schemaEndpoint . errors ) . to . be . eql ( [
529
556
{
530
- ' dataPath' : '.bark' ,
531
- ' keyword' : 'type' ,
532
- ' message' : 'should be string' ,
533
- ' params' : {
534
- ' type' : 'string'
557
+ dataPath : '.bark' ,
558
+ keyword : 'type' ,
559
+ message : 'should be string' ,
560
+ params : {
561
+ type : 'string'
535
562
} ,
536
- ' schemaPath' : '#/allOf/0/properties/bark/type'
563
+ schemaPath : '#/allOf/0/properties/bark/type'
537
564
} ,
538
565
{
539
- ' dataPath' : '.fur' ,
540
- ' keyword' : 'pattern' ,
541
- ' message' : 'should match pattern "^\\d+$"' ,
542
- ' params' : {
543
- ' pattern' : '^\\d+$'
566
+ dataPath : '.fur' ,
567
+ keyword : 'pattern' ,
568
+ message : 'should match pattern "^\\d+$"' ,
569
+ params : {
570
+ pattern : '^\\d+$'
544
571
} ,
545
- ' schemaPath' : '#/allOf/1/properties/fur/pattern'
572
+ schemaPath : '#/allOf/1/properties/fur/pattern'
546
573
}
547
574
] ) ;
548
575
expect ( isMatch ) . to . be . false ;
@@ -552,12 +579,12 @@ describe('oai3 - request tests', function () {
552
579
describe ( 'discriminator-pet' , function ( ) {
553
580
let schemaEndpoint ;
554
581
before ( function ( ) {
555
- schemaEndpoint = schema [ '/pet-discriminator' ] [ ' post' ] . body [ 'application/json' ] ;
582
+ schemaEndpoint = schema [ '/pet-discriminator' ] . post . body [ 'application/json' ] ;
556
583
} ) ;
557
584
it ( 'missing discriminator field' , function ( ) {
558
585
// body match
559
- let isBodysMatch = schemaEndpoint . validate ( {
560
- ' bark' : 'hav hav'
586
+ const isBodysMatch = schemaEndpoint . validate ( {
587
+ bark : 'hav hav'
561
588
} ) ;
562
589
563
590
expect ( schemaEndpoint . errors [ 0 ] . message ) . to . equal ( 'should be equal to one of the allowed values' ) ;
@@ -571,25 +598,25 @@ describe('oai3 - request tests', function () {
571
598
} ) ;
572
599
it ( 'when discriminator type is dog and missing field' , function ( ) {
573
600
// body match
574
- let isBodysMatch = schemaEndpoint . validate ( {
575
- ' type' : 'dog_object'
601
+ const isBodysMatch = schemaEndpoint . validate ( {
602
+ type : 'dog_object'
576
603
} ) ;
577
604
expect ( schemaEndpoint . errors ) . to . be . eql ( [
578
605
{
579
- ' dataPath' : '' ,
580
- ' keyword' : 'required' ,
581
- ' message' : "should have required property 'bark'" ,
582
- ' params' : {
583
- ' missingProperty' : 'bark'
606
+ dataPath : '' ,
607
+ keyword : 'required' ,
608
+ message : "should have required property 'bark'" ,
609
+ params : {
610
+ missingProperty : 'bark'
584
611
} ,
585
- ' schemaPath' : '#/required'
612
+ schemaPath : '#/required'
586
613
}
587
614
] ) ;
588
615
expect ( isBodysMatch ) . to . be . false ;
589
616
} ) ;
590
617
it ( 'valid complex body' , function ( ) {
591
618
// body match
592
- let isBodysMatch = schemaEndpoint . validate ( {
619
+ const isBodysMatch = schemaEndpoint . validate ( {
593
620
bark : 'hav hav' ,
594
621
type : 'dog_object'
595
622
} ) ;
@@ -600,12 +627,12 @@ describe('oai3 - request tests', function () {
600
627
describe ( 'discriminator-multiple pet' , function ( ) {
601
628
let schemaEndpoint ;
602
629
before ( function ( ) {
603
- schemaEndpoint = schema [ '/pet-discriminator-multiple' ] [ ' post' ] . body [ 'application/json' ] ;
630
+ schemaEndpoint = schema [ '/pet-discriminator-multiple' ] . post . body [ 'application/json' ] ;
604
631
} ) ;
605
632
it ( 'missing discriminator field' , function ( ) {
606
633
// body match
607
- let isBodysMatch = schemaEndpoint . validate ( {
608
- ' fur' : 'hav hav'
634
+ const isBodysMatch = schemaEndpoint . validate ( {
635
+ fur : 'hav hav'
609
636
} ) ;
610
637
611
638
expect ( schemaEndpoint . errors [ 0 ] . message ) . to . equal ( 'should be equal to one of the allowed values' ) ;
@@ -620,7 +647,7 @@ describe('oai3 - request tests', function () {
620
647
} ) ;
621
648
it ( 'missing discriminator field on the on inside discriminator' , function ( ) {
622
649
// body match
623
- let isBodysMatch = schemaEndpoint . validate ( {
650
+ const isBodysMatch = schemaEndpoint . validate ( {
624
651
bark : 'hav hav' ,
625
652
type : 'dog_multiple'
626
653
} ) ;
@@ -636,7 +663,7 @@ describe('oai3 - request tests', function () {
636
663
} ) ;
637
664
it ( 'when discriminator type is dog_multiple and model small_dog and missing root field name and specific plane field' , function ( ) {
638
665
// body match
639
- let isBodysMatch = schemaEndpoint . validate ( {
666
+ const isBodysMatch = schemaEndpoint . validate ( {
640
667
type : 'dog_multiple' ,
641
668
model : 'small_dog'
642
669
} ) ;
@@ -648,7 +675,7 @@ describe('oai3 - request tests', function () {
648
675
} ) ;
649
676
it ( 'when valid discriminator type is dog_multiple and model small_dog' , function ( ) {
650
677
// body match
651
- let isBodysMatch = schemaEndpoint . validate ( {
678
+ const isBodysMatch = schemaEndpoint . validate ( {
652
679
name : 'sesna' ,
653
680
max_length : 'max_length' ,
654
681
dog_age : '3' ,
@@ -662,11 +689,11 @@ describe('oai3 - request tests', function () {
662
689
describe ( 'discriminator-mapping pet' , function ( ) {
663
690
let schemaEndpoint ;
664
691
before ( function ( ) {
665
- schemaEndpoint = schema [ '/pet-discriminator-mapping' ] [ ' post' ] . body [ 'application/json' ] ;
692
+ schemaEndpoint = schema [ '/pet-discriminator-mapping' ] . post . body [ 'application/json' ] ;
666
693
} ) ;
667
694
it ( 'missing discriminator field on the root' , function ( ) {
668
695
// body match
669
- let isBodysMatch = schemaEndpoint . validate ( {
696
+ const isBodysMatch = schemaEndpoint . validate ( {
670
697
fur : '6'
671
698
} ) ;
672
699
@@ -681,7 +708,7 @@ describe('oai3 - request tests', function () {
681
708
} ) ;
682
709
it ( 'when discriminator type is mapped_dog and model small_dog and missing root field name and specific dog field' , function ( ) {
683
710
// body match
684
- let isBodysMatch = schemaEndpoint . validate ( {
711
+ const isBodysMatch = schemaEndpoint . validate ( {
685
712
type : 'mapped_dog' ,
686
713
model : 'small_dog'
687
714
} ) ;
@@ -693,7 +720,7 @@ describe('oai3 - request tests', function () {
693
720
} ) ;
694
721
it ( 'when valid discriminator type is mapped_dog and model small_dog' , function ( ) {
695
722
// body match
696
- let isBodysMatch = schemaEndpoint . validate ( {
723
+ const isBodysMatch = schemaEndpoint . validate ( {
697
724
name : 'sesna' ,
698
725
max_length : 'max_length' ,
699
726
dog_age : '200' ,
@@ -708,3 +735,27 @@ describe('oai3 - request tests', function () {
708
735
} ) ;
709
736
} ) ;
710
737
} ) ;
738
+
739
+ describe ( 'oai3 - request tests with options' , function ( ) {
740
+ const options = { contentTypeValidation : true } ;
741
+ before ( function ( ) {
742
+ schema = schemaValidatorGenerator . buildSchemaSync ( swaggerPath , options ) ;
743
+ } ) ;
744
+ it ( 'bad request - wrong content-type (should be application/json)' , function ( ) {
745
+ const schemaEndpoint = schema [ '/pet' ] . post ;
746
+ schemaEndpoint . parameters . validate ( {
747
+ query : { } ,
748
+ headers : {
749
+ 'public-key' : '1.0' ,
750
+ 'content-length' : 1 ,
751
+ 'content-type' : 'application/x-www-form-urlencoded'
752
+ } ,
753
+ path : { }
754
+ } ) ;
755
+
756
+ const { errors } = schemaEndpoint . parameters . errors [ 0 ] ;
757
+ expect ( errors . message ) . to . eql ( 'content-type must be one of application/json' ) ;
758
+ expect ( errors . params [ 'content-type' ] ) . to . eql ( 'application/x-www-form-urlencoded' ) ;
759
+ expect ( errors . params . types ) . to . eql ( [ 'application/json' ] ) ;
760
+ } ) ;
761
+ } ) ;
0 commit comments