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

json-schema 2.0.1 broke schema validation #58

Closed
dazza-codes opened this issue Jun 25, 2013 · 2 comments
Closed

json-schema 2.0.1 broke schema validation #58

dazza-codes opened this issue Jun 25, 2013 · 2 comments

Comments

@dazza-codes
Copy link

The full validation broke with v2.0.1 when it was fine with v2.0.0, for example:

### BEGIN JSON SCHEMA RUBY CODE
 PAGE_SCHEMA = <<-END_SCHEMA
  {
    "type": "object",
    "title": "page",
    "description": "A Resource Index page of results.",
    "additionalProperties": false,
    "properties": {
      "page": { "type": "number", "required": true },
      "pageCount": { "type": "number", "required": true },
      "prevPage": { "type": ["number","null"], "required": true },
      "nextPage": { "type": ["number","null"], "required": true },
      "links": { "type": "object", "required": true },
      "collection": { "type": "array", "required": true }
    }
  }
  END_SCHEMA
### END JSON SCHEMA RUBY CODE

The validation error reads: "JSON::Schema::ValidationError: The property '#/properties/page/required' of type TrueClass did not match the following type: array in schema http://json-schema.org/draft-04/schema#"

This can't be right for a "required" property, surely?

@hoxworth
Copy link
Contributor

The major version change from 1.x to 2.x was made due to the breaking changes introduced in JSON Schema Draft-04 with respect to a number of properties. Since Draft-04 is final, this library now uses it as the default validator when the specific version is not specified. The usage of the required keyword did change in Draft-04, and thus validation is failing.

Draft-03 support is still provided, so existing schemas can validate against Draft-03 through a variety of methods. The simplest and most portable way of doing this is to use the $schema keyword in your schema and set it to draft-03, as follows:

PAGE_SCHEMA <<-END_SCHEMA
  {
    "$schema": "http://json-schema.org/draft-03/schema#",
    "type": "object",
    "title": "page",
    ...
  }
END_SCHEMA

Alternatively, you can specify the draft-03 validator from the validation call, if you cannot or do not want to edit your schema itself.

JSON::Validator.validate(schema, data, :version => :draft3)

I know this is a frustrating change, and I'm not exactly sure what the right approach is to ensure backwards compatibility. My supposition is that since JSON Schema is still in draft 4 and is (slowly) changing, the best we can do with this library is give users the ability to lock to specific schema versions while updating the major version number whenever a new breaking change occurs in a published draft.

For reference, the following link outlines changes from draft-03 to draft-04, if you are interested in some of the updates: https://github.com/json-schema/json-schema/wiki/ChangeLog.

@hoxworth
Copy link
Contributor

hoxworth commented Jul 1, 2013

Closing this until a failure case can be presented with respect to schema versions.

@hoxworth hoxworth closed this as completed Jul 1, 2013
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