jddf-ruby
Documentation on rubydoc.info: https://www.rubydoc.info/github/jddf/jddf-ruby
This gem is a Ruby implementation of JSON Data Definition Format, a
schema language for JSON. You can use this gem to:
- Validate input data against a schema,
- Get a list of validation errors from that input data, or
- Build your own tooling on top of JSON Data Definition Format
Installing
You can install this gem by running:
gem install jddf
Or if you're using Bundler:
gem 'jddf'
Usage
The two most important classes offered by the JDDF
module are:
Schema
, which represents a JDDF schema,Validator
, which can validate a Schema
against any parsed
JSON data, andValidationError
, which represents a single validation
problem with the input. Validator#validate
returns an array of these.
Here's a working example:
require 'jddf'
schema = JDDF::Schema.from_json({
'properties' => {
'name' => { 'type' => 'string' },
'age' => { 'type' => 'uint32' },
'phones' => {
'elements' => { 'type' => 'string' }
}
}
})
input_ok = {
'name' => 'John Doe',
'age' => 43,
'phones' => [
'+44 1234567',
'+44 2345678'
]
}
input_bad = {
'age' => '43',
'phones' => [
'+44 1234567',
442345678
]
}
validator = JDDF::Validator.new
result_ok = validator.validate(schema, input_ok)
result_bad = validator.validate(schema, input_bad)
p result_ok.size
p result_bad.size
p result_bad[0]
p result_bad[1]
p result_bad[2]