Skip to content

Commit 9f5fa58

Browse files
authoredNov 16, 2021
fix: add support for openapi 3.0 content type validation (#97)
* tests: add test for openapi 3.0 content type validation * fix: support content type validation in openapi 3.0
1 parent 322df4b commit 9f5fa58

File tree

2 files changed

+323
-261
lines changed

2 files changed

+323
-261
lines changed
 

‎src/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ function getOptions(opts = {}) {
9494
);
9595
}
9696

97+
function getContentTypes(isOpenApi3, dereferenced, currentPath, currentMethod) {
98+
let contentType;
99+
if (isOpenApi3) {
100+
const requestBody = dereferenced.paths[currentPath][currentMethod].requestBody;
101+
contentType = requestBody && Object.keys(requestBody.content);
102+
} else {
103+
contentType = dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes;
104+
}
105+
return contentType;
106+
}
107+
97108
function buildRequestValidator(referenced, dereferenced, currentPath, currentMethod, options) {
98109
const requestSchema = {};
99110
let localParameters = [];
@@ -122,8 +133,8 @@ function buildRequestValidator(referenced, dereferenced, currentPath, currentMet
122133
localParameters = oai2.buildPathParameters(parameters, pathParameters);
123134
}
124135

125-
requestSchema.parameters = buildParametersValidation(localParameters,
126-
dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes, options);
136+
const contentTypes = getContentTypes(isOpenApi3, dereferenced, currentPath, currentMethod);
137+
requestSchema.parameters = buildParametersValidation(localParameters, contentTypes, options);
127138

128139
return requestSchema;
129140
}

‎test/openapi3/request/request-oai3-test.js

+310-259
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,104 @@ const fs = require('fs').promises;
1010
const expect = chai.expect;
1111

1212
const swaggerPath = path.join(__dirname, 'pets-request.yaml');
13+
let schema;
1314

1415
describe('oai3 - request tests', function () {
15-
let schema;
1616
before(function () {
1717
schema = schemaValidatorGenerator.buildSchemaSync(swaggerPath, {});
1818
});
1919
describe('check headers', function () {
2020
let schemaEndpoint;
2121
before(function() {
22-
schemaEndpoint = schema['/pet']['post'];
22+
schemaEndpoint = schema['/pet'].post;
2323
});
2424
it('valid headers', function () {
2525
// 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' },
2929
path: {},
30-
files: undefined });
30+
files: undefined
31+
});
3132
expect(schemaEndpoint.parameters.errors).to.be.equal(null);
3233
expect(isParametersMatch).to.be.true;
3334
});
3435
it('missing required header', function () {
3536
// 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' },
3840
path: {},
39-
files: undefined });
41+
files: undefined
42+
});
4043
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'
4649
},
47-
'schemaPath': '#/properties/headers/required'
50+
schemaPath: '#/properties/headers/required'
4851
}]);
4952
expect(isParametersMatch).to.be.false;
5053
});
5154

5255
it('invalid type for headers', function () {
5356
// parameters match
54-
let isParametersMatch = schemaEndpoint.parameters.validate({ query: {},
57+
const isParametersMatch = schemaEndpoint.parameters.validate({
58+
query: {},
5559
headers: 3,
5660
path: {},
57-
files: undefined });
61+
files: undefined
62+
});
5863
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'
6469
},
65-
'schemaPath': '#/properties/headers/type'
70+
schemaPath: '#/properties/headers/type'
6671
}]);
6772
expect(isParametersMatch).to.be.false;
6873
});
6974

7075
it('invalid format for headers', function () {
7176
// 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+
},
7583
path: {},
76-
files: undefined });
84+
files: undefined
85+
});
7786
expect(schemaEndpoint.parameters.errors).to.be.eql([
7887
{
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'
8493
},
85-
'schemaPath': '#/properties/headers/properties/header_uuid/format'
94+
schemaPath: '#/properties/headers/properties/header_uuid/format'
8695
}
8796
]);
8897
expect(isParametersMatch).to.be.false;
8998
});
9099

91100
it('valid format for headers', function () {
92101
// 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+
},
96108
path: {},
97-
files: undefined });
109+
files: undefined
110+
});
98111
expect(schemaEndpoint.parameters.errors).to.be.eql(null);
99112
expect(isParametersMatch).to.be.true;
100113
});
@@ -103,71 +116,75 @@ describe('oai3 - request tests', function () {
103116
describe('check queries', function () {
104117
let schemaEndpoint;
105118
before(function () {
106-
schemaEndpoint = schema['/pets-query']['get'];
119+
schemaEndpoint = schema['/pets-query'].get;
107120
});
108121
it('valid query', function () {
109-
let isParametersMatch = schemaEndpoint.parameters.validate({
122+
const isParametersMatch = schemaEndpoint.parameters.validate({
110123
query: { page: '1' },
111124
headers: {},
112125
path: {},
113-
files: undefined });
126+
files: undefined
127+
});
114128
expect(schemaEndpoint.parameters.errors).to.be.equal(null);
115129
expect(isParametersMatch).to.be.true;
116130
});
117131
it('missing required query', function () {
118-
let isParametersMatch = schemaEndpoint.parameters.validate({
132+
const isParametersMatch = schemaEndpoint.parameters.validate({
119133
query: { wrong_query: 'nothing' },
120134
headers: {},
121135
path: {},
122-
files: undefined });
136+
files: undefined
137+
});
123138
expect(schemaEndpoint.parameters.errors).to.be.eql([
124139
{
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'
130145
},
131-
'schemaPath': '#/properties/query/additionalProperties'
146+
schemaPath: '#/properties/query/additionalProperties'
132147
},
133148
{
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'
139154
},
140-
'schemaPath': '#/properties/query/required'
155+
schemaPath: '#/properties/query/required'
141156
}
142157
]);
143158
expect(isParametersMatch).to.be.false;
144159
});
145160

146161
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() },
149164
headers: {},
150165
path: {},
151-
files: undefined });
166+
files: undefined
167+
});
152168
expect(schemaEndpoint.parameters.errors).to.be.equal(null);
153169
expect(isParametersMatch).to.be.true;
154170
});
155171

156172
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 },
159175
headers: {},
160176
path: {},
161-
files: undefined });
177+
files: undefined
178+
});
162179
expect(schemaEndpoint.parameters.errors).to.be.eql([
163180
{
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'
169186
},
170-
'schemaPath': '#/properties/query/properties/query_uuid/format'
187+
schemaPath: '#/properties/query/properties/query_uuid/format'
171188
}
172189
]);
173190
expect(isParametersMatch).to.be.false;
@@ -182,11 +199,12 @@ describe('oai3 - request tests', function () {
182199
// Validate that the test case does not contain parameters set
183200
expect(jsonSchema.paths[path][method].parameters || []).to.be.empty;
184201

185-
let isParametersMatch = endpointSchema.parameters.validate({
202+
const isParametersMatch = endpointSchema.parameters.validate({
186203
query: { page: '1' },
187204
headers: {},
188205
path: {},
189-
files: undefined });
206+
files: undefined
207+
});
190208
expect(endpointSchema.parameters.errors).to.be.eql([
191209
{
192210
dataPath: '.query',
@@ -204,74 +222,81 @@ describe('oai3 - request tests', function () {
204222

205223
describe('check path', function () {
206224
it('valid path param', function () {
207-
let schemaEndpoint = schema['/pets-path/:name']['get'];
225+
const schemaEndpoint = schema['/pets-path/:name'].get;
208226
// 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' },
212230
path: { name: 'kitty' },
213-
files: undefined });
231+
files: undefined
232+
});
214233
expect(schemaEndpoint.parameters.errors).to.be.equal(null);
215234
expect(isParametersMatch).to.be.true;
216235
});
217236
it('missing required path', function () {
218-
let schemaEndpoint = schema['/pets-path/:name']['get'];
237+
const schemaEndpoint = schema['/pets-path/:name'].get;
219238

220239
// 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' },
223243
path: { namee: 'kitty' },
224-
files: undefined });
244+
files: undefined
245+
});
225246
expect(schemaEndpoint.parameters.errors).to.be.eql([
226247
{
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'
232253
},
233-
'schemaPath': '#/properties/path/additionalProperties'
254+
schemaPath: '#/properties/path/additionalProperties'
234255
},
235256
{
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'
241262
},
242-
'schemaPath': '#/properties/path/required'
263+
schemaPath: '#/properties/path/required'
243264
}
244265
]);
245266
expect(isParametersMatch).to.be.false;
246267
});
247268

248269
it('valid path param format', function () {
249-
let schemaEndpoint = schema['/pets/:pet_id']['get'];
270+
const schemaEndpoint = schema['/pets/:pet_id'].get;
250271
// parameters match
251-
let isParametersMatch = schemaEndpoint.parameters.validate({ query: {},
272+
const isParametersMatch = schemaEndpoint.parameters.validate({
273+
query: {},
252274
headers: { },
253275
path: { pet_id: uuid() },
254-
files: undefined });
276+
files: undefined
277+
});
255278
expect(schemaEndpoint.parameters.errors).to.be.equal(null);
256279
expect(isParametersMatch).to.be.true;
257280
});
258281

259282
it('invalid path param format', function () {
260-
let schemaEndpoint = schema['/pets/:pet_id']['get'];
283+
const schemaEndpoint = schema['/pets/:pet_id'].get;
261284
// parameters match
262-
let isParametersMatch = schemaEndpoint.parameters.validate({ query: {},
285+
const isParametersMatch = schemaEndpoint.parameters.validate({
286+
query: {},
263287
headers: { },
264288
path: { pet_id: 321 },
265-
files: undefined });
289+
files: undefined
290+
});
266291
expect(schemaEndpoint.parameters.errors).to.be.eql([
267292
{
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'
273298
},
274-
'schemaPath': '#/properties/path/properties/pet_id/format'
299+
schemaPath: '#/properties/path/properties/pet_id/format'
275300
}
276301
]);
277302
expect(isParametersMatch).to.be.false;
@@ -284,125 +309,127 @@ describe('oai3 - request tests', function () {
284309
describe('check body', function () {
285310
describe('simple body', function () {
286311
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'
290315
});
291316
expect(schemaEndpoint.body['application/json'].errors).to.be.equal(null);
292317
expect(isBodysMatch).to.be.true;
293318
});
294319
it('missing required field in simple body', function () {
295-
let schemaEndpoint = schema['/dog']['post'];
320+
const schemaEndpoint = schema['/dog'].post;
296321

297-
let isBodysMatch = schemaEndpoint.body['application/json'].validate({
298-
'fur': 'hav hav'
322+
const isBodysMatch = schemaEndpoint.body['application/json'].validate({
323+
fur: 'hav hav'
299324
});
300325

301326
expect(schemaEndpoint.body['application/json'].errors).to.be.eql([
302327
{
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'
308333
},
309-
'schemaPath': '#/required'
334+
schemaPath: '#/required'
310335
}
311336
]);
312337
expect(isBodysMatch).to.be.false;
313338
});
314339
it('invalid field type in simple body', function () {
315-
let schemaEndpoint = schema['/dog']['post'];
340+
const schemaEndpoint = schema['/dog'].post;
316341

317-
let isBodysMatch = schemaEndpoint.body['application/json'].validate({
318-
'bark': 111
342+
const isBodysMatch = schemaEndpoint.body['application/json'].validate({
343+
bark: 111
319344
});
320345

321346
expect(schemaEndpoint.body['application/json'].errors).to.be.eql([
322347
{
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'
328353
},
329-
'schemaPath': '#/properties/bark/type'
354+
schemaPath: '#/properties/bark/type'
330355
}
331356
]);
332357
expect(isBodysMatch).to.be.false;
333358
});
334359
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+
});
343370

344371
expect(schemaEndpoint.body['application/json'].errors).to.be.eql(null);
345372
expect(isBodysMatch).to.be.true;
346373
});
347374
it('invalid body - quantitive test', function () {
348-
let schemaEndpoint = schema['/many-body-fields']['post'];
375+
const schemaEndpoint = schema['/many-body-fields'].post;
349376

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 });
351378

352379
expect(schemaEndpoint.body['application/json'].errors).to.be.eql([
353380
{
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'
359386
},
360-
'message': 'should be number'
387+
message: 'should be number'
361388
},
362389
{
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'
368395
},
369-
'message': 'should be number'
396+
message: 'should be number'
370397
},
371398
{
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'
377404
},
378-
'message': 'should be number'
405+
message: 'should be number'
379406
},
380407
{
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'
386413
},
387-
'message': 'should be string'
414+
message: 'should be string'
388415
},
389416
{
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'
395422
},
396-
'message': 'should be string'
423+
message: 'should be string'
397424
},
398425
{
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'
404431
},
405-
'message': 'should be string'
432+
message: 'should be string'
406433
}
407434
]);
408435
expect(isBodysMatch).to.be.false;
@@ -411,79 +438,79 @@ describe('oai3 - request tests', function () {
411438
describe('anyOf body', function () {
412439
let schemaEndpoint;
413440
before(function () {
414-
schemaEndpoint = schema['/pet-any-of']['post'].body['application/json'];
441+
schemaEndpoint = schema['/pet-any-of'].post.body['application/json'];
415442
});
416443
it('valid full body', function () {
417-
let isMatch = schemaEndpoint.validate(
444+
const isMatch = schemaEndpoint.validate(
418445
{
419-
'bark': 'hav hav',
420-
'fur': 1
446+
bark: 'hav hav',
447+
fur: 1
421448
});
422449
expect(schemaEndpoint.errors).to.be.equal(null);
423450
expect(isMatch).to.be.true;
424451
});
425452

426453
it('missing required field in body', function () {
427-
let isMatch = schemaEndpoint.validate({ });
454+
const isMatch = schemaEndpoint.validate({ });
428455
expect(schemaEndpoint.errors).to.be.eql([
429456
{
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'
435462
},
436-
'schemaPath': '#/anyOf/0/required'
463+
schemaPath: '#/anyOf/0/required'
437464
},
438465
{
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'
444471
},
445-
'schemaPath': '#/anyOf/1/required'
472+
schemaPath: '#/anyOf/1/required'
446473
},
447474
{
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'
453480
}
454481
]);
455482
expect(isMatch).to.be.false;
456483
});
457484
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'
461488
});
462489
expect(schemaEndpoint.errors).to.be.eql([
463490
{
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'
469496
},
470-
'schemaPath': '#/anyOf/0/properties/bark/type'
497+
schemaPath: '#/anyOf/0/properties/bark/type'
471498
},
472499
{
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+$'
478505
},
479-
'schemaPath': '#/anyOf/1/properties/fur/pattern'
506+
schemaPath: '#/anyOf/1/properties/fur/pattern'
480507
},
481508
{
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'
487514
}
488515
]);
489516
expect(isMatch).to.be.false;
@@ -493,56 +520,56 @@ describe('oai3 - request tests', function () {
493520
let schemaEndpoint;
494521

495522
before(function () {
496-
schemaEndpoint = schema['/pet-all-of']['post'].body['application/json'];
523+
schemaEndpoint = schema['/pet-all-of'].post.body['application/json'];
497524
});
498525
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'
502529
});
503530
expect(schemaEndpoint.errors).to.be.equal(null);
504531
expect(isMatch).to.be.true;
505532
});
506533
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'
509536
});
510537
expect(schemaEndpoint.errors).to.be.eql([
511538
{
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'
517544
},
518-
'schemaPath': '#/allOf/1/required'
545+
schemaPath: '#/allOf/1/required'
519546
}
520547
]);
521548
expect(isMatch).to.be.false;
522549
});
523550
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'
527554
});
528555
expect(schemaEndpoint.errors).to.be.eql([
529556
{
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'
535562
},
536-
'schemaPath': '#/allOf/0/properties/bark/type'
563+
schemaPath: '#/allOf/0/properties/bark/type'
537564
},
538565
{
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+$'
544571
},
545-
'schemaPath': '#/allOf/1/properties/fur/pattern'
572+
schemaPath: '#/allOf/1/properties/fur/pattern'
546573
}
547574
]);
548575
expect(isMatch).to.be.false;
@@ -552,12 +579,12 @@ describe('oai3 - request tests', function () {
552579
describe('discriminator-pet', function () {
553580
let schemaEndpoint;
554581
before(function () {
555-
schemaEndpoint = schema['/pet-discriminator']['post'].body['application/json'];
582+
schemaEndpoint = schema['/pet-discriminator'].post.body['application/json'];
556583
});
557584
it('missing discriminator field', function () {
558585
// body match
559-
let isBodysMatch = schemaEndpoint.validate({
560-
'bark': 'hav hav'
586+
const isBodysMatch = schemaEndpoint.validate({
587+
bark: 'hav hav'
561588
});
562589

563590
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 () {
571598
});
572599
it('when discriminator type is dog and missing field', function () {
573600
// body match
574-
let isBodysMatch = schemaEndpoint.validate({
575-
'type': 'dog_object'
601+
const isBodysMatch = schemaEndpoint.validate({
602+
type: 'dog_object'
576603
});
577604
expect(schemaEndpoint.errors).to.be.eql([
578605
{
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'
584611
},
585-
'schemaPath': '#/required'
612+
schemaPath: '#/required'
586613
}
587614
]);
588615
expect(isBodysMatch).to.be.false;
589616
});
590617
it('valid complex body', function () {
591618
// body match
592-
let isBodysMatch = schemaEndpoint.validate({
619+
const isBodysMatch = schemaEndpoint.validate({
593620
bark: 'hav hav',
594621
type: 'dog_object'
595622
});
@@ -600,12 +627,12 @@ describe('oai3 - request tests', function () {
600627
describe('discriminator-multiple pet', function () {
601628
let schemaEndpoint;
602629
before(function () {
603-
schemaEndpoint = schema['/pet-discriminator-multiple']['post'].body['application/json'];
630+
schemaEndpoint = schema['/pet-discriminator-multiple'].post.body['application/json'];
604631
});
605632
it('missing discriminator field', function () {
606633
// body match
607-
let isBodysMatch = schemaEndpoint.validate({
608-
'fur': 'hav hav'
634+
const isBodysMatch = schemaEndpoint.validate({
635+
fur: 'hav hav'
609636
});
610637

611638
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 () {
620647
});
621648
it('missing discriminator field on the on inside discriminator', function () {
622649
// body match
623-
let isBodysMatch = schemaEndpoint.validate({
650+
const isBodysMatch = schemaEndpoint.validate({
624651
bark: 'hav hav',
625652
type: 'dog_multiple'
626653
});
@@ -636,7 +663,7 @@ describe('oai3 - request tests', function () {
636663
});
637664
it('when discriminator type is dog_multiple and model small_dog and missing root field name and specific plane field', function () {
638665
// body match
639-
let isBodysMatch = schemaEndpoint.validate({
666+
const isBodysMatch = schemaEndpoint.validate({
640667
type: 'dog_multiple',
641668
model: 'small_dog'
642669
});
@@ -648,7 +675,7 @@ describe('oai3 - request tests', function () {
648675
});
649676
it('when valid discriminator type is dog_multiple and model small_dog', function () {
650677
// body match
651-
let isBodysMatch = schemaEndpoint.validate({
678+
const isBodysMatch = schemaEndpoint.validate({
652679
name: 'sesna',
653680
max_length: 'max_length',
654681
dog_age: '3',
@@ -662,11 +689,11 @@ describe('oai3 - request tests', function () {
662689
describe('discriminator-mapping pet', function () {
663690
let schemaEndpoint;
664691
before(function () {
665-
schemaEndpoint = schema['/pet-discriminator-mapping']['post'].body['application/json'];
692+
schemaEndpoint = schema['/pet-discriminator-mapping'].post.body['application/json'];
666693
});
667694
it('missing discriminator field on the root', function () {
668695
// body match
669-
let isBodysMatch = schemaEndpoint.validate({
696+
const isBodysMatch = schemaEndpoint.validate({
670697
fur: '6'
671698
});
672699

@@ -681,7 +708,7 @@ describe('oai3 - request tests', function () {
681708
});
682709
it('when discriminator type is mapped_dog and model small_dog and missing root field name and specific dog field', function () {
683710
// body match
684-
let isBodysMatch = schemaEndpoint.validate({
711+
const isBodysMatch = schemaEndpoint.validate({
685712
type: 'mapped_dog',
686713
model: 'small_dog'
687714
});
@@ -693,7 +720,7 @@ describe('oai3 - request tests', function () {
693720
});
694721
it('when valid discriminator type is mapped_dog and model small_dog', function () {
695722
// body match
696-
let isBodysMatch = schemaEndpoint.validate({
723+
const isBodysMatch = schemaEndpoint.validate({
697724
name: 'sesna',
698725
max_length: 'max_length',
699726
dog_age: '200',
@@ -708,3 +735,27 @@ describe('oai3 - request tests', function () {
708735
});
709736
});
710737
});
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

Comments
 (0)
Please sign in to comment.