0.01
No commit activity in last 3 years
No release in over 3 years
a gem to check whether given to hashes are of the same format
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
>= 0
 Project Readme

Build Status Code Climate

HashPolice

A gem to check whether given to hashes are of the same format

Installation

Add this line to your application's Gemfile:

gem 'hash_police'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_police

Usage

rule = {
  :name => "a string",
  :age => 28,
  :favorites => [ "a string" ],
  :locations => [
    { :name => "string", :duration => 3 }
  ]
}

valid = {
  :name => "Jack",
  :age => 28,
  :favorites => [ "sport", "music" ],
  :locations => [
    { :name => "Taiwan", :duration => 25 },
    { :name => "US", :duration => 5 }
  ]
}

invalid = {
  :name => [],
  :age => "not a number",
  :locations => [
    { :name => "Taiwan", :duration => 25 },
    { :name => 23 }
  ]
}

police = HashPolice::Police.new(rule)

result = police.check(valid)
result.passed? # => true
result.error_messages # => ""

result = police.check(invalid)
result.passed? # => false
result.error_messages #=> "`name`: expect String, got Array; `favorites`: missing; `locations.1.name`: expect String, got Array; `locations.1.duration`: missing"

RSpec matcher:

HashPolice provides a RSpec matcher have_the_same_hash_format_as checking the formats of two hashes.

  require 'hash_police/rspec_matcher'

  it "should be able to use the matcher `have_the_same_hash_format_as`" do
    expected = { 'str' => '', 'an_arr' => [ 1 ] }
    to_be_checked = { 'str' => 'hola', :an_arr => [1,3,4] }

    expect(to_be_checked).to have_the_same_hash_format_as(expected)
  end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request