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

Array Validates Against Schema When It Shouldn't #137

Closed
loganfrederick opened this issue Oct 15, 2014 · 2 comments
Closed

Array Validates Against Schema When It Shouldn't #137

loganfrederick opened this issue Oct 15, 2014 · 2 comments

Comments

@loganfrederick
Copy link

I've run into an issue where comparing a an array of data successfully validates against a schema I maintain when it shouldn't.

For example, if I have a schema file I maintain called schema.json which has a body like:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "My Title",
  "properties": {
    "my_field": {
      "type": "string"
    }
  }
}

Which I know works from validating other non-array JSON objects against it.

But when I set the following variable:

data = [1,2,3]

And call:

errors = JSON::Validator.fully_validate(schema.json, data)

It does not return any errors even though clearly the data array should not match the schema.

One hack workaround I tried is just throwing an error if data, converted to a string, starts with a "[", but that's obviously a hack.

Am I required to use ":strict => true" in this case (I'd rather not so as to allow flexibility in having other fields sent to the Schema Validator)? Has anyone else run into this?

@RST-J
Copy link
Contributor

RST-J commented Oct 15, 2014

Why shouldn't it match the schema? The schema does not require 'object' or any particular type for the data. And it only states that if there is a property 'my_field' then it must be of type string, but it may also be absent as it is not declared as required.
I'd agree with you if the validation still succeeds if there is a top-lebel 'type' definition of something that prohibits an array.

@loganfrederick
Copy link
Author

@RST-J I was not aware the top-level also had a type "object" available as an option. That fixes it. Thank you for the help!

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