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.
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.6.gem
require 'rubygems' require 'json-schema' JSON::Validator.validate('schema.json', 'data.json') schema = { "type" => "object", "properties" => { "a" => {"type" => "integer", "required" => true} } } data = { "a" => 5 } JSON::Validator.validate(schema, data) data = [data,data,data] JSON::Validator.validate(schema, data, :list => true) data = { "a" => "taco" } begin JSON::Validator.validate2(schema, data) rescue ValidationError puts $!.message end
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)
The following core schema definitions are not implemented:
- format – Some of the formats listed will be supported, others are fairly vague in their definition and may be left out
- 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
- fragmentResolution (only handles default slash-delimited)
- contentEncoding
- pathStart
- (Much) More testing
- Documentation
- Breaking the current validator out into a subclass, acting as the foundation for supporting multiple versions of validators