santhosh-tekuri/jsonschema

Only the first error is returned

Closed this issue · 2 comments

When validating a JSON document that contains multiple errors, only the trace for the first error is returned. For example:

{
  "items" : {
     "type" : "integer" 
  }
}

with a document [ "foo", "bar" ] should produce two errors, but only one error for "foo" is returned.
Similarly if we expand the schema to:

{
  "items" : {
     "type" : "integer" 
  },
  "if" : true,
  "then" : false
}

Only the error for the if/else is returned. All other implementations I tested return all errors and don't short-circuit. Example using AJV
Judging from the code it seems pretty intentional, so I'm not actually sure you see it as a bug, but it might be worth mentioning in the README or making this behaviour configurable as it is not the default behaviour I expected (although saying that I'm probably biased).

This was not intentional, i did not think of it.

since ValidationError struct already has Causes []*ValidationError it should be possible to implement this feature without breaking public API.

I agree that, returning all errors is more intuitive .

after this fix, the validation errors for the example you mentioned are shows as below:

$ cat schema.json
{
  "items" : {
     "type" : "integer"
  },
  "if" : true,
  "then" : false
}

$ cat doc.json
[ "hello" , "world"]

$ jv schema.json doc.json
"doc.json" does not conform to the schema specified. reason:
I[#] S[#] validation failed
  I[#] S[#/then] if-then failed
    I[#] S[#/then] always fail
  I[#/0] S[#/items/type] expected integer, but got string
  I[#/1] S[#/items/type] expected integer, but got string