brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

"More than one subschema matched in 'oneOf' match"--but which? #25

Closed gjtorikian closed 9 years ago

gjtorikian commented 9 years ago

When you provide input for more than one subschema, you receive the above error message: https://github.com/brandur/json_schema/blob/4090ee106229c1dd813e354062281191bf177378/lib/json_schema/validator.rb#L412

It would be helpful, I think, to list which values were provided, and which were expected. For example: "You provided both itema and itemb, but only oneOf should match." Something like that.

I can try to open up a PR if that's agreeable.

brandur commented 9 years ago

@gjtorikian Awesome idea.

I think one difficulty I had when implementing this was how to represent a schema in an error message and still have it succinct enough not to be too unreadable. Do you have any particular ideas here? The full schema in the error would be quite verbose, so maybe you'd just provide a JSON reference to it?

gjtorikian commented 9 years ago

Hrm, you're right, the more I think about it, the trickier it becomes. There's no telling what oneOf looks like. Is it like this:

"oneOf": [
  { "$ref": "#/definitions/diskDevice" }, 
  { "$ref": "#/definitions/diskUUID" },
  { "$ref": "#/definitions/nfs" },
  { "$ref": "#/definitions/tmpfs" }
]

Or like this:

"oneOf": [
  { "type": "number", "multipleOf": 5 },
  { "type": "number", "multipleOf": 3 }
]

Or this:

"oneOf": [
   {"required": ["diskDevice"]},
   {"required": ["diskUUID"]}
]

If each element was spat out as a straight key, would that work? So: More than one value provided: {"required": ["diskDevice"]}, {"required": ["diskUUID"]}.

brandur commented 9 years ago

@gjtorikian It might! Do you have any ideas on how we might be able to format a larger schema in the error message though? It seems like any schema of sufficient size would lead to run-on error messages that would be fairly difficult to read.

gjtorikian commented 9 years ago

I'd rather have an informative error message that runs on than an uninformative one. :smile_cat:

brandur commented 9 years ago

Haha, cool. +1. What do you think about the JSON reference approach? Example:

oneOf matched more than one subschema. Matched subschemas were: /schemata/app#/definitions/contrived/oneOf/1, /schemata/app#/definitions/contrived/oneOf/3, and /schemata/app#/definitions/contrived/oneOf/4.

It's not terribly helpful by itself either, but at least it gives the user the necessary information that they'll need to debug the problem, even if it'll require a bit of leg work on their part.

gjtorikian commented 9 years ago

Yeah, I think that's brilliant.

brandur commented 9 years ago

Yeah, I think that's brilliant.

Nice! :) Still feel like trying your hand at a pull? Haha.

gjtorikian commented 9 years ago

I'll try it out, sure.

gjtorikian commented 9 years ago

This looks resolved by https://github.com/brandur/json_schema/pull/26. Thanks @isaacseymour!