Skip to content

Commit 411e284

Browse files
authored
Merge pull request #2373 from blackflux/dev
[Gally]: master <- dev
2 parents d55245a + 7375c97 commit 411e284

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/param/_abstract.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,20 @@ class Abstract {
5858
if (this.nullable !== true) {
5959
throw ApiErrorFn(`Non-nullable ${this.position}-Parameter "${this.name}" is null.`, 400, 99006);
6060
}
61-
} else if (!this.validate(result)) {
62-
throw ApiErrorFn(`Invalid Value for ${this.position}-Parameter "${this.name}" provided.`, 400, 99003, {
63-
value: result
64-
});
61+
} else {
62+
let valid = false;
63+
let details = null;
64+
try {
65+
valid = this.validate(result);
66+
} catch (e) {
67+
details = e.message;
68+
}
69+
if (valid !== true) {
70+
throw ApiErrorFn(`Invalid Value for ${this.position}-Parameter "${this.name}" provided.`, 400, 99003, {
71+
value: result,
72+
...(typeof details === 'string' ? { details } : {})
73+
});
74+
}
6575
}
6676
result = this.getter !== null && ![undefined, null].includes(result)
6777
? (params) => this.getter(result, params)

src/param/json.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ class Json extends Abstract {
2121
valid = false;
2222
}
2323
}
24-
if (valid && !Joi.test(valueParsed, this.schema)) {
25-
valid = false;
24+
if (valid) {
25+
const { error } = this.schema.validate(valueParsed);
26+
if (error !== undefined) {
27+
throw error;
28+
}
2629
}
2730
return valid;
2831
}

test/param/json.spec.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ describe('Testing Json Parameter', () => {
3838
})).to.deep.equal({ key: 'value' });
3939
});
4040

41-
it('Testing invalid json parameter', () => {
42-
expect(() => jsonParam.get({
43-
body: {
44-
param: 'string'
45-
}
46-
})).to.throw('Invalid Value for json-Parameter "param" provided.');
41+
it('Testing invalid json parameter', async ({ capture }) => {
42+
const err = await capture(() => jsonParam.get({ body: { param: 'string' } }));
43+
expect(err.message).to.equal('Invalid Value for json-Parameter "param" provided.');
44+
expect(err.context).to.deep.equal({ value: 'string', details: '"value" must be of type object' });
4745
});
4846
});

0 commit comments

Comments
 (0)