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

Commit 19bc2f7

Browse files
committed
feat: replace jsonlint with json-parse-helpfulerror
1 parent 66f0c70 commit 19bc2f7

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
"googlediff": "0.1.0",
3333
"http-string-parser": "0.0.5",
3434
"is-type": "0.0.1",
35+
"json-parse-helpfulerror": "1.0.3",
3536
"json-pointer": "0.6.0",
36-
"jsonlint": "josdejong/jsonlint",
3737
"media-typer": "0.3.0",
3838
"tv4": "1.3.0"
3939
},

src/mixins/validatable-http-message.coffee

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
jsonlint = require 'jsonlint'
1+
jph = require 'json-parse-helpfulerror'
22
mediaTyper = require 'media-typer'
33
validators = require '../validators'
44

@@ -138,19 +138,20 @@ class Validatable
138138
contentType = @headers?['content-type']
139139
if @isJsonContentType contentType
140140
try
141-
jsonlint.parse @body
141+
jph.parse @body
142142
@validation.body.realType = contentType
143143
catch error
144144
message = {
145-
message: 'Real body "Content-Type" header is "' +
146-
contentType + '" but body is not a parsable JSON.'
145+
message: """\
146+
Real body 'Content-Type' header is '#{contentType}' \
147+
but body is not a parseable JSON:\n#{error.message}\
148+
"""
147149
severity: 'error'
148150
}
149-
message.message = message.message + "\n" + error.message
150151
@validation.body.results.push message
151152
else
152153
try
153-
JSON.parse @body
154+
jph.parse @body
154155
@validation.body.realType = 'application/json'
155156
catch error
156157
@validation.body.realType = 'text/plain'
@@ -163,10 +164,13 @@ class Validatable
163164
if @expected.bodySchema?
164165
if typeof @expected.bodySchema == 'string'
165166
try
166-
parsed = JSON.parse @expected.bodySchema
167+
parsed = jph.parse @expected.bodySchema
167168
if typeof parsed != 'object' or Array.isArray parsed
168169
message = {
169-
message: 'Cant\'t validate. Expected body JSON Schema is not an Object'
170+
message: """\
171+
Can't validate. Expected body JSON Schema 'Content-Type' is
172+
not a parseable JSON:\n#{error.message}\
173+
"""
170174
severity: 'error'
171175
}
172176
@validation.body.results.push message
@@ -187,18 +191,20 @@ class Validatable
187191

188192
if @isJsonContentType expectedContentType
189193
try
190-
jsonlint.parse @expected.body
194+
jph.parse @expected.body
191195
@validation.body.expectedType = expectedContentType
192196
catch error
193197
message = {
194-
message: 'Can\'t validate. Expected body Content-Type is ' + expectedContentType + ' but body is not a parseable JSON'
198+
message: """\
199+
Can't validate. Expected body 'Content-Type' is '#{expectedContentType}' \
200+
but body is not a parseable JSON:\n#{error.message}\
201+
"""
195202
severity: 'error'
196203
}
197-
message.message = message.message + ": " +error.message
198204
@validation.body.results.push message
199205
else
200206
try
201-
JSON.parse @expected.body
207+
jph.parse @expected.body
202208
@validation.body.expectedType = 'application/json'
203209
catch error
204210
@validation.body.expectedType = 'text/plain'

test/unit/mixins/validatable-http-message-test.coffee

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,17 @@ describe "Http validatable mixin", () ->
549549
assert.include results, 'error'
550550

551551
it 'should add error message with lint result', () ->
552-
expected = "Parse error on line 1:\n...\"creative?\": false, 'creativ': true }\n-----------------------^\nExpecting 'STRING', got 'undefined'"
552+
expected = """\
553+
Real body 'Content-Type' header is '#{contentType}' but body is not a parseable JSON:
554+
Unexpected token '\\'' at 1:22
555+
{"creative?": false, 'creativ': true }
556+
^
557+
"""
553558
messages = []
554559
for result in instance.validation.body.results
555560
messages.push result.message
556561

557-
assert.include messages[0], expected
562+
assert.equal messages[0], expected
558563

559564
it 'should not overwrite existing errors', () ->
560565
instance.validation.body.results = [
@@ -751,12 +756,17 @@ describe "Http validatable mixin", () ->
751756
assert.include messages[0], 'is not a parseable JSON'
752757

753758
it 'should add error message with lint result', () ->
754-
expected = "Parse error on line 1:\n...\"creative?\": false, 'creativ': true }\n-----------------------^\nExpecting 'STRING', got 'undefined'"
759+
expected = """\
760+
Can't validate. Expected body 'Content-Type' is '#{contentType}' but body is not a parseable JSON:
761+
Unexpected token '\\'' at 1:22
762+
{"creative?": false, 'creativ': true }
763+
^
764+
"""
755765
messages = []
756766
for result in instance.validation.body.results
757767
messages.push result.message
758768

759-
assert.include messages[0], expected
769+
assert.equal messages[0], expected
760770

761771
describe 'expected headers have not content-type application/json', () ->
762772
describe 'expected body is a parseable JSON', () ->

0 commit comments

Comments
 (0)