Skip to content

Commit d047ccb

Browse files
authored
Merge pull request #59 from cdimascio/additional-prop-info
Missing list of additional properties, when additionalProperties error is thrown #58
2 parents bc50cc0 + 7ff34c0 commit d047ccb

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-openapi-validator",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "Automatically validate API requests using an OpenAPI 3 and Express.",
55
"main": "dist/index.js",
66
"scripts": {

src/middlewares/util.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ export function ajvErrorsToValidatorError(status, errors) {
3939
e.params &&
4040
e.params.missingProperty &&
4141
e.dataPath + '.' + e.params.missingProperty;
42+
const additionalProperty =
43+
e.params &&
44+
e.params.additionalProperty &&
45+
e.dataPath + '.' + e.params.additionalProperty;
46+
const path = required || additionalProperty || e.dataPath || e.schemaPath;
4247
return {
43-
path: required || e.dataPath || e.schemaPath,
48+
path,
4449
message: e.message,
4550
errorCode: `${e.keyword}.openapi.validation`,
4651
};

test/request.bodies.ref.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,28 @@ describe(packageJson.name, () => {
5050
testProperty: 'abc',
5151
})
5252
.expect(200));
53+
54+
it('should return 400 if an additional property is encountered', async () =>
55+
request(app)
56+
.post(`${basePath}/request_bodies_ref`)
57+
.send({
58+
testProperty: 'abc',
59+
invalidProperty: 'abc',
60+
invalidProperty2: 'abc',
61+
})
62+
.expect(400)
63+
.then(r => {
64+
const errors = r.body.errors;
65+
expect(errors)
66+
.to.be.an('array')
67+
.with.length(2);
68+
expect(errors[0].path).to.equal('.body.invalidProperty');
69+
expect(errors[0].message).to.equal(
70+
'should NOT have additional properties',
71+
);
72+
expect(errors[1].path).to.equal('.body.invalidProperty2');
73+
expect(errors[1].message).to.equal(
74+
'should NOT have additional properties',
75+
);
76+
}));
5377
});

test/resources/request.bodies.ref.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ components:
2626
application/json:
2727
schema:
2828
type: object
29+
additionalProperties: false
2930
properties:
3031
testProperty:
3132
type: string

0 commit comments

Comments
 (0)