Features:
- Supports JSON Schema 2019-09 and 2020-12
- Supports custom dialects, vocabularies, keywords, format validators, output formatters
- Supports custom schema resolvers
Usage
require "json_skooma"
# Create a registry to store schemas, vocabularies, dialects, etc.
JSONSkooma.create_registry("2020-12", assert_formats: true)
# Load a schema
schema_hash = {
"$schema" => "https://json-schema.org/draft/2020-12/schema",
"type" => "object",
"properties" => {
"name" => {"type" => "string"},
"race" => {"enum" => %w[Nord Khajiit Argonian Breton Redguard Dunmer Altmer Bosmer Orc Imperial]},
"class" => {"type" => "string"},
"level" => {"type" => "integer", "minimum" => 1},
"equipment" => {
"type" => "array",
"items" => {"type" => "string"}
}
},
"required" => %w[name race class level]
}
schema = JSONSkooma::JSONSchema.new(schema_hash)
data_hash = {
name: "Matz",
race: "Human",
class: "Dragonborn",
level: 50,
equipment: %w[Ruby],
}
result = schema.evaluate(data_hash)
result.valid? # => false
result.output(:basic)
# {"valid"=>false,
# "errors"=>
# [{"instanceLocation"=>"",
# "keywordLocation"=>"/properties",
# "absoluteKeywordLocation"=>"urn:uuid:f477b6ca-7308-4be6-b88c-e848b9002793#/properties",
# "error"=>"Properties [\"race\"] are invalid"},
# {"instanceLocation"=>"/race",
# "keywordLocation"=>"/properties/race/enum",
# "absoluteKeywordLocation"=>"urn:uuid:f477b6ca-7308-4be6-b88c-e848b9002793#/properties/race/enum",
# "error"=>
# "The instance value \"Human\" must be equal to one of the elements in the defined enumeration: [\"Nord\", \"Khajiit\", \"Argonian\", \"Breton\", \"Redguard\", \"Dunmer\", \"Altmer\", \"Bosmer\", \"Orc\", \"Imperial\"]"}]}