hashidator 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -0
- data/TODO +3 -0
- data/hashidator.gemspec +21 -0
- data/lib/hashidator.rb +50 -0
- data/test/helper.rb +15 -0
- data/test/test_hashidator.rb +83 -0
- metadata +59 -0
data/README
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
define schemas as a hash, and validate hashes!
|
2
|
+
|
3
|
+
require 'hashidator'
|
4
|
+
|
5
|
+
schema = {:name => String, :age => Integer}
|
6
|
+
valid = {:name => "Harry", :age => 20}
|
7
|
+
invalid = {:name => 1234, :age => "twenty"}
|
8
|
+
|
9
|
+
h = Hashidator.new(schema)
|
10
|
+
h.validate(valid) #=> true
|
11
|
+
h.validate(invalid) #=> false
|
12
|
+
|
13
|
+
see examples/basic.rb for full coverage. it knows
|
14
|
+
classes, ranges, booleans and duck typing. yay!
|
15
|
+
|
16
|
+
(c) 2009 harry vangberg <harry@vangberg.name>,
|
17
|
+
distributed under the mit license. google it.
|
data/TODO
ADDED
data/hashidator.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "hashidator"
|
3
|
+
s.version = "0.1"
|
4
|
+
s.date = "2009-12-18"
|
5
|
+
s.summary = "define schemas as a hash, and validate hashes!"
|
6
|
+
s.email = "harry@vangberg.name"
|
7
|
+
s.homepage = "http://github.com/ichverstehe/hashidator"
|
8
|
+
s.description = "define schemas as a hash, and validate hashes!"
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.authors = ["Harry Vangberg"]
|
11
|
+
s.files = [
|
12
|
+
"README",
|
13
|
+
"TODO",
|
14
|
+
"hashidator.gemspec",
|
15
|
+
"lib/hashidator.rb"
|
16
|
+
]
|
17
|
+
s.test_files = [
|
18
|
+
"test/helper.rb",
|
19
|
+
"test/test_hashidator.rb"
|
20
|
+
]
|
21
|
+
end
|
data/lib/hashidator.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class Hashidator
|
2
|
+
def self.validate(schema, input)
|
3
|
+
hd = new(schema)
|
4
|
+
hd.validate(input)
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_accessor :schema, :errors
|
8
|
+
|
9
|
+
def initialize(schema)
|
10
|
+
@schema = schema
|
11
|
+
end
|
12
|
+
|
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
|
35
|
+
}
|
36
|
+
|
37
|
+
results.all?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module Boolean
|
42
|
+
end
|
43
|
+
|
44
|
+
class TrueClass
|
45
|
+
include Boolean
|
46
|
+
end
|
47
|
+
|
48
|
+
class FalseClass
|
49
|
+
include Boolean
|
50
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.unshift "lib"
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'contest'
|
5
|
+
require 'hashidator'
|
6
|
+
|
7
|
+
class Test::Unit::TestCase
|
8
|
+
def assert_true(subject)
|
9
|
+
assert_equal true, subject
|
10
|
+
end
|
11
|
+
|
12
|
+
def assert_false(subject)
|
13
|
+
assert_equal false, subject
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test/helper'
|
2
|
+
|
3
|
+
class TestHashidator < Test::Unit::TestCase
|
4
|
+
def h(schema, input)
|
5
|
+
Hashidator.validate(schema, input)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_validate_integer
|
9
|
+
assert_true h({:age => Integer}, {:age => 20})
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_invalidate_integer
|
13
|
+
assert_false h({:age => Integer}, {:age => "twenty"})
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_validate_string
|
17
|
+
assert_true h({:name => String}, {:name => "Harry"})
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_invalidate_string
|
21
|
+
assert_false h({:name => String}, {:name => 44667})
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_validate_in_range
|
25
|
+
assert_true h({:age => (10..20)}, {:age => 10})
|
26
|
+
assert_true h({:age => (10..20)}, {:age => 15})
|
27
|
+
assert_true h({:age => (10..20)}, {:age => 20})
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_invalidate_in_range
|
31
|
+
assert_false h({:age => (10..20)}, {:age => 9})
|
32
|
+
assert_false h({:age => (10..20)}, {:age => 21})
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_validate_array_members
|
36
|
+
assert_true h({:children => [String]}, {:children => ["Sue", "Mike"]})
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_invalidate_array_members
|
40
|
+
assert_false h({:children => [String]}, {:children => ["Sue", 1234]})
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_validate_boolean
|
44
|
+
assert_true h({:admin => Boolean}, {:admin => true})
|
45
|
+
assert_true h({:admin => Boolean}, {:admin => true})
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_invalidate_boolean
|
49
|
+
assert_false h({:admin => Boolean}, {:admin => 1234})
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_validate_respond_to
|
53
|
+
assert_true h({:name => :to_s}, {:name => "Harry"})
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_invalidate_respond_to
|
57
|
+
assert_false h({:name => :non_existing_method}, {:name => "Harry"})
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_validate_array_members_respond_to
|
61
|
+
assert_true h(
|
62
|
+
{:names => [:to_s]},
|
63
|
+
{:names => ["Harry", "Damone"]}
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_invalidate_array_members_respond_to
|
68
|
+
assert_false h(
|
69
|
+
{:name => [:non_existing_method]},
|
70
|
+
{:name => ["Harry", "Damone"]}
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_validate_nested
|
75
|
+
schema = {:name => {:first => String, :last => String}}
|
76
|
+
assert_true h(schema, {:name => {:first => "Mike", :last => "Damone"}})
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_invalidate_nested
|
80
|
+
schema = {:name => {:first => String, :last => String}}
|
81
|
+
assert_false h(schema, {:name => {:first => "Mike", :last => 1234}})
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashidator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Harry Vangberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-18 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: define schemas as a hash, and validate hashes!
|
17
|
+
email: harry@vangberg.name
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- TODO
|
27
|
+
- hashidator.gemspec
|
28
|
+
- lib/hashidator.rb
|
29
|
+
has_rdoc: false
|
30
|
+
homepage: http://github.com/ichverstehe/hashidator
|
31
|
+
licenses: []
|
32
|
+
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.3.5
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: define schemas as a hash, and validate hashes!
|
57
|
+
test_files:
|
58
|
+
- test/helper.rb
|
59
|
+
- test/test_hashidator.rb
|