xeipuuv / gojsonschema

An implementation of JSON Schema, draft v4 v6 & v7 - Go language
2.55k stars 357 forks source link

Extra error when validation for a "oneOf" rule fails #73

Open joshwget opened 9 years ago

joshwget commented 9 years ago

Whenever validation for a "oneOf" rule fails because of an incorrect type, an "incorrect type" error is also generated.

For example, with schema

{
  "type": "object",
  "properties": {
    "a": {
      "oneOf": [
        { "type": "array" },
        { "type": "object" }
      ]
    }
  }
}

and document

{
  "a": "a"
}

the following errors are generated

- a: Must validate one and only one schema (oneOf)
- a: Invalid type. Expected: array, given: string

It seems like a bug for there to be more than one error here. If it is expected behavior, the expected type for the second error only lists the first type in the "oneOf" rule.

cristiangraz commented 8 years ago

Looks like the author of this portion of the validation intended to have oneOf/anyOf/allOf errors return that specific error, as well as an additional error to go with it: https://github.com/xeipuuv/gojsonschema/blob/master/validation.go#L318-L322

comment reads:

// add error messages of closest matching subSchema as
// that's probably the one the user was trying to match

I agree with @joshwget in this scenario it doesn't make sense to have both errors -- when the second error could just as easily be "object" over "array".

It seems to make sense to keep the mergeErrors functionality in the allOf case where you'd want all of the validation rules to run and return all errors, but is anyone aware of a scenario with oneOf/anyOf where this would make sense to keep as is?

For reference/discussion, here is the list of tests that fail after making this change:

--- FAIL: TestJsonSchemaTestSuite (0.56s)
    schema_test.go:398: Test failed : anyOf :: neither anyOf valid, expects 2 errors, given 1 errors
    schema_test.go:409: Test failed : anyOf :: neither anyOf valid, expected 'number_any_of, number_gte' errors, given 'number_any_of' errors
    schema_test.go:398: Test failed : anyOf with base schema :: both anyOf invalid, expects 2 errors, given 1 errors
    schema_test.go:409: Test failed : anyOf with base schema :: both anyOf invalid, expected 'number_any_of, string_lte' errors, given 'number_any_of' errors
    schema_test.go:398: Test failed : invalid definition :: invalid definition schema, expects 2 errors, given 1 errors
    schema_test.go:409: Test failed : invalid definition :: invalid definition schema, expected 'enum, number_any_of' errors, given 'number_any_of' errors

/cc @xeipuuv