hashidator 0.3.1 → 0.4.0

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: 083c986d0c9f200a3780b92590eba1a6c5a3bc19
4
+ data.tar.gz: f43f7f87b611a0b448e7e5962bf75c1cbe015d86
5
+ SHA512:
6
+ metadata.gz: 3410c9606685bd160689050acb9bc1826ccab7931eeb5db74f5da1bb6d426b2cb001b53453579d2b7a6f6c6b7381f258fef60347a981a704459505fe4d66a1c4
7
+ data.tar.gz: 8b43927c3a5ad3d8e6676f00b44b05103265016e7a11ade494359f55e2f157cea72ee9091010f9933c20225945f6470493994fede79599f6969e9b2a2d97d450
data/hashidator.gemspec CHANGED
@@ -1,10 +1,12 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'hashidator/version'
3
+
1
4
  Gem::Specification.new do |s|
2
5
  s.name = "hashidator"
3
- s.version = "0.3.1"
4
- s.date = "2010-10-05"
6
+ s.version = Hashidator::VERSION
5
7
  s.summary = "define schemas as a hash, and validate hashes!"
6
8
  s.email = "harry@vangberg.name"
7
- s.homepage = "http://github.com/ichverstehe/hashidator"
9
+ s.homepage = "https://github.com/vangberg/hashidator"
8
10
  s.description = "define schemas as a hash, and validate hashes!"
9
11
  s.has_rdoc = false
10
12
  s.authors = ["Harry Vangberg", "Peter Suschlik"]
@@ -12,7 +14,8 @@ Gem::Specification.new do |s|
12
14
  "README",
13
15
  "TODO",
14
16
  "hashidator.gemspec",
15
- "lib/hashidator.rb"
17
+ "lib/hashidator.rb",
18
+ "lib/hashidator/version.rb"
16
19
  ]
17
20
  s.test_files = [
18
21
  "test/helper.rb",
@@ -0,0 +1,3 @@
1
+ class Hashidator
2
+ VERSION = "0.4.0"
3
+ end
data/lib/hashidator.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  class Hashidator
2
+ autoload :VERSION, 'hashidator/version'
3
+
2
4
  def self.validate(schema, input)
3
5
  new(schema).validate(input)
4
6
  end
@@ -20,20 +22,15 @@ class Hashidator
20
22
 
21
23
  def validate_value(validator, value)
22
24
  case validator
23
- when Range
24
- validator.include? value
25
25
  when Array
26
26
  value.respond_to?(:all?) && value.all? {|x| validate_value(validator[0], x)}
27
27
  when Symbol
28
28
  value.respond_to? validator
29
- when Regexp
30
- validator.match value.to_s
31
29
  when Hash
32
- Hashidator.validate(validator, value)
33
- when Class, Module
34
- value.is_a? validator
35
- when Proc
36
- result = validator.call(value)
30
+ self.class.validate(validator, value)
31
+ else
32
+ result = validator == value
33
+ result = validator === value unless result
37
34
  result = validate_value(result, value) unless Boolean === result
38
35
  result
39
36
  end
@@ -50,3 +47,7 @@ end
50
47
  class FalseClass
51
48
  include Boolean
52
49
  end
50
+
51
+ class Proc
52
+ alias :=== :call if RUBY_VERSION < "1.9"
53
+ end
@@ -52,7 +52,6 @@ class TestHashidator < Test::Unit::TestCase
52
52
 
53
53
  def test_validate_boolean
54
54
  assert_true h({:admin => Boolean}, {:admin => true})
55
- assert_true h({:admin => Boolean}, {:admin => true})
56
55
  end
57
56
 
58
57
  def test_invalidate_boolean
@@ -69,6 +68,17 @@ class TestHashidator < Test::Unit::TestCase
69
68
  assert_false h({:uri => /^http:/}, {:uri => :symbol})
70
69
  end
71
70
 
71
+ def test_validate_exact_values
72
+ assert_true h({:result => "ok"}, {:result => "ok"})
73
+ assert_true h({:bool => true}, {:bool => true})
74
+ end
75
+
76
+ def test_invalidate_exact_values
77
+ assert_false h({:result => "ok"}, {:result => "error"})
78
+ assert_false h({:result => "ok"}, {:result => nil})
79
+ assert_false h({:bool => false}, {:bool => true})
80
+ end
81
+
72
82
  def test_validate_respond_to
73
83
  assert_true h({:name => :to_s}, {:name => "Harry"})
74
84
  end
@@ -124,6 +134,25 @@ class TestHashidator < Test::Unit::TestCase
124
134
  assert_false h(schema, {:methods => ["Harry", 2]})
125
135
  end
126
136
 
137
+ def test_validation_with_equality
138
+ optional = Class.new do
139
+ def initialize(schema)
140
+ @schema = schema
141
+ end
142
+
143
+ def ===(other)
144
+ other.nil? || @schema
145
+ end
146
+ end
147
+
148
+ schema = {:phone => optional.new(String)}
149
+ assert_true h(schema, {:phone => "+49 2305 4711"})
150
+ assert_true h(schema, {:phone => nil})
151
+ assert_true h(schema, {})
152
+ assert_false h(schema, {:phone => false})
153
+ assert_false h(schema, {:phone => 1234567})
154
+ end
155
+
127
156
  def test_validate_nested
128
157
  schema = {:name => {:first => String, :last => String}}
129
158
  assert_true h(schema, {:name => {:first => "Mike", :last => "Damone"}})
@@ -148,6 +177,22 @@ class TestHashidator < Test::Unit::TestCase
148
177
  )
149
178
  end
150
179
 
180
+ def test_deep_nested_validation_uses_subclass
181
+ h = Class.new(Hashidator) do
182
+ def validate(input)
183
+ throw :halt, "#{input[:value]} HAMMERZEIT" if input[:value] == 'STOP'
184
+ super
185
+ end
186
+ end
187
+
188
+ caught = catch(:halt) do
189
+ h.validate(
190
+ {:deep => { :value => String}},
191
+ {:deep => { :value => 'STOP'}})
192
+ end
193
+ assert_equal "STOP HAMMERZEIT", caught
194
+ end
195
+
151
196
  def test_invalidate_for_nil_input
152
197
  schema = {:user => { :name => String }}
153
198
  assert_false h(schema, nil)
metadata CHANGED
@@ -1,74 +1,53 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: hashidator
3
- version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Harry Vangberg
14
8
  - Peter Suschlik
15
9
  autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
-
19
- date: 2010-10-05 00:00:00 +02:00
20
- default_executable:
12
+ date: 2016-03-07 00:00:00.000000000 Z
21
13
  dependencies: []
22
-
23
14
  description: define schemas as a hash, and validate hashes!
24
15
  email: harry@vangberg.name
25
16
  executables: []
26
-
27
17
  extensions: []
28
-
29
18
  extra_rdoc_files: []
30
-
31
- files:
19
+ files:
32
20
  - README
33
21
  - TODO
34
22
  - hashidator.gemspec
35
23
  - lib/hashidator.rb
24
+ - lib/hashidator/version.rb
36
25
  - test/helper.rb
37
26
  - test/test_hashidator.rb
38
- has_rdoc: true
39
- homepage: http://github.com/ichverstehe/hashidator
27
+ homepage: https://github.com/vangberg/hashidator
40
28
  licenses: []
41
-
29
+ metadata: {}
42
30
  post_install_message:
43
31
  rdoc_options: []
44
-
45
- require_paths:
32
+ require_paths:
46
33
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
48
- none: false
49
- requirements:
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
50
36
  - - ">="
51
- - !ruby/object:Gem::Version
52
- hash: 3
53
- segments:
54
- - 0
55
- version: "0"
56
- required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
59
41
  - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 3
62
- segments:
63
- - 0
64
- version: "0"
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
65
44
  requirements: []
66
-
67
45
  rubyforge_project:
68
- rubygems_version: 1.3.7
46
+ rubygems_version: 2.5.1
69
47
  signing_key:
70
- specification_version: 3
48
+ specification_version: 4
71
49
  summary: define schemas as a hash, and validate hashes!
72
- test_files:
50
+ test_files:
73
51
  - test/helper.rb
74
52
  - test/test_hashidator.rb
53
+ has_rdoc: false