Skip to content

Commit 8717c02

Browse files
msmolkibertoad
authored andcommitted
Multiple content type support (#102)
1 parent 1c8d7eb commit 8717c02

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

src/middleware.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function validate(...args) {
3232
function _validateRequest(requestOptions) {
3333
return Promise.all([
3434
_validateParams(requestOptions.headers, requestOptions.params, requestOptions.query, requestOptions.files, requestOptions.path, requestOptions.method.toLowerCase()).catch(e => e),
35-
_validateBody(requestOptions.body, requestOptions.path, requestOptions.method.toLowerCase()).catch(e => e)
35+
_validateBody(requestOptions.body, requestOptions.path, requestOptions.method.toLowerCase(), requestOptions.headers['content-type']).catch(e => e)
3636
]).then(function (errors) {
3737
if (errors[0] || errors[1]) {
3838
return errors[0] && errors[1] ? Promise.reject(errors[0].concat(errors[1])) : errors[0] ? Promise.reject(errors[0]) : Promise.reject(errors[1]);
@@ -52,11 +52,15 @@ function _validateRequest(requestOptions) {
5252
});
5353
}
5454

55-
function _validateBody(body, path, method) {
55+
function _validateBody(body, path, method, contentType) {
5656
return new Promise(function (resolve, reject) {
57-
const methodSchema = schemaEndpointResolver.getMethodSchema(schemas, path, method);
58-
if (methodSchema && methodSchema.body && !methodSchema.body.validate(body)) {
59-
return reject(methodSchema.body.errors);
57+
const methodSchema = schemaEndpointResolver.getMethodSchema(schemas, path, method) || {};
58+
59+
if (methodSchema.body) {
60+
const validator = methodSchema.body[contentType] || methodSchema.body;
61+
if (!validator.validate(body)) {
62+
return reject(validator.errors);
63+
}
6064
}
6165
return resolve();
6266
});

test/openapi3/openapi3-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,40 @@ describe('input-validation middleware tests', function () {
6868
done();
6969
});
7070
});
71+
it('validate depending on content-type -- valid dog', function (done) {
72+
request(app)
73+
.post('/pet')
74+
.set('public-key', '1.0')
75+
.set('content-type', 'application/x-www-form-urlencoded')
76+
.send({
77+
bark: 'foo'
78+
})
79+
.expect(200, function (err, res) {
80+
if (err) {
81+
throw err;
82+
}
83+
expect(res.body.result).to.equal('OK');
84+
done();
85+
});
86+
});
87+
it('validate depending on content-type -- invalid dog', function (done) {
88+
request(app)
89+
.post('/pet')
90+
.set('public-key', '1.0')
91+
.set('content-type', 'application/x-www-form-urlencoded')
92+
.send({
93+
bark: 'not foo'
94+
})
95+
.expect(400, function (err, res) {
96+
if (err) {
97+
throw err;
98+
}
99+
expect(res.body).to.deep.equal({
100+
'more_info': '["body/bark should be equal to one of the allowed values [foo,bar]","body should have required property \'fur\'","body should match exactly one schema in oneOf"]'
101+
});
102+
done();
103+
});
104+
});
71105
it('valid cat', function (done) {
72106
request(app)
73107
.post('/pet')

test/openapi3/pets.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ paths:
138138
application/json:
139139
schema:
140140
$ref: '#/components/schemas/pet'
141+
application/x-www-form-urlencoded:
142+
schema:
143+
$ref: '#/components/schemas/pet2'
141144
description: Create pet
142145
required: true
143146
/dog/{id}:
@@ -493,6 +496,12 @@ components:
493496
oneOf:
494497
- $ref: '#/components/schemas/dog_object'
495498
- $ref: '#/components/schemas/cat_object'
499+
pet2:
500+
description: pet
501+
type: object
502+
oneOf:
503+
- $ref: '#/components/schemas/dog_object2'
504+
- $ref: '#/components/schemas/cat_object'
496505
pets:
497506
type: array
498507
items:
@@ -549,6 +558,16 @@ components:
549558
properties:
550559
bark:
551560
type: string
561+
dog_object2:
562+
type: object
563+
required:
564+
- bark
565+
properties:
566+
bark:
567+
type: string
568+
enum:
569+
- foo
570+
- bar
552571
dog_multiple:
553572
type: object
554573
required:

test/openapi3/test-server-pet.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function (options) {
1818
inputValidation.init(`${__dirname}/pets.yaml`, options || inputValidationOptions);
1919
const app = express();
2020
app.use(bodyParser.json());
21+
app.use(bodyParser.urlencoded());
2122

2223
app.get('/pets', inputValidation.validate, function (req, res, next) {
2324
res.json({ result: 'OK' });

0 commit comments

Comments
 (0)