Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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