hashidator 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/hashidator.gemspec +2 -2
- data/lib/hashidator.rb +21 -21
- data/test/test_hashidator.rb +29 -0
- metadata +2 -2
data/hashidator.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "hashidator"
|
3
|
-
s.version = "0.
|
4
|
-
s.date = "2009-12-
|
3
|
+
s.version = "0.2"
|
4
|
+
s.date = "2009-12-19"
|
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
@@ -11,31 +11,31 @@ class Hashidator
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def validate(input)
|
14
|
-
results =
|
15
|
-
|
16
|
-
schema.each { |key, validator|
|
17
|
-
value = input[key]
|
18
|
-
|
19
|
-
results << case validator
|
20
|
-
when Range;
|
21
|
-
validator.include? value
|
22
|
-
when Array;
|
23
|
-
if validator[0].is_a? Symbol
|
24
|
-
value.respond_to? validator[0]
|
25
|
-
else
|
26
|
-
value.all? {|x| x.is_a? validator[0]}
|
27
|
-
end
|
28
|
-
when Symbol;
|
29
|
-
value.respond_to? validator
|
30
|
-
when Hash;
|
31
|
-
Hashidator.validate(validator, value)
|
32
|
-
when Class, Module;
|
33
|
-
value.is_a? validator
|
34
|
-
end
|
14
|
+
results = schema.map { |key, validator|
|
15
|
+
validate_value(validator, input[key])
|
35
16
|
}
|
36
17
|
|
37
18
|
results.all?
|
38
19
|
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def validate_value(validator, value)
|
24
|
+
case validator
|
25
|
+
when Range
|
26
|
+
validator.include? value
|
27
|
+
when Array
|
28
|
+
value.all? {|x| validate_value(validator[0], x) }
|
29
|
+
when Symbol
|
30
|
+
value.respond_to? validator
|
31
|
+
when Regexp
|
32
|
+
value.match validator
|
33
|
+
when Hash
|
34
|
+
Hashidator.validate(validator, value)
|
35
|
+
when Class, Module
|
36
|
+
value.is_a? validator
|
37
|
+
end
|
38
|
+
end
|
39
39
|
end
|
40
40
|
|
41
41
|
module Boolean
|
data/test/test_hashidator.rb
CHANGED
@@ -49,6 +49,14 @@ class TestHashidator < Test::Unit::TestCase
|
|
49
49
|
assert_false h({:admin => Boolean}, {:admin => 1234})
|
50
50
|
end
|
51
51
|
|
52
|
+
def test_validate_regexp
|
53
|
+
assert_true h({:uri => /^http:/}, {:uri => "http://example.com"})
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_invalidate_regexp
|
57
|
+
assert_false h({:uri => /^http:/}, {:uri => "john coltrane"})
|
58
|
+
end
|
59
|
+
|
52
60
|
def test_validate_respond_to
|
53
61
|
assert_true h({:name => :to_s}, {:name => "Harry"})
|
54
62
|
end
|
@@ -71,6 +79,13 @@ class TestHashidator < Test::Unit::TestCase
|
|
71
79
|
)
|
72
80
|
end
|
73
81
|
|
82
|
+
def test_validate_array_members_range
|
83
|
+
assert_true h(
|
84
|
+
{:names => [(1..9)]},
|
85
|
+
{:names => [1, 9]}
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
74
89
|
def test_validate_nested
|
75
90
|
schema = {:name => {:first => String, :last => String}}
|
76
91
|
assert_true h(schema, {:name => {:first => "Mike", :last => "Damone"}})
|
@@ -80,4 +95,18 @@ class TestHashidator < Test::Unit::TestCase
|
|
80
95
|
schema = {:name => {:first => String, :last => String}}
|
81
96
|
assert_false h(schema, {:name => {:first => "Mike", :last => 1234}})
|
82
97
|
end
|
98
|
+
|
99
|
+
def test_validate_deep_nested
|
100
|
+
assert_true h(
|
101
|
+
{:people => [{ :name => :to_s, :age => (18..40)}]},
|
102
|
+
{:people => [{ :name => "Ann", :age => 19 }, { :name => "Bob", :age => 23 }]}
|
103
|
+
)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_invalidate_deep_nested
|
107
|
+
assert_false h(
|
108
|
+
{:people => [{ :name => :to_s, :age => (18..40)}]},
|
109
|
+
{:people => [{ :name => "Ann", :age => 15 }, { :name => "Bob", :age => 23 }]}
|
110
|
+
)
|
111
|
+
end
|
83
112
|
end
|
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.
|
4
|
+
version: "0.2"
|
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-
|
12
|
+
date: 2009-12-19 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|