Project

jddf

0.0
No commit activity in last 3 years
No release in over 3 years
JSON Data Definition Format validation for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.17
~> 10.0
~> 3.0
~> 0.75.0
 Project Readme

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:

  1. Validate input data against a schema,
  2. Get a list of validation errors from that input data, or
  3. 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, and
  • ValidationError, which represents a single validation problem with the input. Validator#validate returns an array of these.

Here's a working example:

require 'jddf'

# In this example, we're passing in a Hash directly into Schema#from_json, but
# this type of Hash is exactly what JSON#parse returns.
schema = JDDF::Schema.from_json({
  'properties' => {
    'name' => { 'type' => 'string' },
    'age' => { 'type' => 'uint32' },
    'phones' => {
      'elements' => { 'type' => 'string' }
    }
  }
})

# Like before, in order to keep things simple we're construct raw Ruby values
# here. But you can also get this sort of data by parsing JSON using the
# standard library's JSON#parse.
#
# This input data is perfect. It satisfies all the schema requirements.
input_ok = {
  'name' => 'John Doe',
  'age' => 43,
  'phones' => [
    '+44 1234567',
    '+44 2345678'
  ]
}

# This input data has problems. "name" is missing, "age" has the wrong type,
# and "phones[1]" has the wrong type.
input_bad = {
  'age' => '43',
  'phones' => [
    '+44 1234567',
    442345678
  ]
}

# Validator can validate schemas against inputs. Validator#validate returns an
# array of ValidationError.
#
# These ValidationError instances are portable -- every implementation of JDDF,
# across every language, returns the same errors.
validator = JDDF::Validator.new
result_ok = validator.validate(schema, input_ok)
result_bad = validator.validate(schema, input_bad)

p result_ok.size  # 0
p result_bad.size # 3

# This error indicates that "name" is missing.
#
# #<struct JDDF::ValidationError instance_path=[], schema_path=["properties", "name"]
p result_bad[0]

# This error indicates that "age" has the wrong type.
#
# #<struct JDDF::ValidationError instance_path=["age"], schema_path=["properties", "age", "type"]>
p result_bad[1]

# This error indicates that "phones[1]" has the wrong type.
#
# #<struct JDDF::ValidationError instance_path=["phones", "1"], schema_path=["properties", "phones", "elements", "type"]>
p result_bad[2]