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

Commit 1e25940

Browse files
fix: throws upon invalid JSON schema during body validation
1 parent c2ede2d commit 1e25940

File tree

3 files changed

+21
-51
lines changed

3 files changed

+21
-51
lines changed

lib/units/validateBody.js

Lines changed: 7 additions & 32 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

@@ -148,27 +152,6 @@ function getBodyValidator(realType, expectedType) {
148152
return [null, validator[0]];
149153
}
150154

151-
/**
152-
* Parses a given JSON Schema to ensure it's a valid JSON.
153-
* @param {string} schema
154-
*/
155-
const validateJsonSchema = (schema) => {
156-
try {
157-
JSON.parse(schema);
158-
} catch (error) {
159-
throw new Error(`\
160-
Failed to parse a given JSON Schema:
161-
162-
${schema}
163-
164-
Please check that the given JSON Schema is a valid JSON.
165-
166-
Original parsing error:
167-
${error.message}\
168-
`);
169-
}
170-
};
171-
172155
/**
173156
* Validates given bodies of transaction elements.
174157
* @param {Object<string, any>} expected
@@ -192,14 +175,6 @@ function validateBody(expected, real) {
192175
'real'
193176
);
194177

195-
// Attempt to parse a given JSON Schema
196-
// to verify that it's a valid JSON.
197-
// TODO Move this into an input validation layer
198-
// once such is introduced.
199-
if (typeof expected.bodySchema === 'string') {
200-
validateJsonSchema(expected.bodySchema);
201-
}
202-
203178
const [expectedTypeError, expectedType] = expected.bodySchema
204179
? getBodySchemaType(expected.bodySchema)
205180
: getBodyType(

test/unit/units/validateBody.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@ describe('validateBody', () => {
469469
);
470470

471471
it('must throw with malformed JSON schema error', () => {
472-
expect(getResult).to.throw(/^Failed to parse a given JSON Schema:/g);
472+
expect(getResult).to.throw(
473+
/^Failed to validate HTTP message "body": given JSON Schema is not a valid JSON/g
474+
);
473475
});
474476
});
475477
});

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)