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 3. The project originally started as a fork of Constellation’s ruby-jsonschema project, but differences in the JSON schema draft versions implemented as well as assumptions made by the ruby-jsonschema library forced this to become a new project.
This project is far from complete but is in a somewhat usable state…
Usage
Install:
gem install json-schema
If downloading the git repo, build the gem and install it:
$ rake package
$ gem install pkg/json-schema-0.1.0.gem
require 'rubygems'
require 'json-schema'
JSON::Validator.validate('schema.json', 'data.json')
schema = {
"properties" => {
"a" => {"type" => "integer"}
}
}
data = {
"a" => 5
}
JSON::Validator.validate(schema, data)
data = {
"a" => "taco"
}
begin
JSON::Validator.validate2(schema, data)
rescue ValidationError
puts $!.
end
Currently implemented
The following core schema definitions are currently implemented:
- type
- properties
- patternProperties
- additionalProperties
- items
- additionalItems
- required
- dependencies
- minimum
- maximum
- exclusiveMinimum
- exclusiveMaximum
- minItems
- maxItems
- uniqueItems
- pattern
- minLength
- maxLength
- enum
- title
- description
- divisibleBy
- disallow
- extends
- id
- $ref (this implementation only follows slash-delimited fragment resolution)
Not implemented
The following core schema definitions are not implemented:
- default – This library aims to solely be a validator and does not modify an object it is validating.
- $schema – Support for this will come later, possibly with the ability to validate against other JSON Schema draft versions
In addition, the following hyper schema attributes are not implemented at this time:
- links (not much to do with validation…)
- fragmentResolution (only handles default slash-delimited)
- readonly
- contentEncoding
- pathStart
- mediaType
To Do
- (Much) More testing
- Adding a persistence option to schemas that enables the validator to only load schemas once across multiple validations