hashema 0.0.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/License.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Ben Christel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # hashema
2
+
3
+ Hashema lets you validate JSONable objects (hashes and arrays) against a schema, and assert their validity in your RSpec examples.
4
+
5
+ ## Validating an object against a schema
6
+
7
+ The API of `Hashema::Validator` consists of an initializer and two instance methods: `valid?` and `failure_message`. The initializer takes an object to validate and a schema, in that order. `valid?` will return `true` iff the object conforms to the schema. If `valid?` is `false`, `failure_message` will return a description of the failure.
8
+
9
+ ```ruby
10
+ validator = Hashema::Validator.new(
11
+ # the object to validate
12
+ { blog:
13
+ { url: 'http://www.blagoblag.com',
14
+ posts: [
15
+ { title: 'hello',
16
+ permalink: '/hello',
17
+ comment_count: 1
18
+ },
19
+ { title: 'test',
20
+ permalink: '/test',
21
+ comment_count: 0
22
+ }
23
+ ]
24
+ }
25
+ },
26
+
27
+ # the schema
28
+ { blog:
29
+ { url: /^https?:\/\//,
30
+ posts: [
31
+ { title: String,
32
+ permalink: /^\/[a-zA-Z0-9_\-\+\/]+$/,
33
+ comment_count: Numeric
34
+ }
35
+ ]
36
+ }
37
+ }
38
+ )
39
+
40
+ validator.valid? # true
41
+ ```
42
+
43
+ ## RSpec matcher usage
44
+
45
+ ```ruby
46
+ expect(datum).to conform_to_schema schema
47
+ ```
data/lib/hashema.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'hashema/validator'
2
+
3
+ begin
4
+ require 'rspec'
5
+ require 'hashema/conform_to_schema'
6
+ rescue LoadError => e
7
+ end
@@ -0,0 +1,14 @@
1
+ RSpec::Matchers.define :conform_to_schema do |schema|
2
+ match do |actual|
3
+ @validator = Hashema::Validator.new(actual, schema)
4
+ @validator.valid?
5
+ end
6
+
7
+ def failure_message
8
+ @validator.failure_message
9
+ end
10
+
11
+ def failure_message_when_negated
12
+ "expected\n#{actual.inspect}\nnot to match schema\n#{schema.inspect}"
13
+ end
14
+ end
@@ -0,0 +1,73 @@
1
+ module Hashema
2
+ class Validator
3
+ def initialize(actual, schema)
4
+ @actual = actual
5
+ @schema = schema
6
+ match! @actual, @schema
7
+ end
8
+
9
+ def valid?
10
+ !!@match
11
+ end
12
+
13
+ def failure_message
14
+ @mismatch
15
+ end
16
+
17
+ private
18
+
19
+ def match!(actual, schema, path=[])
20
+ @match = begin
21
+ if schema.is_a? Hash
22
+ match_hash! actual, schema, path
23
+ elsif schema.is_a? Array and schema.length == 1
24
+ match_array! actual, schema, path
25
+ elsif schema.is_a? Array and schema.length > 1
26
+ match_alternatives! actual, schema, path
27
+ else
28
+ match_with_triple_equals! actual, schema, path
29
+ end
30
+ end
31
+ end
32
+
33
+ def match_hash!(actual, schema, path)
34
+ recording_mismatches actual, schema, path do
35
+ schema.keys.sort == actual.keys.sort and
36
+ actual.all? do |key, value|
37
+ match! value, schema[key], path + [key]
38
+ end
39
+ end
40
+ end
41
+
42
+ def match_array!(actual, schema, path)
43
+ recording_mismatches actual, schema, path do
44
+ actual.is_a? Array and
45
+ actual.each_with_index.all? { |elem, i| match! elem, schema[0], path + [i] }
46
+ end
47
+ end
48
+
49
+ def match_alternatives!(actual, alternatives, path)
50
+ recording_mismatches actual, alternatives, path, true do
51
+ alternatives.any? do |alternative|
52
+ match! actual, alternative, path
53
+ end
54
+ end
55
+ end
56
+
57
+ def match_with_triple_equals!(actual, expected, path)
58
+ recording_mismatches actual, expected, path do
59
+ expected === actual
60
+ end
61
+ end
62
+
63
+ def recording_mismatches(actual, schema, path, overwrite=false)
64
+ if yield
65
+ true
66
+ else
67
+ mismatch = "expected /#{path.join("/")} to match\n#{schema.inspect}\nbut got\n#{actual.inspect}"
68
+ @mismatch = mismatch if !@mismatch || overwrite
69
+ false
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ module Hashema
2
+ module Version
3
+ STRING = '0.0.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Christel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ description: Assert that JSONable objects conform to a schema
31
+ email:
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files:
35
+ - README.md
36
+ files:
37
+ - lib/hashema.rb
38
+ - lib/hashema/conform_to_schema.rb
39
+ - lib/hashema/validator.rb
40
+ - lib/hashema/version.rb
41
+ - License.txt
42
+ - README.md
43
+ homepage: http://github.com/benchristel/hashema
44
+ licenses:
45
+ - MIT
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.23
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: hashema-0.0.0
69
+ test_files: []