question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better error messages when oneOf/anyOf doesn't match

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mafintoshcommented, Feb 26, 2015

@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

0reactions
adraicommented, Jul 31, 2015

+1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data does not match any schemas from 'oneOf' - Stack Overflow
I added c.GeneratePolymorphicSchemas(); to the AddSwaggerGen options but it has not helped. [Update] Here is the first error message. ERROR: ...
Read more >
Handling Validation Errors - python-jsonschema
Try to find an error that appears to be the best match among given errors. In general, errors that are higher up in...
Read more >
Understanding JSON Schema
JSON Schema is a powerful tool for validating the structure of JSON data. However, learning to use it by reading its.
Read more >
JSON validation - IBM
Nested oneOf, anyOf, and allOf schema constructs can result in a large number of error messages, as the JSON parser attempts to validate...
Read more >
Language Guide (proto3) | Protocol Buffers - Google Developers
Message fields can be one of the following: singular : a well-formed message can have zero or one of this field (but not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found