Open
Description
Describe the bug
The function isJSON
rejects valid json.
Examples
import validator from 'validator'
const validJson = "0"
if (!validator.isJSON(validJson)) {
throw Error(`Invalid json: ${validJson}`)
}
You can also use fast-check
to generate examples for testing:
import * as fc from 'fast-check'
import validator from 'validator'
test('isJSON', () => {
fc.assert(
fc.property(fc.json(), (json) => {
expect(validator.isJSON(json)).toBeTruthy()
}),
)
})
FAIL src/fast-check/fast-check.spec.ts
● isJSON
Property failed after 4 tests
{ seed: 1003302133, path: "3:0", endOnFailure: true }
Counterexample: ["0"]
Shrunk 1 time(s)
Got error: Error: expect(received).toBeTruthy()
Received: false
Stack trace: Error: expect(received).toBeTruthy()
Received: false
5 | fc.assert(
6 | fc.property(fc.json(), (json) => {
> 7 | expect(validator.isJSON(json)).toBeTruthy()
| ^
8 | }),
9 | )
10 | })
Additional context
Just wondering why we don't do the following?
export default function isJSON(str) {
assertString(str);
try {
var obj = JSON.parse(str);
return true
} catch (e) {
/* ignore */
}
return false;
}