Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages when oneOf/anyOf doesn't match #38

Open
skilledDeveloper opened this issue Feb 26, 2015 · 7 comments
Open

Better error messages when oneOf/anyOf doesn't match #38

skilledDeveloper opened this issue Feb 26, 2015 · 7 comments

Comments

@skilledDeveloper
Copy link

Consider this example:

var schema = {
    "type": "object",
    "required": ["foo", "bar"],
    "additionalProperties": false,
    "properties": {
        "foo": {
            "type": "integer"
        },
        "bar": {
            "oneOf": [{
                "type": "object",
                "required": ["id"],
                "additionalProperties": false,
                "properties": {
                    "id": {
                        "type": "integer"
                    }
                }
            }, {
                "type": null,
                "additionalProperties": false
            }]
        }
    }
};

var candidate = {
    "foo": 123,
    "bar": {
        "id": 456
    }
};

Based on the schema the candidate is valid. However, if I change the id to a string value (e.g "456"), I will get this error:

[ { field: 'data.bar',
    message: 'no (or more than one) schemas match' } ]

While the error message is not wrong, it can be more informative by saying which field has issue.
The same thing can apply for missing required fields or having additional properties when it's not allowed.

@danieljuhl
Copy link
Contributor

Are you using the latest version? Additional properties and required fields should be very informative, if you use the verbose mode.

How do you think the error message for the above example should be?

@skilledDeveloper
Copy link
Author

Doesn't really matter. Maybe the same way that it works for the required fields?
Or probably something like this:

[ { field: 'data.bar',
    message: 'no (or more than one) schemas match:  
         field: 'bar.id' 
         message: 'is the wrong type'} ]

@mafintosh
Copy link
Owner

@skilledDeveloper would something like the following work:

{
  field: 'data.bar.id',
  schema: 'data.bar#anyOf[1]',
  message: 'is the wrong type'
}

Then we could add a schema identifier to subschemas as well to solve stuff like #22 as well

@skilledDeveloper
Copy link
Author

That would be great.

@adrai
Copy link

adrai commented Mar 26, 2015

any news?

@watson
Copy link
Collaborator

watson commented Mar 26, 2015

@mafintosh I think a schema property would be a nice addition. First I thought it would be nicer if we could fix it without adding extra properties, but we add extra properties already when in verbose mode - so I guess it's ok :)

I don't know if there is a convention for naming these "anonymous" inline schemas, but I think your data.bar#oneOf[1] suggestion is pretty self explanatory.

When using $ref, I guess the schema value would just be the name of the external schema.

@adrai
Copy link

adrai commented Jul 31, 2015

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants