structure_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c0b88191b18ae92fc1294dd58ea5682c2e42a49
4
+ data.tar.gz: 49d5cb2b698018f2dce6521c1cbea879f17e46a1
5
+ SHA512:
6
+ metadata.gz: 443fc198c4e4369845534e2c76c59433f8a9d55e9a704fd5b86157b7c61ac5b2e81825db1e6616ff1877631cfd74fae6618cc1bf77fd927205cff1fab1155e49
7
+ data.tar.gz: dca82a629ee8004b94d1d07365337662f64b5d88b2e9fc749fb5e684fe999bbfbd20f99de9414a90be8d7a1cb7b9a5689b07e655804d8df7dd634aeb683c76e9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in structure_validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Claudio Ortolina
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # StructureValidator
2
+
3
+ Allows testing a hash against a specified structure:
4
+
5
+ test_hash = {
6
+ name: 'Foo',
7
+ count: 3,
8
+ data: {
9
+ text: 'content',
10
+ meta: 'http://example.com'
11
+ }
12
+ }
13
+
14
+ expected_structure = {
15
+ name: 'string',
16
+ count: 'integer',
17
+ data: {
18
+ text: 'string',
19
+ meta: 'string
20
+ }
21
+ }
22
+
23
+ StructureValidator.validate(test_hash, expected_structure)
24
+ # => true
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ gem 'structure_validator'
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install structure_validator
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,43 @@
1
+ require 'json'
2
+ require 'structure_validator/version'
3
+
4
+ module StructureValidator
5
+ class UnsupportedDataType < RuntimeError; end
6
+
7
+ def self.validate(object, expected_structure)
8
+ check_required_keys(object, expected_structure) && check_type_for_each_key(object, expected_structure)
9
+ end
10
+
11
+ private
12
+
13
+ def self.check_required_keys(object, expected_structure)
14
+ (expected_structure.keys - object.keys).empty?
15
+ end
16
+
17
+ def self.check_type_for_each_key(object, expected_structure)
18
+ expected_structure.delete_if do |key, type|
19
+ if type.is_a?(Hash) && object[key].is_a?(Hash)
20
+ validate(object[key], type)
21
+ else
22
+ test_type_for(object[key], type)
23
+ end
24
+ end.empty?
25
+ end
26
+
27
+ def self.test_type_for(value, type)
28
+ case type.to_sym
29
+ when :string
30
+ value.is_a? String
31
+ when :integer
32
+ value.is_a? Integer
33
+ when :json
34
+ begin
35
+ !!JSON.parse(value)
36
+ rescue JSON::ParserError
37
+ false
38
+ end
39
+ else
40
+ raise UnsupportedDataType.new("Unexpected type: #{type}")
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module StructureValidator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ class Hash
2
+ # Returns a new hash with +self+ and +other_hash+ merged recursively.
3
+ #
4
+ # h1 = { x: { y: [4,5,6] }, z: [7,8,9] }
5
+ # h2 = { x: { y: [7,8,9] }, z: 'xyz' }
6
+ #
7
+ # h1.deep_merge(h2) #=> {x: {y: [7, 8, 9]}, z: "xyz"}
8
+ # h2.deep_merge(h1) #=> {x: {y: [4, 5, 6]}, z: [7, 8, 9]}
9
+ # h1.deep_merge(h2) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
10
+ # #=> {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]}
11
+ def deep_merge(other_hash, &block)
12
+ dup.deep_merge!(other_hash, &block)
13
+ end
14
+
15
+ # Same as +deep_merge+, but modifies +self+.
16
+ def deep_merge!(other_hash, &block)
17
+ other_hash.each_pair do |k,v|
18
+ tv = self[k]
19
+ if tv.is_a?(Hash) && v.is_a?(Hash)
20
+ self[k] = tv.deep_merge(v, &block)
21
+ else
22
+ self[k] = block && tv ? block.call(k, tv, v) : v
23
+ end
24
+ end
25
+ self
26
+ end
27
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'core_ext/hash'
2
+ require_relative '../lib/structure_validator'
3
+
4
+ describe StructureValidator do
5
+
6
+ let(:hash_to_validate) do
7
+ {
8
+ item: {
9
+ name: 'Foo',
10
+ count: 3,
11
+ data: {
12
+ some: 'value'
13
+ }.to_json
14
+ }
15
+ }
16
+ end
17
+
18
+ let(:expected_structure) do
19
+ {
20
+ item: {
21
+ name: 'string',
22
+ count: 'integer',
23
+ data: 'json'
24
+ }
25
+ }
26
+ end
27
+
28
+ it "validates the hash" do
29
+ expect(StructureValidator.validate(hash_to_validate, expected_structure)).to be_true
30
+ end
31
+
32
+ it "checks required keys" do
33
+ hash_to_validate[:item].delete(:count)
34
+ expect(StructureValidator.validate(hash_to_validate, expected_structure)).to be_false
35
+ end
36
+
37
+ it "raises error with an unexpected type" do
38
+ expected_structure.deep_merge!(item: { name: 'foo' })
39
+ expect {
40
+ StructureValidator.validate(hash_to_validate, expected_structure)
41
+ }.to raise_error(StructureValidator::UnsupportedDataType, 'Unexpected type: foo')
42
+ end
43
+
44
+ it "checks the key type" do
45
+ hash_to_validate.merge!(item: { count: 'foo' })
46
+ expect(StructureValidator.validate(hash_to_validate, expected_structure)).to be_false
47
+ end
48
+
49
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'structure_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "structure_validator"
8
+ spec.version = StructureValidator::VERSION
9
+ spec.authors = ["Claudio Ortolina"]
10
+ spec.email = ["claudio@new-bamboo.co.uk"]
11
+ spec.description = %q{Validates a hash against an expected structure.}
12
+ spec.summary = %q{Validates a hash against an expected structure.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: structure_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Claudio Ortolina
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Validates a hash against an expected structure.
56
+ email:
57
+ - claudio@new-bamboo.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/structure_validator.rb
68
+ - lib/structure_validator/version.rb
69
+ - spec/core_ext/hash.rb
70
+ - spec/structure_validator_spec.rb
71
+ - structure_validator.gemspec
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Validates a hash against an expected structure.
96
+ test_files:
97
+ - spec/core_ext/hash.rb
98
+ - spec/structure_validator_spec.rb