is_hash_valid 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b8bbbdef9cccd7f6cd83e79af707b7c3b8ec73c5
4
+ data.tar.gz: b117e808c72081f7750c337b7883324f81491051
5
+ SHA512:
6
+ metadata.gz: 822ecd1710f9bbb776f8835aee77aa5558563141bfa88b960ceccf90de8aa2f9a7b3eb21e8849b6247279b11775ca17a5e87522f98bfe6f56b30119f0a63d240
7
+ data.tar.gz: 728d64354fb8f0b97b3bd71a5da012d4268c5b567ceb0af204d83a3e1515f9c1326cabca0aa5054ff7b55f1bc096e526323989a8e30ad82b282cf12fc2964ae4
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in is_hash_valid.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ben Janecke
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # IsHashValid
2
+
3
+ A simple hash validator that allows you to do complex validations with custom error messages.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'is_hash_valid'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install is_hash_valid
20
+
21
+ ## Usage
22
+
23
+ ### Roll your own validator
24
+
25
+ ```ruby
26
+ class SnobbyValidator < IsHashValid::Validator
27
+ def initialize(hash)
28
+ super hash {
29
+ name: {
30
+ be_royal: "Must be of royalty"
31
+ },
32
+ age: {
33
+ over18: "No minors allowed"
34
+ }
35
+ }
36
+ end
37
+
38
+ def over18(value)
39
+ value > 18
40
+ end
41
+
42
+ def be_royal(value)
43
+ value.scan(/King|Queen/).empty? ? false : true
44
+ end
45
+ end
46
+
47
+ valid = SnobbyValidator.new({ name: 'King Richard', age: 9001 })
48
+ invalid = SnobbyValidator.new({ name: 'Timmy', age: 8 })
49
+
50
+ valid.valid? #true
51
+ valid.errors # {}
52
+
53
+ invalid.valid? #false
54
+ invalid.errors # { errors: { name: [ "Must be of royalty "], age: ["No minors allowed"] } }
55
+
56
+ ```
57
+
58
+ ### Or use some of the baked in validators
59
+
60
+ ```ruby
61
+ invalid = IsHashValid::Validator.new({ name: 2 }, {
62
+ name: {
63
+ not_empty: "Name may not be empty",
64
+ be_string: "Name must be a string"
65
+ }
66
+ })
67
+
68
+ invalid.valid? #false
69
+ invalid.errors # { errors: { name: [ "Name may not be empty", "Name must be a string" ] } }
70
+ ```
71
+
72
+ Current baked in validators are
73
+
74
+ * not_empty
75
+ * be_string
76
+ * required
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( https://github.com/[my-github-username]/is_hash_valid/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'is_hash_valid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "is_hash_valid"
8
+ spec.version = IsHashValid::VERSION
9
+ spec.authors = ["Ben Janecke"]
10
+ spec.email = ["benjanecke@gmail.com"]
11
+ spec.summary = %q{A simple hash validator.}
12
+ spec.description = %q{A simple hash validator that allows you to do complex validations with custom error messages.}
13
+ spec.homepage = "https://github.com/BenJanecke/is_hash_valid"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2.0"
24
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'validators'
2
+
3
+ module IsHashValid
4
+ class Validator
5
+ include IsHashValid::Validators
6
+
7
+ def initialize(hash, validators)
8
+ @hash = hash
9
+ @validators = validators
10
+ @errors = { }
11
+ validate
12
+ end
13
+
14
+ def valid?
15
+ @errors.empty? ? true : false
16
+ end
17
+
18
+ def errors
19
+ { errors: @errors }
20
+ end
21
+
22
+ def validate
23
+ @validators.each do |field, validators|
24
+ validators.each do |validator, message|
25
+ if !self.send(validator.to_sym, @hash[field.to_sym])
26
+ @errors[field] = [] unless @errors.has_key? field
27
+ @errors[field].push(message)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ module IsHashValid
2
+ module Validators
3
+ def required(value)
4
+ value ? true : false
5
+ end
6
+
7
+ def not_empty(value)
8
+ return false unless value.is_a? String
9
+ value.empty? ? false : true
10
+ end
11
+
12
+ def be_string(value)
13
+ value.is_a? String
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module IsHashValid
2
+ VERSION = "1.1.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "is_hash_valid/version"
2
+ require "is_hash_valid/validator"
3
+ require "is_hash_valid/validators"
4
+
5
+ module IsHashValid
6
+ end
@@ -0,0 +1,91 @@
1
+ require "spec_helper"
2
+ require "./lib/is_hash_valid/validator"
3
+
4
+ describe IsHashValid::Validator do
5
+ subject { IsHashValid::Validator.new({}, {}) }
6
+
7
+ describe "#initialize" do
8
+ it "expects @hash to be set" do
9
+ expect(subject.instance_variable_get(:@hash)).to eq({})
10
+ end
11
+
12
+ it "expects @validators to be set" do
13
+ expect(subject.instance_variable_get(:@validators)).to eq({})
14
+ end
15
+ end
16
+
17
+ describe "#valid?" do
18
+ it "expects true by default" do
19
+ expect(subject.valid?).to eq(true)
20
+ end
21
+ end
22
+
23
+ describe "#errors" do
24
+ it "expects an empty hash by default" do
25
+ expect(subject.errors).to eq({ errors: {} })
26
+ end
27
+ end
28
+
29
+ describe "given a hash with a name attribute that may not be empty" do
30
+ describe "when name is empty" do
31
+ subject do
32
+ IsHashValid::Validator.new({ name: "" }, {
33
+ name: {
34
+ not_empty: "Name may not be empty"
35
+ }
36
+ })
37
+ end
38
+
39
+ describe "#valid?" do
40
+ it { expect(subject.valid?).to eq(false) }
41
+ end
42
+
43
+ describe "#errors" do
44
+ it { expect(subject.errors).to eq({ errors: { name: [ "Name may not be empty" ] } }) }
45
+ end
46
+ end
47
+
48
+ describe "when name is not empty" do
49
+ subject do
50
+ IsHashValid::Validator.new({ name: "I'm not empty :D" }, {
51
+ name: {
52
+ not_empty: 'Name may not be empty'
53
+ }
54
+ })
55
+ end
56
+
57
+ describe "#valid?" do
58
+ it { expect(subject.valid?).to eq(true) }
59
+ end
60
+
61
+ describe "#errors" do
62
+ it { expect(subject.errors).to eq({ errors: { } }) }
63
+ end
64
+
65
+ end
66
+ end
67
+
68
+ describe "given a subclass with a custom validator" do
69
+ class SubclassWithCustomValidator < IsHashValid::Validator
70
+ def be_royal(value)
71
+ value.scan(/King|Queen/).empty? ? false : true
72
+ end
73
+ end
74
+
75
+ describe "when I run 2 validators on name that both fail" do
76
+ subject do
77
+ SubclassWithCustomValidator.new({ name: "" }, {
78
+ name: {
79
+ not_empty: "Name may not be empty",
80
+ be_royal: "Name must be royal"
81
+ }
82
+ })
83
+ end
84
+
85
+ describe "#errors" do
86
+ it { expect(subject.errors).to eq({ errors: { name: [ "Name may not be empty", "Name must be royal" ] } }) }
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+ require "./lib/is_hash_valid/validators"
3
+
4
+ describe "Validators" do
5
+ subject { class ValidatorsSpec; include IsHashValid::Validators end.new }
6
+
7
+ describe "required" do
8
+ describe "given a empty string" do
9
+ it { expect(subject.required("")).to eq(true) }
10
+ end
11
+
12
+ describe "given nil" do
13
+ it { expect(subject.required(nil)).to eq(false) }
14
+ end
15
+ end
16
+
17
+ describe "not_empty" do
18
+ describe "given a empty string" do
19
+ it { expect(subject.not_empty("")).to eq(false) }
20
+ end
21
+
22
+ describe "given a string with a value" do
23
+ it { expect(subject.not_empty("I'm not empty!")).to eq(true) }
24
+ end
25
+ end
26
+
27
+ describe "be_string" do
28
+ describe "given a empty string" do
29
+ it { expect(subject.be_string("")).to eq(true) }
30
+ end
31
+
32
+ describe "given a integer" do
33
+ it { expect(subject.be_string(2)).to eq(false) }
34
+ end
35
+
36
+ describe "given a bool" do
37
+ it { expect(subject.be_string(true)).to eq(false) }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require './lib/is_hash_valid/version.rb'
3
+
4
+ describe "Version" do
5
+ it { expect(IsHashValid::VERSION).to eq("1.1.1") }
6
+ end
@@ -0,0 +1,2 @@
1
+ RSpec.configure do |config|
2
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is_hash_valid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Janecke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ description: A simple hash validator that allows you to do complex validations with
56
+ custom error messages.
57
+ email:
58
+ - benjanecke@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - is_hash_valid.gemspec
70
+ - lib/is_hash_valid.rb
71
+ - lib/is_hash_valid/validator.rb
72
+ - lib/is_hash_valid/validators.rb
73
+ - lib/is_hash_valid/version.rb
74
+ - spec/is_hash_valid/validator_spec.rb
75
+ - spec/is_hash_valid/validators_spec.rb
76
+ - spec/is_hash_valid/version_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: https://github.com/BenJanecke/is_hash_valid
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A simple hash validator.
102
+ test_files:
103
+ - spec/is_hash_valid/validator_spec.rb
104
+ - spec/is_hash_valid/validators_spec.rb
105
+ - spec/is_hash_valid/version_spec.rb
106
+ - spec/spec_helper.rb