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

Possible bug: wrong expectation of required object property #139

Closed
nepalez opened this issue Oct 25, 2014 · 5 comments
Closed

Possible bug: wrong expectation of required object property #139

nepalez opened this issue Oct 25, 2014 · 5 comments

Comments

@nepalez
Copy link

nepalez commented Oct 25, 2014

With a schema .../firm.json:

// JSON schema for a firm object
{
  "$schema": "http://json-schema.org/draft-04/schema#",

  "type": "object",

  "properties": {
    "uuid":     {
      "type": "string",
      "format": "^[a-f\\d]{8}-([a-f\\d]{4}-){3}[a-f\\d]{12}$"
    },
    "name":     { "type": "string", "minLength": 1 },
    "num":      { "type": "string", "minLength": 1 },
    "synonyms": {
      "type":  "array",
      "items": { "type": "string", "minimum": 1 }
    }
  },

  "additionalProperties": false,
  "required": ["uuid", "name"]
}

And a json file:

{
  "uuid": "b9e9d9c1-24ae-40de-a932-a7f46bc90632",
  "name": "Firm",
  "synonyms": []
}

I'm getting an error:

JSON::Schema::ValidationError:
  The property '#' did not contain a required property of 'num' in schema ... /firm.json#
    # .../json-schema/validator.rb:63:in `validation_error'
    # .../json-schema/attributes/properties_v4.rb:18:in `block in validate'
    # .../json-schema/attributes/properties_v4.rb:6:in `each'
    # .../json-schema/attributes/properties_v4.rb:6:in `validate'
    # .../json-schema/validator.rb:96:in `block in validate'
    # .../json-schema/validator.rb:94:in `each'
    # .../json-schema/validator.rb:94:in `validate'
    # .../json-schema/schema.rb:40:in `validate'
    # .../json-schema/attributes/ref.rb:8:in `validate'
    # .../json-schema/validator.rb:96:in `block in validate'
    # .../json-schema/validator.rb:94:in `each'
    # .../json-schema/validator.rb:94:in `validate'
    # .../json-schema/schema.rb:40:in `validate'
    # .../json-schema/attributes/items.rb:10:in `block in validate'
    # .../json-schema/attributes/items.rb:7:in `each'
    # .../json-schema/attributes/items.rb:7:in `each_with_index'
    # .../json-schema/attributes/items.rb:7:in `validate'
    # .../json-schema/validator.rb:96:in `block in validate'
    # .../json-schema/validator.rb:94:in `each'
    # .../json-schema/validator.rb:94:in `validate'
    # .../json-schema/schema.rb:40:in `validate'
    # .../json-schema/attributes/properties_v4.rb:24:in `block in validate'
    # .../json-schema/attributes/properties_v4.rb:6:in `each'
    # .../json-schema/attributes/properties_v4.rb:6:in `validate'
    # .../json-schema/validator.rb:96:in `block in validate'
    # .../json-schema/validator.rb:94:in `each'
    # .../json-schema/validator.rb:94:in `validate'
    # .../json-schema/schema.rb:40:in `validate'
    # .../json-schema/validator.rb:225:in `validate'
    # .../json-schema/validator.rb:396:in `validate!'

It seems validator requires num property, but it shouldn't.

Used ruby MRI 2.1.2, json-schema 2.2.5

@pd
Copy link
Contributor

pd commented Oct 25, 2014

I'm unable to reproduce this, also on 2.2.5. With JSON::Validator.fully_validate(schema, firm), I'm getting back no errors. Can you add a small script to trigger this in a gist?

@nepalez
Copy link
Author

nepalez commented Oct 25, 2014

https://gist.github.com/nepalez/ac7d592b77577e62f367

I've used JSON::Validator.validate!, not a JSON::Validator.fully_validate

@pd
Copy link
Contributor

pd commented Oct 25, 2014

Ah; this is due to the confusing behavior of strict validation when mixed with the required property in draft4. As far as I know, strict provides no real benefit to you, since you are already using required and additionalProperties. If possible, I'd suggest just not using that option.

The error is raised from here: https://github.com/hoxworth/json-schema/blob/master/lib/json-schema/attributes/properties_v4.rb#L18-L21

Perhaps that could check schema.has_key?('required') or similar, but the interactions of the features are already confusing enough IMO. =)

@nepalez
Copy link
Author

nepalez commented Oct 25, 2014

Thanks! You are right. With additionalProperties being explicitly set I don't need strict option either.

But now I wonder if a strict validation is useful at all. Isn't it always safer to set additionalProperties instead of strict?

@nepalez nepalez closed this as completed Oct 25, 2014
@hoxworth
Copy link
Contributor

Thanks a ton for taking this on @pd !

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

3 participants