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

Commit abfcbd8

Browse files
fix: provide custom abstraction above "jju" to stop relying on "fs"
1 parent ddeb03c commit abfcbd8

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

lib/units/validateBody.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const jph = require('json-parse-helpfulerror');
21
const mediaTyper = require('media-typer');
32
const contentTypeUtils = require('content-type');
43

4+
const { isValidField } = require('./isValid');
55
const { TextDiff, JsonExample, JsonSchema } = require('../validators');
66
const isset = require('../utils/isset');
7-
const { isValidField } = require('./isValid');
7+
const parseJson = require('../utils/parseJson');
88

99
function isPlainText(mediaType) {
1010
return mediaType.type === 'text' && mediaType.subtype === 'plain';
@@ -69,7 +69,7 @@ function getBodyType(body, contentType, httpMessageOrigin) {
6969
const hasJsonContentType = isJsonContentType(contentType);
7070

7171
try {
72-
jph.parse(body);
72+
parseJson(body);
7373
const bodyMediaType = parseContentType(
7474
hasJsonContentType ? contentType : 'application/json'
7575
);
@@ -101,7 +101,7 @@ function getBodySchemaType(bodySchema) {
101101
}
102102

103103
try {
104-
jph.parse(bodySchema);
104+
parseJson(bodySchema);
105105
return [null, jsonSchemaType];
106106
} catch (error) {
107107
// Gavel must throw when given malformed JSON Schema.

lib/utils/parseJson.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { parse } = require('jju/lib/parse');
2+
3+
const parseJson = (json, revivew) => {
4+
try {
5+
return parse(json, revivew);
6+
} catch (error) {
7+
throw error;
8+
}
9+
};
10+
11+
module.exports = parseJson;

package-lock.json

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"deep-equal": "1.0.1",
4444
"http-string-parser": "0.0.6",
4545
"jju": "1.4.0",
46-
"json-parse-helpfulerror": "1.0.3",
4746
"json-pointer": "0.6.0",
4847
"media-typer": "1.1.0",
4948
"tv4": "1.3.0"

test/cucumber/steps/fields.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const chai = require('chai');
2-
const jhp = require('json-parse-helpfulerror');
2+
const parseJson = require('../../../lib/utils/parseJson');
33

44
chai.config.truncateThreshold = 0;
55
const { expect } = chai;
@@ -9,7 +9,7 @@ module.exports = function() {
99
fieldName,
1010
expectedJson
1111
) {
12-
const expected = jhp.parse(expectedJson);
12+
const expected = parseJson(expectedJson);
1313
expect(this.result.fields[fieldName]).to.deep.equal(expected);
1414
});
1515
};

test/cucumber/steps/general.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
const { expect } = require('chai');
2-
const jhp = require('json-parse-helpfulerror');
2+
const parseJson = require('../../../lib/utils/parseJson');
33

44
module.exports = function() {
55
this.Given(
66
/^I expect the following HTTP (message|request|response):$/i,
77
function(_, expectedMessage) {
8-
this.expected = jhp.parse(expectedMessage);
8+
this.expected = parseJson(expectedMessage);
99
}
1010
);
1111

1212
this.Given(/^the actual HTTP (message|request|response) equals:$/i, function(
1313
_,
1414
actualMessage
1515
) {
16-
this.actual = jhp.parse(actualMessage);
16+
this.actual = parseJson(actualMessage);
1717
});
1818

1919
// Inline value assertion.
@@ -72,7 +72,7 @@ module.exports = function() {
7272
const stringifiedActual = JSON.stringify(this.result, null, 2);
7373

7474
expect(this.result).to.deep.equal(
75-
jhp.parse(expectedResult),
75+
parseJson(expectedResult),
7676
`\
7777
Expected the following result:
7878

0 commit comments

Comments
 (0)