santhosh-tekuri/jsonschema

InvalidJSONTypeError.Error() should include unexpected value

Closed this issue · 1 comments

A InvalidJSONTypeError error is returned when the schema validator encounters an unexpected type. In that case, the error includes the type, e.g. invalid jsonType: map[interface{}]interface{}; the value is not included in the error message.

It would be useful to show the unexpected value because it's not very obvious which part of the input document has the wrong type.

Steps to reproduce:

Create the following YAML document and validate against a JSON schema:

data := `
mapping:
   2: "abc"
   foo: "bar"
`
var inputYaml interface{}
yaml.Unmarshal(data, &inputYaml)
schema.ValidateInterface(inputYaml)

With the above document, yaml.Unmarshal() unmarshals the input data into a map[interface{}]interface{}. Looking at this minimalistic example, it's obvious that 2 should have been a string. But with larger input documents, it is harder to find the problem.
I think this can only happen when unmarshaling from yaml, not from JSON.

Proposed Solution

A basic improvement would be to include contextual information:

- panic(InvalidJSONTypeError(fmt.Sprintf("%T", v)))
+ panic(InvalidJSONTypeError(fmt.Sprintf("%T: %#v", v, v)))

you have to search for the specified datatype in the interface you have passed.
it is not trivial to expect to give such debugging information.