hashidator 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -11,7 +11,7 @@ define schemas as a hash, and validate hashes!
11
11
  h.validate(invalid) #=> false
12
12
 
13
13
  see examples/basic.rb for full coverage. it knows
14
- classes, ranges, booleans and duck typing. yay!
14
+ classes, ranges, booleans, proc and duck typing. yay!
15
15
 
16
16
  (c) 2009 harry vangberg <harry@vangberg.name>,
17
17
  distributed under the mit license. google it.
data/hashidator.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "hashidator"
3
- s.version = "0.2"
4
- s.date = "2009-12-19"
3
+ s.version = "0.3"
4
+ s.date = "2010-02-03"
5
5
  s.summary = "define schemas as a hash, and validate hashes!"
6
6
  s.email = "harry@vangberg.name"
7
7
  s.homepage = "http://github.com/ichverstehe/hashidator"
data/lib/hashidator.rb CHANGED
@@ -1,21 +1,18 @@
1
1
  class Hashidator
2
2
  def self.validate(schema, input)
3
- hd = new(schema)
4
- hd.validate(input)
3
+ new(schema).validate(input)
5
4
  end
6
5
 
7
- attr_accessor :schema, :errors
6
+ attr_accessor :schema
8
7
 
9
8
  def initialize(schema)
10
9
  @schema = schema
11
10
  end
12
11
 
13
12
  def validate(input)
14
- results = schema.map { |key, validator|
13
+ schema.all? {|key, validator|
15
14
  validate_value(validator, input[key])
16
15
  }
17
-
18
- results.all?
19
16
  end
20
17
 
21
18
  private
@@ -25,7 +22,7 @@ class Hashidator
25
22
  when Range
26
23
  validator.include? value
27
24
  when Array
28
- value.all? {|x| validate_value(validator[0], x) }
25
+ value.all? {|x| validate_value(validator[0], x)}
29
26
  when Symbol
30
27
  value.respond_to? validator
31
28
  when Regexp
@@ -34,6 +31,10 @@ class Hashidator
34
31
  Hashidator.validate(validator, value)
35
32
  when Class, Module
36
33
  value.is_a? validator
34
+ when Proc
35
+ result = validator.call(value)
36
+ result = validate_value(result, value) unless Boolean === result
37
+ result
37
38
  end
38
39
  end
39
40
  end
data/test/helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  $:.unshift "lib"
2
2
 
3
3
  require 'test/unit'
4
- require 'contest'
5
4
  require 'hashidator'
6
5
 
7
6
  class Test::Unit::TestCase
@@ -36,6 +36,10 @@ class TestHashidator < Test::Unit::TestCase
36
36
  assert_true h({:children => [String]}, {:children => ["Sue", "Mike"]})
37
37
  end
38
38
 
39
+ def test_validate_empty_array_members
40
+ assert_true h({:children => [String]}, {:children => []})
41
+ end
42
+
39
43
  def test_invalidate_array_members
40
44
  assert_false h({:children => [String]}, {:children => ["Sue", 1234]})
41
45
  end
@@ -86,6 +90,32 @@ class TestHashidator < Test::Unit::TestCase
86
90
  )
87
91
  end
88
92
 
93
+ def test_validate_proc_with_boolean_result
94
+ assert_true h(
95
+ {:methods => proc {|v| v.all? {|e| e.is_a?(String) || e.is_a?(Symbol) } }},
96
+ {:methods => ["to_s", :to_h]}
97
+ )
98
+ end
99
+
100
+ def test_invalidate_proc_with_boolean_result
101
+ assert_false h(
102
+ {:methods => proc {|v| v.all? {|e| e.is_a?(String) || e.is_a?(Symbol) } }},
103
+ {:methods => ["to_s", 123]}
104
+ )
105
+ end
106
+
107
+ def test_validate_proc_with_cascaded_result
108
+ schema = {:methods => proc {|v| v.first.is_a?(Integer) ? [Integer] : [String] } }
109
+ assert_true h(schema, {:methods => [1, 2]})
110
+ assert_true h(schema, {:methods => ["Harry", "Damone"]})
111
+ end
112
+
113
+ def test_invalidate_proc_with_cascaded_result
114
+ schema = {:methods => proc {|v| v.first.is_a?(Integer) ? [Integer] : [String] } }
115
+ assert_false h(schema, {:methods => [1, "Damone"]})
116
+ assert_false h(schema, {:methods => ["Harry", 2]})
117
+ end
118
+
89
119
  def test_validate_nested
90
120
  schema = {:name => {:first => String, :last => String}}
91
121
  assert_true h(schema, {:name => {:first => "Mike", :last => "Damone"}})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashidator
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Vangberg
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-19 00:00:00 +01:00
12
+ date: 2010-02-03 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  requirements: []
51
51
 
52
52
  rubyforge_project:
53
- rubygems_version: 1.3.5
53
+ rubygems_version: 1.3.3
54
54
  signing_key:
55
55
  specification_version: 3
56
56
  summary: define schemas as a hash, and validate hashes!