schema_hash 0.1.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.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.md +41 -0
- data/README.rdoc +20 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/hash.rb +10 -0
- data/lib/schema_hash.rb +1 -0
- data/lib/schemable.rb +42 -0
- data/lib/traversable.rb +30 -0
- data/schema_hash.gemspec +60 -0
- data/spec/hash_spec.rb +25 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/traversable_spec.rb +137 -0
- metadata +82 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 David James
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# schema_hash
|
2
|
+
|
3
|
+
Is your hash in shape? Validates a hash against a (basic) schema.
|
4
|
+
|
5
|
+
This was built to support validation of config.yml files with nested keys.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
> require 'schema_hash'
|
10
|
+
> h = { :timeout => 10, :uri => "data.gov" }
|
11
|
+
|
12
|
+
> h.schema = { :timeout => true }
|
13
|
+
> h.valid?
|
14
|
+
=> false
|
15
|
+
|
16
|
+
h.schema = { :timeout => true, :uri => true, :key => true }
|
17
|
+
h.valid?
|
18
|
+
=> false
|
19
|
+
|
20
|
+
h.schema = { :timeout => true, :uri => true }
|
21
|
+
h.valid?
|
22
|
+
=> true
|
23
|
+
|
24
|
+
## Future Ideas
|
25
|
+
|
26
|
+
* Specify optional keys
|
27
|
+
|
28
|
+
## Note on Patches/Pull Requests
|
29
|
+
|
30
|
+
* Fork the project.
|
31
|
+
* Make your feature addition or bug fix.
|
32
|
+
* Add tests for it. This is important so I don't break it in a
|
33
|
+
future version unintentionally.
|
34
|
+
* Commit, do not mess with rakefile, version, or history.
|
35
|
+
(if you want to have your own version, that is fine but
|
36
|
+
bump version in a commit by itself I can ignore when I pull)
|
37
|
+
* Send me a pull request. Bonus points for topic branches.
|
38
|
+
|
39
|
+
## Copyright
|
40
|
+
|
41
|
+
Copyright (c) 2009 David James. See LICENSE for details.
|
data/README.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
= schema_hash
|
2
|
+
|
3
|
+
Is your hash in shape? Validates a hash against a (basic) schema.
|
4
|
+
|
5
|
+
This was built to support validation of config.yml files with nested keys.
|
6
|
+
|
7
|
+
== Note on Patches/Pull Requests
|
8
|
+
|
9
|
+
* Fork the project.
|
10
|
+
* Make your feature addition or bug fix.
|
11
|
+
* Add tests for it. This is important so I don't break it in a
|
12
|
+
future version unintentionally.
|
13
|
+
* Commit, do not mess with rakefile, version, or history.
|
14
|
+
(if you want to have your own version, that is fine but
|
15
|
+
bump version in a commit by itself I can ignore when I pull)
|
16
|
+
* Send me a pull request. Bonus points for topic branches.
|
17
|
+
|
18
|
+
== Copyright
|
19
|
+
|
20
|
+
Copyright (c) 2009 David James. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "schema_hash"
|
8
|
+
gem.summary = %Q{Validates a hash against a schema.}
|
9
|
+
gem.description = %Q{Is your hash in shape? We can help.}
|
10
|
+
gem.email = "djames@sunlightfoundation.com"
|
11
|
+
gem.homepage = "http://github.com/djsun/schema_hash"
|
12
|
+
gem.authors = ["David James"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
if File.exist?('VERSION')
|
40
|
+
version = File.read('VERSION')
|
41
|
+
else
|
42
|
+
version = ""
|
43
|
+
end
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "schema_hash #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/hash.rb
ADDED
data/lib/schema_hash.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/hash')
|
data/lib/schemable.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/traversable')
|
2
|
+
|
3
|
+
module Schemable
|
4
|
+
|
5
|
+
include Traversable
|
6
|
+
|
7
|
+
attr_reader :schema
|
8
|
+
|
9
|
+
def schema=(schema)
|
10
|
+
hash = schema.dup
|
11
|
+
hash.extend Traversable
|
12
|
+
@schema = hash.dup.extend Traversable
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
valid_subset? && !invalid_superset?
|
17
|
+
end
|
18
|
+
|
19
|
+
# Are all required keys present?
|
20
|
+
# "valid when nothing is missing"
|
21
|
+
def valid_subset?
|
22
|
+
valid = true
|
23
|
+
schema.traverse do |key_path, value|
|
24
|
+
actual = self.ref(key_path)
|
25
|
+
missing = actual.nil?
|
26
|
+
valid = valid && !missing
|
27
|
+
end
|
28
|
+
valid
|
29
|
+
end
|
30
|
+
|
31
|
+
# Are no extra keys present?
|
32
|
+
# "invalid when something is unexpected"
|
33
|
+
def invalid_superset?
|
34
|
+
invalid = false
|
35
|
+
self.traverse do |key_path, value|
|
36
|
+
expected = schema.ref(key_path)
|
37
|
+
invalid = invalid || !expected
|
38
|
+
end
|
39
|
+
invalid
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/traversable.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Traversable
|
2
|
+
|
3
|
+
def traverse(&block)
|
4
|
+
self.each do |key, value|
|
5
|
+
Traversable._traverse(key, value, [], &block)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self._traverse(key, value, path, &block)
|
10
|
+
if value.is_a?(Hash)
|
11
|
+
value.each do |k, v|
|
12
|
+
p = path + [key]
|
13
|
+
yield(p, value)
|
14
|
+
_traverse(k, v, p, &block)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
yield(path + [key], value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# reference(["a", "b"]) is a safe version of self["a"]["b"]
|
22
|
+
def ref(key_path)
|
23
|
+
object = self
|
24
|
+
key_path.each do |key|
|
25
|
+
object = object.is_a?(Hash) ? object[key] : nil
|
26
|
+
end
|
27
|
+
object
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/schema_hash.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{schema_hash}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David James"]
|
12
|
+
s.date = %q{2009-10-08}
|
13
|
+
s.description = %q{Is your hash in shape? We can help.}
|
14
|
+
s.email = %q{djames@sunlightfoundation.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
"LICENSE",
|
24
|
+
"README.md",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/hash.rb",
|
29
|
+
"lib/schema_hash.rb",
|
30
|
+
"lib/schemable.rb",
|
31
|
+
"lib/traversable.rb",
|
32
|
+
"schema_hash.gemspec",
|
33
|
+
"spec/hash_spec.rb",
|
34
|
+
"spec/spec_helper.rb",
|
35
|
+
"spec/traversable_spec.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/djsun/schema_hash}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.5}
|
41
|
+
s.summary = %q{Validates a hash against a schema.}
|
42
|
+
s.test_files = [
|
43
|
+
"spec/hash_spec.rb",
|
44
|
+
"spec/spec_helper.rb",
|
45
|
+
"spec/traversable_spec.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
59
|
+
end
|
60
|
+
end
|
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
|
5
|
+
describe "calling #schema on a Hash" do
|
6
|
+
before do
|
7
|
+
@h = { :a => "a" }
|
8
|
+
@h.schema = {} # value is arbitrary
|
9
|
+
@h
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should be Schemable" do
|
13
|
+
@h.is_a?(Schemable).should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should have #valid?" do
|
17
|
+
@h.respond_to?(:valid?).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have #schema" do
|
21
|
+
@h.respond_to?(:schema).should be_true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Traversable do
|
4
|
+
|
5
|
+
describe "#ref" do
|
6
|
+
describe "simple hash" do
|
7
|
+
before do
|
8
|
+
@h = { :a => "a" }
|
9
|
+
@h.extend Traversable
|
10
|
+
end
|
11
|
+
|
12
|
+
it "[]" do
|
13
|
+
@h.ref([]).should == @h
|
14
|
+
end
|
15
|
+
|
16
|
+
it "[:x]" do
|
17
|
+
@h.ref([:x]).should == nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "[:x, :y]" do
|
21
|
+
@h.ref([:x, :y]).should == nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "[:a]" do
|
25
|
+
@h.ref([:a]).should == @h[:a]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "2 level hash" do
|
30
|
+
before do
|
31
|
+
@h = { :a => { :b => "b" } }
|
32
|
+
@h.extend Traversable
|
33
|
+
end
|
34
|
+
|
35
|
+
it "[]" do
|
36
|
+
@h.ref([]).should == @h
|
37
|
+
end
|
38
|
+
|
39
|
+
it "[:x]" do
|
40
|
+
@h.ref([:x]).should == nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "[:x, :y]" do
|
44
|
+
@h.ref([:x, :y]).should == nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it "[:a]" do
|
48
|
+
@h.ref([:a]).should == @h[:a]
|
49
|
+
end
|
50
|
+
|
51
|
+
it "[:a, :b]" do
|
52
|
+
@h.ref([:a, :b]).should == @h[:a][:b]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "3 level hash" do
|
57
|
+
before do
|
58
|
+
@h = { :a => { :b => { :c => "c" } } }
|
59
|
+
@h.extend Traversable
|
60
|
+
end
|
61
|
+
|
62
|
+
it "[]" do
|
63
|
+
@h.ref([]).should == @h
|
64
|
+
end
|
65
|
+
|
66
|
+
it "[:x]" do
|
67
|
+
@h.ref([:x]).should == nil
|
68
|
+
end
|
69
|
+
|
70
|
+
it "[:x, :y]" do
|
71
|
+
@h.ref([:x, :y]).should == nil
|
72
|
+
end
|
73
|
+
|
74
|
+
it "[:a]" do
|
75
|
+
@h.ref([:a]).should == @h[:a]
|
76
|
+
end
|
77
|
+
|
78
|
+
it "[:a, :x]" do
|
79
|
+
@h.ref([:a, :x]).should == nil
|
80
|
+
end
|
81
|
+
|
82
|
+
it "[:a, :b]" do
|
83
|
+
@h.ref([:a, :b]).should == @h[:a][:b]
|
84
|
+
end
|
85
|
+
|
86
|
+
it "[:a, :b, :x]" do
|
87
|
+
@h.ref([:a, :b, :x]).should == nil
|
88
|
+
end
|
89
|
+
|
90
|
+
it "[:a, :b, :c]" do
|
91
|
+
@h.ref([:a, :b, :c]).should == @h[:a][:b][:c]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#traverse" do
|
97
|
+
it "over a 1 level hash" do
|
98
|
+
@h = { :a => "a" }
|
99
|
+
@h.extend Traversable
|
100
|
+
array = []
|
101
|
+
@h.traverse do |key_path, value|
|
102
|
+
array << [key_path, value]
|
103
|
+
end
|
104
|
+
array.should == [
|
105
|
+
[[:a], "a"]
|
106
|
+
]
|
107
|
+
end
|
108
|
+
|
109
|
+
it "over a 2 level hash" do
|
110
|
+
@h = { :a => { :b => "b" } }
|
111
|
+
@h.extend Traversable
|
112
|
+
array = []
|
113
|
+
@h.traverse do |key_path, value|
|
114
|
+
array << [key_path, value]
|
115
|
+
end
|
116
|
+
array.should == [
|
117
|
+
[[:a], { :b => "b" }],
|
118
|
+
[[:a, :b], "b"]
|
119
|
+
]
|
120
|
+
end
|
121
|
+
|
122
|
+
it "over a 3 level hash" do
|
123
|
+
@h = { :a => { :b => { :c => "c" } } }
|
124
|
+
@h.extend Traversable
|
125
|
+
array = []
|
126
|
+
@h.traverse do |key_path, value|
|
127
|
+
array << [key_path, value]
|
128
|
+
end
|
129
|
+
array.should == [
|
130
|
+
[[:a], { :b => {:c => "c" }}],
|
131
|
+
[[:a, :b], { :c => "c"}],
|
132
|
+
[[:a, :b, :c], "c"]
|
133
|
+
]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schema_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David James
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-08 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Is your hash in shape? We can help.
|
26
|
+
email: djames@sunlightfoundation.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.md
|
34
|
+
- README.rdoc
|
35
|
+
files:
|
36
|
+
- .document
|
37
|
+
- .gitignore
|
38
|
+
- LICENSE
|
39
|
+
- README.md
|
40
|
+
- README.rdoc
|
41
|
+
- Rakefile
|
42
|
+
- VERSION
|
43
|
+
- lib/hash.rb
|
44
|
+
- lib/schema_hash.rb
|
45
|
+
- lib/schemable.rb
|
46
|
+
- lib/traversable.rb
|
47
|
+
- schema_hash.gemspec
|
48
|
+
- spec/hash_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
- spec/traversable_spec.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/djsun/schema_hash
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Validates a hash against a schema.
|
79
|
+
test_files:
|
80
|
+
- spec/hash_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
- spec/traversable_spec.rb
|