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.
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.
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.
From rubygems.org:
gem install json-schemaFrom the git repo:
gem build json-schema.gemspec
gem install json-schema-*.gemThree 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 completeAll 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.
……For this example, we are going to extend the JSON Schema Draft 3 specification by adding a 'bitwise-and' property for 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'
…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
# => true
JSON::Validator.validate(metaschema, schema)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:
# Change the default schema reader used
JSON::Validator.schema_reader = JSON::Schema::Reader.new(:accept_uri => true, :accept_file => false)
# For this validation call, use a reader which only accepts URIs from my-website.com
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.
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 = :jsonOptionally, the JSON Schema library supports using the MultiJSON library for selecting JSON backends. If the MultiJSON library is installed, it will be autoloaded.
Deprecation notice: MultiJSON support is deprecated and will be removed in a future version. To stop using MultiJSON, add the following to your application's initialization code:
JSON::Validator.use_multi_json = falseThe 'format' attribute is only validated for the following values:
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.
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.
This gem is licensed under the MIT license.
To make a new release, please do:
bundle install --with release --path .vendorbundle exec rake changelogNo open issues yet, or sync has not completed.