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

Invalid schema not reported using fully_validate with a oneOf attribute #116

Closed
abargnesi opened this issue Aug 14, 2014 · 2 comments
Closed

Comments

@abargnesi
Copy link

JSON::Validator.fully_validate only reports a schema error with the oneOf attribute.

Here is an example test:

require 'json'
require 'json-schema'
require 'pathname'
require 'pp'
require 'minitest/autorun'
require 'minitest/unit'

class ExtTest < Minitest::Test

  def test_validate_schema
    schema = {
      "oneOf" => [
        {
          "type" => "object",
          "required" => ["a","b"],
          "properties" => {
            "a" => {"type" => "integer"},
            "b" => {"type" => "string"}
          }
        }
      ]
    }

    data = {
        "a" => "taco"
    }

    errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
    puts errors
  end
end

Running the test outputs:

[tony@starship test (reference_schema *)]$ ruby test-oneOf.rb 
Run options: --seed 25046

# Running:

{:schema=>#<URI::Generic:0x007f7a6cbc71e8 URL:840ddff5-9a23-5e12-b904-1790f2467ce3#>, :fragment=>"#/", :message=>"The property '#/' of type Hash did not match any of the required schemas in schema 840ddff5-9a23-5e12-b904-1790f2467ce3#", :failed_attribute=>"OneOf"}
.

Finished in 0.004075s, 245.3878 runs/s, 0.0000 assertions/s.

1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

If I remove the oneOf attribute then my output becomes:

[tony@starship test (reference_schema *)]$ ruby test-single.rb 
Run options: --seed 21753

# Running:

{:schema=>#<URI::Generic:0x007fecb81564c0 URL:01545b94-1588-55b9-8a56-97f328178a87#>, :fragment=>"#/", :message=>"The property '#/' did not contain a required property of 'b' in schema 01545b94-1588-55b9-8a56-97f328178a87#", :failed_attribute=>"Required"}
{:schema=>#<URI::Generic:0x007fecb81564c0 URL:01545b94-1588-55b9-8a56-97f328178a87#>, :fragment=>"#/a", :message=>"The property '#/a' of type String did not match the following type: integer in schema 01545b94-1588-55b9-8a56-97f328178a87#", :failed_attribute=>"TypeV4"}

Is it me misinterpreting the json-schema specification?

@hoxworth
Copy link
Contributor

This is as designed and follows the JSON Schema draft-04 specification. When oneOf is utilized, a JSON object only validates if it validates against one of the schemas defined in the oneOf schema array. Since the object in your example does not validate against any of the schemas in the oneOf schema array (you only have one schema), it fails validation for the oneOf directive. It would not follow JSON Schema conventions to list validation failures in all of the optional schemas listed in the oneOf directive, as if one of the schemas were to validate the data, there would be no validation failure.

If oneOf is absent, the object simply fails to validate against the schema (for the provided reasons).

Does this make sense?

@abargnesi
Copy link
Author

Your description makes sense to me, but the implementation isn't very useful. This prevents me from getting the cause of the oneOf schema validation error.

Are there other methods of retrieving the most-specific validation errors? For example the jsonschema package for Python provides a best_match API that reveals specific errors in the case of oneOf or allOf.

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

2 participants