Open aelse opened 4 years ago
I could see how changing the README would communicate clearer what would be returned as an err
. I don't agree on treating malformed JSON and an invalid JSON document in the same manner. JSON schema validation and JSON parsing are fundamentally two different things and I don't want to combine the two in the same error mechanism.
If you're happy with a README change I'll put in a PR.
I'm always very happy with any kind of contribution 😃
When validating a JSON document I get an error from
Validate
if the user-provided document is not valid JSON. My expectation is that no error would be generated andresult.Valid() == false
.To reproduce, load a JSON schema and attempt to validate against a document that is not well-formed JSON:
{"abc": "invalid json encoding"
The current example code in the readme panics in the case of an error during validation. To me this suggests that error cases are due to internal logic or runtime problems rather than bad user input.
If I want to differentiate between invalid JSON and a gojsonschema error then I need to perform an additional step to unmarshal the payload.
I would suggest a behaviour change to return
nil
error and an invalid result in the case that the document is not well formed JSON, but that might be a big ask.Alternatively, the example in the readme could be updated to be clearer about the possible source of the error. eg.
Readme code below for easy reference.
Our code that encountered the problem looks more like:
The output from this program is: