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

Commit 31f54db

Browse files
Merge pull request #268 from apiaryio/body-unparsable-json
Removes "jju", always uses JSON.parse() for expected/actual bodies
2 parents 48ecefe + 3aade08 commit 31f54db

File tree

11 files changed

+177
-1056
lines changed

11 files changed

+177
-1056
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
<a href="https://ci.appveyor.com/project/Apiary/gavel-js/branch/master" target="_blank">
99
<img src="https://ci.appveyor.com/api/projects/status/0cpnaoakhs8q58tn/branch/master?svg=true" alt="Build Status" />
1010
</a>
11-
<a href="https://coveralls.io/r/apiaryio/gavel.js?branch=master" target="_blank">
12-
<img src="https://coveralls.io/repos/apiaryio/gavel.js/badge.svg?branch=master" alt="Coverage Status" />
13-
</a>
1411
<a href="https://snyk.io/test/npm/gavel" target="_blank">
1512
<img src="https://snyk.io/test/npm/gavel/badge.svg" alt="Known Vulnerabilities" />
1613
</a>

lib/units/validateBody.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function getBodyType(body, contentType, httpMessageOrigin) {
7070

7171
try {
7272
parseJson(body);
73+
7374
const bodyMediaType = parseContentType(
7475
hasJsonContentType ? contentType : 'application/json'
7576
);

lib/utils/parseJson.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const { parse } = require('jju/lib/parse');
2-
1+
/**
2+
* Parses JSON.
3+
*
4+
* Previously we've used jsonlint and jju for parsing. While we've stepped
5+
* back to use plain JSON.parse(), we keep our doors open to switch the parser
6+
* - which is the reason why this is abstracted into parseJson()
7+
*/
38
const parseJson = (json, revivew) => {
4-
try {
5-
return parse(json, revivew);
6-
} catch (error) {
7-
throw error;
8-
}
9+
return JSON.parse(json, revivew);
910
};
1011

1112
module.exports = parseJson;

lib/utils/schema-v4-generator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const getType = require('./get-type');
2+
const parseJson = require('./parseJson');
23

34
const SCHEMA_VERSION = 'http://json-schema.org/draft-04/schema#';
45

@@ -30,7 +31,7 @@ class SchemaV4Generator {
3031
* @option {SchemaProperties} properties
3132
*/
3233
constructor({ json, properties }) {
33-
this.json = typeof json === 'string' ? JSON.parse(json) : json;
34+
this.json = typeof json === 'string' ? parseJson(json) : json;
3435

3536
this.schema = undefined;
3637
this.properties = properties || new SchemaV4Properties({});

lib/validators/json-schema.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const deepEqual = require('deep-equal');
33
const tv4 = require('tv4');
44
const jsonPointer = require('json-pointer');
55

6+
const parseJson = require('../utils/parseJson');
67
const metaSchemaV3 = require('../meta-schema-v3');
78
const metaSchemaV4 = require('../meta-schema-v4');
89
const errors = require('../errors');
@@ -89,7 +90,7 @@ class JsonSchema {
8990

9091
if (typeof this.data === 'string') {
9192
try {
92-
this.data = JSON.parse(this.data);
93+
this.data = parseJson(this.data);
9394
} catch (error) {
9495
const outError = new errors.DataNotJsonParsableError(
9596
`JSON validator: body: ${error.message}`
@@ -101,7 +102,7 @@ class JsonSchema {
101102

102103
if (typeof this.schema === 'string') {
103104
try {
104-
this.schema = JSON.parse(this.schema);
105+
this.schema = parseJson(this.schema);
105106
} catch (error) {
106107
const outError = new errors.SchemaNotJsonParsableError(
107108
`JSON validator: schema: ${error.message}`

0 commit comments

Comments
 (0)