Skip to content

Commit d505bd7

Browse files
kerenfikobik
authored andcommitted
Add additional properties in error message (#92)
* add addtional properties in error message * remove brackets from error message when error.keyword additionalProperties fix typo * remove typo
1 parent 06b9b28 commit d505bd7

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

src/inputValidationError.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const buildMessage = function(error){
3333
return `${error.message} [${error.params.allowedValues.toString()}]`;
3434
}
3535

36+
if (error.keyword === 'additionalProperties') {
37+
return `${error.message} '${error.params.additionalProperty.toString()}'`;
38+
}
39+
3640
if (error.validation) {
3741
return error.errors.message;
3842
}

test/openapi3/openapi3-test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,31 @@ describe('input-validation middleware tests', function () {
306306
});
307307
});
308308
});
309+
describe('additionalProperties flag false', function () {
310+
it('invalid update when body contains properties which are not in schema', function (done) {
311+
request(app)
312+
.put('/dog/1')
313+
.set('public-key', '1.0')
314+
.send({
315+
max_length: '10',
316+
min_length: '5',
317+
additional1: '1',
318+
additional2: '2'
319+
})
320+
.expect(400, function (err, res) {
321+
if (err) {
322+
throw err;
323+
}
324+
expect(res.body).to.eql({
325+
'more_info': JSON.stringify(
326+
[
327+
"body should NOT have additional properties 'additional1'",
328+
"body should NOT have additional properties 'additional2'"])
329+
});
330+
done();
331+
});
332+
});
333+
});
309334
describe.skip('discriminator pet type is not on the root, only on child', function () {
310335
// does not support wright now.
311336
});
@@ -334,5 +359,28 @@ describe('input-validation middleware tests', function () {
334359
done();
335360
});
336361
});
362+
describe('additionalProperties flag false', function () {
363+
it('invalid update when body contains properties which are not in schema', function (done) {
364+
request(app)
365+
.put('/dog/1')
366+
.set('public-key', '1.0')
367+
.send({
368+
max_length: '10',
369+
min_length: '5',
370+
additional1: '1',
371+
additional2: '2'
372+
})
373+
.expect(400, function (err, res) {
374+
if (err) {
375+
throw err;
376+
}
377+
expect(res.body).to.eql({
378+
'more_info': JSON.stringify(
379+
"body should NOT have additional properties 'additional1'")
380+
});
381+
done();
382+
});
383+
});
384+
});
337385
});
338386
});

test/openapi3/pets.yaml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,77 @@ paths:
140140
$ref: '#/components/schemas/pet'
141141
description: Create pet
142142
required: true
143+
/dog/{id}:
144+
put:
145+
summary: Create a Pet
146+
security:
147+
- public-key: []
148+
description: >-
149+
tags:
150+
- pets
151+
operationId: update-a-pet
152+
parameters:
153+
- $ref: '#/components/parameters/public-key'
154+
- name: id
155+
in: path
156+
description: >
157+
The identifier of the pet
158+
required: true
159+
example: 1
160+
schema:
161+
type: string
162+
responses:
163+
'200':
164+
description: pet updated
165+
headers:
166+
x-zooz-request-id:
167+
description: request id
168+
schema:
169+
type: string
170+
content:
171+
application/json:
172+
schema:
173+
$ref: '#/components/schemas/small_dog'
174+
'400':
175+
description: Bad request
176+
headers:
177+
x-zooz-request-id:
178+
description: request id
179+
schema:
180+
type: string
181+
content:
182+
application/json:
183+
schema:
184+
$ref: '#/components/schemas/error_model'
185+
'401':
186+
description: Unauthorize
187+
headers:
188+
x-zooz-request-id:
189+
description: request id
190+
schema:
191+
type: string
192+
content:
193+
application/json:
194+
schema:
195+
$ref: '#/components/schemas/error_model'
196+
'500':
197+
description: Internal error
198+
headers:
199+
x-zooz-request-id:
200+
description: request id
201+
schema:
202+
type: string
203+
content:
204+
application/json:
205+
schema:
206+
$ref: '#/components/schemas/error_model'
207+
requestBody:
208+
content:
209+
application/json:
210+
schema:
211+
$ref: '#/components/schemas/medium_dog'
212+
description: Update pet
213+
143214
/pet-discriminator:
144215
post:
145216
summary: Create a pet
@@ -513,3 +584,15 @@ components:
513584
properties:
514585
min_length:
515586
type: string
587+
medium_dog:
588+
type: object
589+
required:
590+
- min_length
591+
- max_length
592+
additionalProperties: false
593+
properties:
594+
max_length:
595+
type: string
596+
min_length:
597+
type: string
598+

test/openapi3/test-server-pet.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ module.exports = function (options) {
2525
app.post('/pet', inputValidation.validate, function (req, res, next) {
2626
res.json({ result: 'OK' });
2727
});
28+
app.put('/dog/:id', inputValidation.validate, function (req, res, next) {
29+
res.json({ result: 'OK' });
30+
});
2831
app.post('/pet-discriminator', inputValidation.validate, function (req, res, next) {
2932
res.json({ result: 'OK' });
3033
});

0 commit comments

Comments
 (0)