hashidator 0.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -14,4 +14,6 @@ see examples/basic.rb for full coverage. it knows
14
14
  classes, ranges, booleans, proc and duck typing. yay!
15
15
 
16
16
  (c) 2009 harry vangberg <harry@vangberg.name>,
17
+ peter suschlik <peter@suschlik.de>
18
+
17
19
  distributed under the mit license. google it.
data/hashidator.gemspec CHANGED
@@ -1,13 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "hashidator"
3
- s.version = "0.3"
4
- s.date = "2010-02-03"
3
+ s.version = "0.3.1"
4
+ s.date = "2010-10-05"
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"
8
8
  s.description = "define schemas as a hash, and validate hashes!"
9
9
  s.has_rdoc = false
10
- s.authors = ["Harry Vangberg"]
10
+ s.authors = ["Harry Vangberg", "Peter Suschlik"]
11
11
  s.files = [
12
12
  "README",
13
13
  "TODO",
data/lib/hashidator.rb CHANGED
@@ -10,6 +10,7 @@ class Hashidator
10
10
  end
11
11
 
12
12
  def validate(input)
13
+ input ||= {}
13
14
  schema.all? {|key, validator|
14
15
  validate_value(validator, input[key])
15
16
  }
@@ -22,11 +23,11 @@ class Hashidator
22
23
  when Range
23
24
  validator.include? value
24
25
  when Array
25
- value.all? {|x| validate_value(validator[0], x)}
26
+ value.respond_to?(:all?) && value.all? {|x| validate_value(validator[0], x)}
26
27
  when Symbol
27
28
  value.respond_to? validator
28
29
  when Regexp
29
- value.match validator
30
+ validator.match value.to_s
30
31
  when Hash
31
32
  Hashidator.validate(validator, value)
32
33
  when Class, Module
@@ -1,4 +1,4 @@
1
- require 'test/helper'
1
+ require './test/helper.rb'
2
2
 
3
3
  class TestHashidator < Test::Unit::TestCase
4
4
  def h(schema, input)
@@ -44,6 +44,12 @@ class TestHashidator < Test::Unit::TestCase
44
44
  assert_false h({:children => [String]}, {:children => ["Sue", 1234]})
45
45
  end
46
46
 
47
+ def test_invalidate_array_members_with_non_array_values
48
+ assert_false h({:ary => [String]}, {:ary => nil})
49
+ assert_false h({:ary => [String]}, {:ary => :not_an_array})
50
+ assert_false h({:ary => [String]}, {:ary => {:i_am => "a hash"}})
51
+ end
52
+
47
53
  def test_validate_boolean
48
54
  assert_true h({:admin => Boolean}, {:admin => true})
49
55
  assert_true h({:admin => Boolean}, {:admin => true})
@@ -59,6 +65,8 @@ class TestHashidator < Test::Unit::TestCase
59
65
 
60
66
  def test_invalidate_regexp
61
67
  assert_false h({:uri => /^http:/}, {:uri => "john coltrane"})
68
+ assert_false h({:uri => /^http:/}, {:uri => nil})
69
+ assert_false h({:uri => /^http:/}, {:uri => :symbol})
62
70
  end
63
71
 
64
72
  def test_validate_respond_to
@@ -139,4 +147,11 @@ class TestHashidator < Test::Unit::TestCase
139
147
  {:people => [{ :name => "Ann", :age => 15 }, { :name => "Bob", :age => 23 }]}
140
148
  )
141
149
  end
150
+
151
+ def test_invalidate_for_nil_input
152
+ schema = {:user => { :name => String }}
153
+ assert_false h(schema, nil)
154
+ assert_false h(schema, {})
155
+ assert_false h(schema, {:user => nil})
156
+ end
142
157
  end
metadata CHANGED
@@ -1,15 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashidator
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ hash: 17
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 1
10
+ version: 0.3.1
5
11
  platform: ruby
6
12
  authors:
7
13
  - Harry Vangberg
14
+ - Peter Suschlik
8
15
  autorequire:
9
16
  bindir: bin
10
17
  cert_chain: []
11
18
 
12
- date: 2010-02-03 00:00:00 +01:00
19
+ date: 2010-10-05 00:00:00 +02:00
13
20
  default_executable:
14
21
  dependencies: []
15
22
 
@@ -26,7 +33,9 @@ files:
26
33
  - TODO
27
34
  - hashidator.gemspec
28
35
  - lib/hashidator.rb
29
- has_rdoc: false
36
+ - test/helper.rb
37
+ - test/test_hashidator.rb
38
+ has_rdoc: true
30
39
  homepage: http://github.com/ichverstehe/hashidator
31
40
  licenses: []
32
41
 
@@ -36,21 +45,27 @@ rdoc_options: []
36
45
  require_paths:
37
46
  - lib
38
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
39
49
  requirements:
40
50
  - - ">="
41
51
  - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
42
55
  version: "0"
43
- version:
44
56
  required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
45
58
  requirements:
46
59
  - - ">="
47
60
  - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
48
64
  version: "0"
49
- version:
50
65
  requirements: []
51
66
 
52
67
  rubyforge_project:
53
- rubygems_version: 1.3.3
68
+ rubygems_version: 1.3.7
54
69
  signing_key:
55
70
  specification_version: 3
56
71
  summary: define schemas as a hash, and validate hashes!