Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 4012ccc

Browse files
Merge pull request #214 from apiaryio/malformed-json-schema
Adds json schema parsing check during body validation
2 parents 5a9c50f + 1e25940 commit 4012ccc

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

lib/units/validateBody.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ function getBodySchemaType(bodySchema) {
102102
try {
103103
jph.parse(bodySchema);
104104
return [null, jsonSchemaType];
105-
} catch (exception) {
106-
const error = `Can't validate: expected body JSON Schema is not a parseable JSON:\n${exception.message}`;
105+
} catch (error) {
106+
// Gavel must throw when given malformed JSON Schema.
107+
// See https://github.com/apiaryio/gavel.js/issues/203
108+
throw new Error(`\
109+
Failed to validate HTTP message "body": given JSON Schema is not a valid JSON.
107110
108-
return [error, null];
111+
${error.message}\
112+
`);
109113
}
110114
}
111115

@@ -170,6 +174,7 @@ function validateBody(expected, real) {
170174
real.headers && real.headers['content-type'],
171175
'real'
172176
);
177+
173178
const [expectedTypeError, expectedType] = expected.bodySchema
174179
? getBodySchemaType(expected.bodySchema)
175180
: getBodyType(

test/unit/units/validateBody.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,34 @@ describe('validateBody', () => {
444444
});
445445
});
446446
});
447+
448+
describe('given malformed JSON schema', () => {
449+
const getResult = () =>
450+
validateBody(
451+
{
452+
// Purposely invalid JSON Schema
453+
bodySchema: `
454+
{
455+
"$schema": "http://json-schema.org/draft-04/schema#",
456+
"type": "object",
457+
"properties": {
458+
"href"": {
459+
"type": "string"
460+
}
461+
}
462+
}
463+
}
464+
`
465+
},
466+
{
467+
body: '{ "foo": "bar" }'
468+
}
469+
);
470+
471+
it('must throw with malformed JSON schema error', () => {
472+
expect(getResult).to.throw(
473+
/^Failed to validate HTTP message "body": given JSON Schema is not a valid JSON/g
474+
);
475+
});
476+
});
447477
});

test/unit/units/validateBody/getBodySchemaType.test.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { assert } = require('chai');
1+
const { expect } = require('chai');
22
const mediaTyper = require('media-typer');
33
const { getBodySchemaType } = require('../../../../lib/units/validateBody');
44

@@ -17,14 +17,13 @@ describe('getBodySchemaType', () => {
1717
const [error, mediaType] = getBodySchemaType(value);
1818

1919
it('returns "application/schema+json" media type', () => {
20-
assert.deepEqual(
21-
mediaType,
20+
expect(mediaType).to.deep.equal(
2221
mediaTyper.parse('application/schema+json')
2322
);
2423
});
2524

2625
it('has no errors', () => {
27-
assert.isNull(error);
26+
expect(error).to.be.null;
2827
});
2928
});
3029
});
@@ -41,31 +40,25 @@ describe('getBodySchemaType', () => {
4140
const [error, mediaType] = getBodySchemaType(jsonSchema);
4241

4342
it('returns "application/schema+json" media type', () => {
44-
assert.deepEqual(
45-
mediaType,
43+
expect(mediaType).to.deep.equal(
4644
mediaTyper.parse('application/schema+json')
4745
);
4846
});
4947

5048
it('returns no errors', () => {
51-
assert.isNull(error);
49+
expect(error).to.be.null;
5250
});
5351
});
5452
});
5553
});
5654

5755
describe('when given non-JSON string', () => {
58-
const [error, mediaType] = getBodySchemaType('foo');
56+
const run = () => getBodySchemaType('foo');
5957

60-
it('returns no media type', () => {
61-
assert.isNull(mediaType);
62-
});
63-
64-
it('returns parsing error', () => {
65-
assert.match(
66-
error,
67-
/^Can't validate: expected body JSON Schema is not a parseable JSON:/
68-
);
69-
});
58+
it('throws exception about invalid JSON', () => [
59+
expect(run).to.throw(
60+
/^Failed to validate HTTP message "body": given JSON Schema is not a valid JSON/
61+
)
62+
]);
7063
});
7164
});

0 commit comments

Comments
 (0)