Project

halidator

0.01
No commit activity in last 3 years
No release in over 3 years
A small library to validate hal+json
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 1.6.0
>= 1.0.0
 Project Readme

Small library for validating hal+json

Usage with bundler

gem 'halidator'

Usage stand-alone

gem install halidator
irb
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("http://haltalk.herokuapp.com/users/mike/posts.json").read
puts "Valid = #{Halidator.new(string).valid?}"

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

The default validator is a pure ruby implementation that is slightly more strict than the spec http://tools.ietf.org/html/draft-kelly-json-hal-03. (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 https://github.com/DaveJS/dave.schema.json/blob/master/hal.json 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 = #{Halidator.new(string, :json_schema).valid?}"
puts "Valid = #{Halidator.new(json, :json_schema).valid?}"

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

 > curl -s http://haltalk.herokuapp.com/posts/latest.json | halidate
true

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 http://example.com/bad-hal.json | halidate
false
no self in {'_links' => {'pickles' => 'pie'}}

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

 > curl -s http://example.com/bad-hal.json | halidate 2>/dev/null
false

Thanks to:


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