Small library for validating hal+json
Latest commit 23a2d09 Nov 18, 2013 @deathbob Fixes bug with _embedded

Small library for validating hal+json

Usage with bundler

gem 'halidator'

Usage stand-alone

gem install halidator
require 'halidator'

A tiny example

require 'halidator'
require 'open-uri'

puts "Halidator will try and validate a string, if that's what you give it"
string = open("").read
puts "Valid = #{}"

puts "Halidator also will accept the result of JSON.parse"
json = JSON.parse(string)
puts "Valid = #{}"

The default validator is a pure ruby implementation that is slightly more strict than the spec (Specifically, the default validator requires you to have a self link. This is not technically part of the spec, because everybody wants HAL to be usable as a response to non-GET requests)

You can use a json-schema validator by passing an optional argument. Many thanks to Isaac from the hal-discuss google group for providing this very comprehensive alternate validator for HAL.

puts "Valid = #{, :json_schema).valid?}"
puts "Valid = #{, :json_schema).valid?}"

There is also a executable included so after you install the gem you can do things like

 > curl -s | halidate

Get help with

 >  halidate -h
Usage: halidate [options]
    -j, --json-schema                Validate using the HAL JSON schema
    -h, --help                       Output this help and exit
    -d, --debug                      Output debugging info

If there are errors in the string you pipe to halidate, it will print them on stderr

 > curl -s | halidate
no self in {'_links' => {'pickles' => 'pie'}}

If that's inconvenient, just redirect stderr somewhere you won't see it.

 > curl -s | halidate 2>/dev/null

Thanks to:

Pull requests welcome, especially if you're adding XML support!