Ruby JSON Schema Validator
This library is intended to provide Ruby with an interface for validating JSON
objects against a JSON schema conforming to JSON Schema Draft
6. Legacy support for
JSON Schema Draft 4,
JSON Schema Draft 3,
JSON Schema Draft 2, and
JSON Schema Draft 1 is
also included.
Additional Resources
Version 2.0.0 Upgrade Notes
Please be aware that the upgrade to version 2.0.0 will use Draft-04 by
default, so schemas that do not declare a validator using the $schema
keyword will use Draft-04 now instead of Draft-03. This is the reason for the
major version upgrade.
Version 3.0.0 Upgrade Notes
All individual changes are documented in the CHANGELOG.md. The biggest change
is that the new version only supports Ruby 2.5 and newer. Take a look into the
gemspec file to see the currently supported Ruby version and also
.github/workflows/test.yml
to see the Ruby versions we test on.
Installation
From rubygems.org:
gem install json-schema
From the git repo:
gem build json-schema.gemspec
gem install json-schema-*.gem
Validation
Three base validation methods exist:
validate
: returns a boolean on whether a validation attempt passesvalidate!
: throws a JSON::Schema::ValidationError
with an appropriate message/trace on where the validation failedfully_validate
: builds an array of validation errors return when validation is complete
All methods take two arguments, which can be either a JSON string, a file
containing JSON, or a Ruby object representing JSON data. The first argument to
these methods is always the schema, the second is always the data to validate.
An optional third options argument is also accepted; available options are used
in the examples below.
By default, the validator uses the JSON Schema Draft
4 specification for
validation; however, the user is free to specify additional specifications or
extend existing ones. Legacy support for Draft 1, Draft 2, and Draft 3 is
included by either passing an optional :version
parameter to the validate
method (set either as :draft1
or draft2
), or by declaring the $schema
attribute in the schema and referencing the appropriate specification URI. Note
that the $schema
attribute takes precedence over the :version
option during
parsing and validation.
For further information on json schema itself refer to Understanding
JSON Schema.
Basic Usage
require "json-schema"
schema = {
"type" => "object",
"required" => ["a"],
"properties" => {
"a" => {"type" => "integer"}
}
}
JSON::Validator.validate(schema, { "a" => 5 })
JSON::Validator.validate(schema, {})
require "json"
File.write("schema.json", JSON.dump(schema))
JSON::Validator.validate('schema.json', '{ "a": 5 }')
begin
JSON::Validator.validate!(schema, { "a" => "taco" })
rescue JSON::Schema::ValidationError => e
e.message
end
JSON::Validator.fully_validate(schema, { "a" => "taco" })
Advanced Options
require "json-schema"
schema = {
"type"=>"object",
"required" => ["a"],
"properties" => {
"a" => {
"type" => "integer",
"default" => 42
},
"b" => {
"type" => "object",
"properties" => {
"x" => {
"type" => "integer"
}
}
}
}
}
JSON::Validator.validate(schema, [{"a" => 1}, {"a" => 2}, {"a" => 3}], :list => true)
JSON::Validator.validate(schema, [{"a" => 1}, {"a" => 2}, {"a" => 3}])
JSON::Validator.fully_validate(schema, { "a" => "taco" }, :errors_as_objects => true)
JSON::Validator.validate(schema, { "a" => 1, "b" => { "x" => 2 } }, :strict => true)
JSON::Validator.validate(schema, { "a" => 1, "b" => { "x" => 2 }, "c" => 3 }, :strict => true)
JSON::Validator.validate(schema, { "a" => 1 }, :strict => true)
JSON::Validator.validate(schema, { "x" => 1 }, :fragment => "#/properties/b")
JSON::Validator.validate(schema, { "x" => 1 })
JSON::Validator.validate(schema, { "a" => 1 }, :validate_schema => true)
JSON::Validator.validate({ "required" => true }, { "a" => 1 }, :validate_schema => true)
JSON::Validator.validate(schema, {}, :insert_defaults => true)
JSON::Validator.validate(schema, {})
v2_schema = {
"type" => "object",
"properties" => {
"a" => {
"type" => "integer"
}
}
}
JSON::Validator.validate(v2_schema, {}, :version => :draft2)
JSON::Validator.validate(v2_schema, {})
JSON::Validator.validate(schema, { "a" => 1 }, :parse_data => false)
JSON::Validator.validate(schema, '{ "a": 1 }', :parse_data => false)
JSON::Validator.validate({type: "integer"}, "23")
JSON::Validator.validate({type: "integer"}, "23", parse_integer: false)
JSON::Validator.validate({type: "string"}, "123", parse_integer: false)
JSON::Validator.validate({type: "string"}, "123")
JSON::Validator.validate(schema, '{ "a": 1 }', :json => true)
begin
JSON::Validator.validate(schema, { "a" => 1 }, :json => true)
rescue TypeError => e
e.message
end
File.write("data.json", '{ "a": 1 }')
JSON::Validator.validate(schema, "data.json", :uri => true)
begin
JSON::Validator.validate(schema, { "a" => 1 }, :uri => true)
rescue TypeError => e
e.message
end
File.write("schema.json", v2_schema.to_json)
JSON::Validator.validate("schema.json", {})
File.write("schema.json", schema.to_json)
JSON::Validator.validate("schema.json", {}, :clear_cache => true)
JSON::Validator.validate("schema.json", {})
Extending Schemas
For this example, we are going to extend the JSON Schema Draft
3 specification by adding
a 'bitwise-and' property for validation.
require "json-schema"
class BitwiseAndAttribute < JSON::Schema::Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
if data.is_a?(Integer) && data & current_schema.schema['bitwise-and'].to_i == 0
message = "The property '#{build_fragment(fragments)}' did not evaluate to true when bitwise-AND'd with #{current_schema.schema['bitwise-or']}"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
end
end
end
class ExtendedSchema < JSON::Schema::Draft3
def initialize
super
@attributes["bitwise-and"] = BitwiseAndAttribute
@uri = JSON::Util::URI.parse("http://test.com/test.json")
@names = ["http://test.com/test.json"]
end
JSON::Validator.register_validator(self.new)
end
schema = {
"$schema" => "http://test.com/test.json",
"properties" => {
"a" => {
"bitwise-and" => 1
},
"b" => {
"type" => "string"
}
}
}
data = {
"a" => 0
}
data = {"a" => 1, "b" => "taco"}
JSON::Validator.validate(schema,data)
data = {"a" => 1, "b" => 5}
JSON::Validator.validate(schema,data)
data = {"a" => 0, "b" => "taco"}
JSON::Validator.validate(schema,data)
Custom format validation
The JSON schema standard allows custom formats in schema definitions which
should be ignored by validators that do not support them. JSON::Schema allows
registering procs as custom format validators which receive the value to be
checked as parameter and must raise a JSON::Schema::CustomFormatError
to
indicate a format violation. The error message will be prepended by the property
name, e.g. The property '#a'
require "json-schema"
format_proc = -> value {
raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42"
}
JSON::Validator.register_format_validator("the-answer", format_proc, ["draft4"])
JSON::Validator.register_format_validator("the-answer", format_proc)
JSON::Validator.deregister_format_validator('the-answer', ["draft4"])
JSON::Validator.restore_default_formats(["draft4"])
schema = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"properties" => {
"a" => {
"type" => "string",
"format" => "the-answer",
}
}
}
errors = JSON::Validator.fully_validate(schema, {"a" => "23"})
Validating a JSON Schema
To validate that a JSON Schema conforms to the JSON Schema standard,
you need to validate your schema against the metaschema for the appropriate
JSON Schema Draft. All of the normal validation methods can be used
for this. First retrieve the appropriate metaschema from the internal
cache (using JSON::Validator.validator_for_name()
or
JSON::Validator.validator_for_uri()
) and then simply validate your
schema against it.
require "json-schema"
schema = {
"type" => "object",
"properties" => {
"a" => {"type" => "integer"}
}
}
metaschema = JSON::Validator.validator_for_name("draft4").metaschema
JSON::Validator.validate(metaschema, schema)
Controlling Remote Schema Reading
In some cases, you may wish to prevent the JSON Schema library from making HTTP
calls or reading local files in order to resolve $ref
schemas. If you fully
control all schemas which should be used by validation, this could be
accomplished by registering all referenced schemas with the validator in
advance:
schema = JSON::Schema.new(some_schema_definition, Addressable::URI.parse('http://example.com/my-schema'))
JSON::Validator.add_schema(schema)
If more extensive control is necessary, the JSON::Schema::Reader
instance used
can be configured in a few ways:
JSON::Validator.schema_reader = JSON::Schema::Reader.new(:accept_uri => true, :accept_file => false)
schema_reader = JSON::Schema::Reader.new(
:accept_uri => proc { |uri| uri.host == 'my-website.com' }
)
JSON::Validator.validate(some_schema, some_object, :schema_reader => schema_reader)
The JSON::Schema::Reader
interface requires only an object which responds to
read(string)
and returns a JSON::Schema
instance. See the API
documentation
for more information.
JSON Backends
The JSON Schema library currently supports the json
and yajl-ruby
backend
JSON parsers. If either of these libraries are installed, they will be
automatically loaded and used to parse any JSON strings supplied by the user.
If more than one of the supported JSON backends are installed, the yajl-ruby
parser is used by default. This can be changed by issuing the following before
validation:
JSON::Validator.json_backend = :json
Optionally, the JSON Schema library supports using the MultiJSON library for
selecting JSON backends. If the MultiJSON library is installed, it will be
autoloaded.
Notes
The 'format' attribute is only validated for the following values:
- date-time
- date
- time
- ip-address (IPv4 address in draft1, draft2 and draft3)
- ipv4 (IPv4 address in draft4)
- ipv6
- uri
All other 'format' attribute values are simply checked to ensure the instance
value is of the correct datatype (e.g., an instance value is validated to be an
integer or a float in the case of 'utc-millisec').
Additionally, JSON::Validator does not handle any json hyperschema attributes.
Transfer Notice
This plugin was originally authored by Iain Beeston.
The maintainer preferred that Vox Pupuli take ownership of the module for future improvement and maintenance.
Existing pull requests and issues were transferred, please fork and continue to contribute here.
License
This gem is licensed under the MIT license.
Release information
To make a new release, please do:
- update the version in VERSION.yml
- Install gems with
bundle install --with release --path .vendor
- generate the changelog with
bundle exec rake changelog
- Check if the new version matches the closed issues/PRs in the changelog
- Create a PR with it
- After it got merged, push a tag. GitHub actions will do the actual release to rubygems and GitHub Packages