0.0
No commit activity in last 3 years
No release in over 3 years
JsonValidation validates JSON according to a JSON Schema.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 5.0
~> 10.0

Runtime

 Project Readme

JsonValidation

JsonValidation validates JSON according to a JSON Schema.

It is designed to validate as quickly as possible.

Current limitations

  • The library can only say whether a record is valid or not. In the event that a record is not valid, it will not say why.
  • The library will only validate against draft v4 of the JSON Schema specification.

Installation

Add this line to your application's Gemfile:

gem 'json_validation'

And then execute:

$ bundle

Or install it yourself as:

$ gem install json_validation

Usage

Load the library:

require "json_validation"

Build a validator from a hash representing a JSON schema:

validator = JsonValidation.build_validator(schema)

Load a validator from a schema at a URL or path:

validator = JsonValidation.load_validator(uri)

Use the validator to validate whether a record validates against a schema.

validator.validate(record)
# => returns true or false

Format validation

To validate a record against a format, define a validator class with a #validate method:

class DateTimeFormatValidator
  def validate(record)
    Date.parse(record)
    true
  rescue ArgumentError
    false
  end
end

Then, register the validator:

JsonValidation.add_format_validator("date-time", DateTimeFormatValidator)

Contributing

If you find an example of a record being incorrectly validated against a schema, please raise an issue on GitHub.

You can run the tests with:

$ bundle exec rake test

A handful of tests require internet access.