hashema 0.0.1 → 0.0.2
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.
- checksums.yaml +7 -0
- data/lib/hashema/conform_to_schema.rb +3 -1
- data/lib/hashema/validator.rb +56 -9
- data/lib/hashema/version.rb +1 -1
- metadata +11 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0320697b5755730fd8ffa18843b0f3b496cfd739
|
4
|
+
data.tar.gz: 8bf8bcedb3d51e880d5a13c499754c3fc604a5c0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f8e5882479f1cbb3a9923575812eafc7d78281a69fd070f6ac488468e4b9d002ccfaca37b41717522df1d8429a2559251b07c321941f09a388f99a28aaafda12
|
7
|
+
data.tar.gz: 98a5238131421fb6fe61ff625cf2cbd71ffaaada4d2280df5079f25540709aa0e6cdb872336eb05a229d6819116ff2d894ffe3640cb5db1a03b7c35c5d154a7b
|
@@ -1,5 +1,7 @@
|
|
1
1
|
RSpec::Matchers.define :conform_to_schema do |schema|
|
2
2
|
match do |actual|
|
3
|
+
@schema = schema
|
4
|
+
@actual = actual
|
3
5
|
@validator = Hashema::Validator.new(actual, schema)
|
4
6
|
@validator.valid?
|
5
7
|
end
|
@@ -13,7 +15,7 @@ RSpec::Matchers.define :conform_to_schema do |schema|
|
|
13
15
|
end
|
14
16
|
|
15
17
|
def failure_message_when_negated
|
16
|
-
"expected\n#{actual.inspect}\nnot to match schema\n#{schema.inspect}"
|
18
|
+
"expected\n#{@actual.inspect}\nnot to match schema\n#{@schema.inspect}"
|
17
19
|
end
|
18
20
|
|
19
21
|
def failure_message_for_should_not
|
data/lib/hashema/validator.rb
CHANGED
@@ -3,6 +3,7 @@ module Hashema
|
|
3
3
|
def initialize(actual, schema)
|
4
4
|
@actual = actual
|
5
5
|
@schema = schema
|
6
|
+
@withholding_judgement = false
|
6
7
|
match! @actual, @schema
|
7
8
|
end
|
8
9
|
|
@@ -31,25 +32,54 @@ module Hashema
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def match_hash!(actual, schema, path)
|
35
|
+
if actual.is_a? Hash
|
36
|
+
if schema.keys.sort == actual.keys.sort
|
37
|
+
match_hash_with_same_keys! actual, schema, path
|
38
|
+
else
|
39
|
+
report_mismatched_key_sets! actual, schema, path
|
40
|
+
end
|
41
|
+
else
|
42
|
+
report_error "expected #{format_path path} to be a Hash, but got #{actual.class}"
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def match_hash_with_same_keys!(actual, schema, path)
|
34
48
|
recording_mismatches actual, schema, path do
|
35
|
-
schema.keys.sort == actual.keys.sort and
|
36
49
|
actual.all? do |key, value|
|
37
50
|
match! value, schema[key], path + [key]
|
38
51
|
end
|
39
52
|
end
|
40
53
|
end
|
41
54
|
|
55
|
+
def report_mismatched_key_sets!(actual, schema, path)
|
56
|
+
extras = actual.keys - schema.keys
|
57
|
+
missing = schema.keys - actual.keys
|
58
|
+
error = "expected #{format_path path} to have a different set of keys\n"
|
59
|
+
error += "the extra keys were:\n #{extras.map(&:inspect).join("\n ")}\n" if extras.any?
|
60
|
+
error += "the missing keys were:\n #{missing.map(&:inspect).join("\n ")}\n" if missing.any?
|
61
|
+
report_error error
|
62
|
+
false
|
63
|
+
end
|
64
|
+
|
42
65
|
def match_array!(actual, schema, path)
|
43
|
-
|
44
|
-
actual
|
45
|
-
|
66
|
+
if actual.is_a? Array
|
67
|
+
recording_mismatches actual, schema, path do
|
68
|
+
actual.is_a? Array and
|
69
|
+
actual.each_with_index.all? { |elem, i| match! elem, schema[0], path + [i] }
|
70
|
+
end
|
71
|
+
else
|
72
|
+
report_error "expected #{format_path path} to be an Array, but got #{actual.class}"
|
73
|
+
false
|
46
74
|
end
|
47
75
|
end
|
48
76
|
|
49
77
|
def match_alternatives!(actual, alternatives, path)
|
50
|
-
recording_mismatches actual, alternatives, path
|
78
|
+
recording_mismatches actual, alternatives, path do
|
51
79
|
alternatives.any? do |alternative|
|
52
|
-
|
80
|
+
withholding_judgement actual, alternatives, path do
|
81
|
+
match! actual, alternative, path
|
82
|
+
end
|
53
83
|
end
|
54
84
|
end
|
55
85
|
end
|
@@ -60,14 +90,31 @@ module Hashema
|
|
60
90
|
end
|
61
91
|
end
|
62
92
|
|
63
|
-
def
|
93
|
+
def withholding_judgement(actual, schema, path)
|
94
|
+
original_withholding_judgement = @withholding_judgement
|
95
|
+
@withholding_judgement = true
|
96
|
+
returned = yield
|
97
|
+
@withholding_judgement = original_withholding_judgement
|
98
|
+
returned
|
99
|
+
end
|
100
|
+
|
101
|
+
def recording_mismatches(actual, schema, path)
|
64
102
|
if yield
|
65
103
|
true
|
66
104
|
else
|
67
|
-
|
68
|
-
@mismatch = mismatch if !@mismatch || overwrite
|
105
|
+
report_error "expected #{format_path path} to match\n#{schema.inspect}\nbut got\n#{actual.inspect}"
|
69
106
|
false
|
70
107
|
end
|
71
108
|
end
|
109
|
+
|
110
|
+
def report_error(error)
|
111
|
+
unless @withholding_judgement
|
112
|
+
@mismatch ||= error
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def format_path(path)
|
117
|
+
"/#{path.join("/")}"
|
118
|
+
end
|
72
119
|
end
|
73
120
|
end
|
data/lib/hashema/version.rb
CHANGED
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Ben Christel
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-22 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 2.0.0
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 2.0.0
|
30
27
|
description: Assert that JSONable objects conform to a schema
|
@@ -43,27 +40,27 @@ files:
|
|
43
40
|
homepage: http://github.com/benchristel/hashema
|
44
41
|
licenses:
|
45
42
|
- MIT
|
43
|
+
metadata: {}
|
46
44
|
post_install_message:
|
47
45
|
rdoc_options:
|
48
46
|
- --charset=UTF-8
|
49
47
|
require_paths:
|
50
48
|
- lib
|
51
49
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
50
|
requirements:
|
54
|
-
- -
|
51
|
+
- - '>='
|
55
52
|
- !ruby/object:Gem::Version
|
56
53
|
version: '0'
|
57
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
55
|
requirements:
|
60
|
-
- -
|
56
|
+
- - '>='
|
61
57
|
- !ruby/object:Gem::Version
|
62
58
|
version: '0'
|
63
59
|
requirements: []
|
64
60
|
rubyforge_project:
|
65
|
-
rubygems_version:
|
61
|
+
rubygems_version: 2.0.0
|
66
62
|
signing_key:
|
67
|
-
specification_version:
|
68
|
-
summary: hashema-0.0.
|
63
|
+
specification_version: 4
|
64
|
+
summary: hashema-0.0.2
|
69
65
|
test_files: []
|
66
|
+
has_rdoc:
|