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

Commit

Permalink
fix: provide custom abstraction above "jju" to stop relying on "fs"
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Jul 17, 2019
1 parent 9ed4f62 commit a44852a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
8 changes: 4 additions & 4 deletions lib/units/validateBody.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const jph = require('json-parse-helpfulerror');
const mediaTyper = require('media-typer');
const contentTypeUtils = require('content-type');

const { isValidField } = require('./isValid');
const { TextDiff, JsonExample, JsonSchema } = require('../validators');
const isset = require('../utils/isset');
const { isValidField } = require('./isValid');
const parseJson = require('../utils/parseJson');

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

try {
jph.parse(body);
parseJson(body);
const bodyMediaType = parseContentType(
hasJsonContentType ? contentType : 'application/json'
);
Expand Down Expand Up @@ -101,7 +101,7 @@ function getBodySchemaType(bodySchema) {
}

try {
jph.parse(bodySchema);
parseJson(bodySchema);
return [null, jsonSchemaType];
} catch (error) {
// Gavel must throw when given malformed JSON Schema.
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/parseJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { parse } = require('jju/lib/parse');

const parseJson = (json, revivew) => {
try {
return parse(json, revivew);
} catch (error) {
throw error;
}
};

module.exports = parseJson;
11 changes: 2 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"curl-trace-parser": "0.0.10",
"deep-equal": "1.0.1",
"http-string-parser": "0.0.6",
"json-parse-helpfulerror": "1.0.3",
"jju": "1.4.0",
"json-pointer": "0.6.0",
"media-typer": "1.1.0",
"tv4": "1.3.0"
Expand Down
4 changes: 2 additions & 2 deletions test/cucumber/steps/fields.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const chai = require('chai');
const jhp = require('json-parse-helpfulerror');
const parseJson = require('../../../lib/utils/parseJson');

chai.config.truncateThreshold = 0;
const { expect } = chai;
Expand All @@ -9,7 +9,7 @@ module.exports = function() {
fieldName,
expectedJson
) {
const expected = jhp.parse(expectedJson);
const expected = parseJson(expectedJson);
expect(this.result.fields[fieldName]).to.deep.equal(expected);
});
};
8 changes: 4 additions & 4 deletions test/cucumber/steps/general.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const { expect } = require('chai');
const jhp = require('json-parse-helpfulerror');
const parseJson = require('../../../lib/utils/parseJson');

module.exports = function() {
this.Given(
/^I expect the following HTTP (message|request|response):$/i,
function(_, expectedMessage) {
this.expected = jhp.parse(expectedMessage);
this.expected = parseJson(expectedMessage);
}
);

this.Given(/^the actual HTTP (message|request|response) equals:$/i, function(
_,
actualMessage
) {
this.actual = jhp.parse(actualMessage);
this.actual = parseJson(actualMessage);
});

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

expect(this.result).to.deep.equal(
jhp.parse(expectedResult),
parseJson(expectedResult),
`\
Expected the following result:
Expand Down

0 comments on commit a44852a

Please sign in to comment.