Skip to content

Instantly share code, notes, and snippets.

@lumpidu
Created September 10, 2015 15:41
Show Gist options
  • Save lumpidu/e5ee62b46b16cdc688d1 to your computer and use it in GitHub Desktop.
Save lumpidu/e5ee62b46b16cdc688d1 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'json'
require 'json-schema'
schema = '{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "demo",
"type": "object",
"properties": {
"formats": {
"type": "array",
"items": {
"$ref": "#/definitions/format"
}
}
},
"definitions": {
"format": {
"oneOf": [
{
"$ref": "#/definitions/format_ascii"
},
{
"$ref": "#/definitions/format_binary"
}
]
},
"format_ascii": {
"type": "object",
"properties": {
"encoding": {
"type": "string",
"enum": [
"ascii"
],
"default": "ascii"
},
"scale": {
"type": "number",
"default": 1
}
},
"additionalProperties": false
},
"format_binary": {
"type": "object",
"properties": {
"encoding": {
"type": "string",
"enum": [
"binary"
],
"default": "binary"
},
"order": {
"type": "string",
"enum": [
"little",
"big",
"network"
]
}
},
"required": [
"order"
],
"additionalProperties": false
}
}
}'
data = '{
"formats": [ { "scale": 1}, { "order": "little"} ]
}'
json = JSON.parse(data)
JSON::Validator.fully_validate(schema, json, :insert_defaults => true)
puts json.inspect
puts JSON::Validator.fully_validate(schema, json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment