Git Product home page Git Product logo

halidator's Introduction

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!

halidator's People

Contributors

deathbob avatar tjlytle avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.