Skip to content

Instantly share code, notes, and snippets.

@jayniz
Last active March 29, 2016 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayniz/e8849ea528af6d205698 to your computer and use it in GitHub Desktop.
Save jayniz/e8849ea528af6d205698 to your computer and use it in GitHub Desktop.
require 'json'
require 'json-schema'
schema = JSON.parse <<JSON
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"tag": {
"oneOf": [
{
"$ref": "#/definitions/some:thing"
},
{
"type": "null"
}
]
}
},
"definitions": {
"some:thing": {
"type": "object",
"properties": {
"id": {
"type": [
"number"
]
}
},
"required": [
"id"
]
}
}
}
JSON
data = JSON.parse('{"tag": {"id": 1}}')
empty_data = JSON.parse('{}')
nil_data = JSON.parse('{"tag": null}')
# According to [1] three should be valid but raise:
#
# .../gems/json-schema-2.6.1/lib/json-schema/attributes/ref.rb:63:
# in `block in get_referenced_uri_and_schema':
# The fragment '/definitions/:thing' does not exist on schema
# 2b4d1637-5825-532b-a8a0-2a531331cdad
# (JSON::Schema::SchemaError)
#
JSON::Validator.validate!(schema, data)
JSON::Validator.validate!(schema, empty_data)
JSON::Validator.validate!(schema, nil_data)
# [1] http://jsonschemalint.com/draft4/ you can just copy paste
# schema and datas from above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment