json-matchers 0.3.0 → 0.3.1
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 +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +7 -5
- data/json-matchers.gemspec +2 -2
- data/lib/json-matchers.rb +12 -0
- data/lib/json/matchers.rb +7 -15
- data/lib/json_matchers.rb +14 -0
- data/lib/json_matchers/errors.rb +4 -0
- data/lib/json_matchers/matcher.rb +37 -0
- data/lib/json_matchers/rspec.rb +83 -0
- data/lib/json_matchers/version.rb +3 -0
- data/spec/{json/matchers → json_matchers}/match_response_schema_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/support/file_helpers.rb +6 -6
- metadata +10 -8
- data/lib/json/matchers/errors.rb +0 -6
- data/lib/json/matchers/matcher.rb +0 -39
- data/lib/json/matchers/rspec.rb +0 -83
- data/lib/json/matchers/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f5663f28a62b7036c41b7648cc84db8be036445
|
4
|
+
data.tar.gz: 4dc11e206e1010683b3b92277bd61f314fdcb95c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d710ea1267974d7dd9e7bba6181f2d39206d7552ec3c2613f17f05239ec546ee85ab27ef580d817b7ce434849d201c6e32b06a57beb50821e5a8b961fd067f9f
|
7
|
+
data.tar.gz: 85d2db20bde72739562656cf0b651a9d5df49319cfdd3244b661d623dfc5ec84b231a965268c799c083d938af4732183a510d2abae470091cce4ec8e473bbc57
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
master
|
2
2
|
------
|
3
3
|
|
4
|
+
0.3.1
|
5
|
+
-----
|
6
|
+
|
7
|
+
* BREAKING CHANGE: Rename module to `JsonMatchers`. This resolves clashing with
|
8
|
+
gems like `oj` / `oj_mimic_json` that take control of the standard library's
|
9
|
+
`json` module. As a result, the file to require is now `json_matchers`,
|
10
|
+
instead of `json/matchers`.
|
11
|
+
|
12
|
+
* No longer condone auto-loading RSpec. Add documentation around adding `require
|
13
|
+
"json_matchers/rspec"` to consumers' `spec/spec_helper.rb`
|
14
|
+
|
4
15
|
0.3.0
|
5
16
|
-----
|
6
17
|
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# JsonMatchers
|
2
2
|
|
3
3
|
Validate the JSON returned by your Rails JSON APIs
|
4
4
|
|
@@ -7,7 +7,9 @@ Validate the JSON returned by your Rails JSON APIs
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
|
10
|
+
group :test do
|
11
|
+
gem "json-matchers", require: false
|
12
|
+
end
|
11
13
|
```
|
12
14
|
|
13
15
|
And then execute:
|
@@ -27,7 +29,7 @@ First, include it in your `spec_helper`:
|
|
27
29
|
```ruby
|
28
30
|
# spec/spec_helper.rb
|
29
31
|
|
30
|
-
require "
|
32
|
+
require "json_matchers/rspec"
|
31
33
|
```
|
32
34
|
|
33
35
|
Define your [JSON Schema](http://json-schema.org/example1.html) in the schema directory:
|
@@ -134,13 +136,13 @@ To learn more about `$ref`, check out [Understanding JSON Schema Structuring](ht
|
|
134
136
|
|
135
137
|
By default, the schema directory is `spec/support/api/schemas`.
|
136
138
|
|
137
|
-
This can be configured via `
|
139
|
+
This can be configured via `JsonMatchers.schema_root`.
|
138
140
|
|
139
141
|
|
140
142
|
```ruby
|
141
143
|
# spec/support/json-matchers.rb
|
142
144
|
|
143
|
-
|
145
|
+
JsonMatchers.schema_root = "docs/api/schemas"
|
144
146
|
```
|
145
147
|
|
146
148
|
## Contributing
|
data/json-matchers.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "
|
4
|
+
require "json_matchers/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "json-matchers"
|
8
|
-
spec.version =
|
8
|
+
spec.version = JsonMatchers::VERSION
|
9
9
|
spec.authors = ["Sean Doyle"]
|
10
10
|
spec.email = ["sean.p.doyle24@gmail.com"]
|
11
11
|
spec.summary = %q{Validate your Rails JSON API's JSON}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
warn <<-WARNING
|
2
|
+
DEPRECATED: The `json-matchers` gem has been deprecated.
|
3
|
+
|
4
|
+
Please replace change your Gemfile's reference from `json-matchers` to
|
5
|
+
`json_matchers`.
|
6
|
+
WARNING
|
7
|
+
|
8
|
+
require "json_matchers"
|
9
|
+
|
10
|
+
if defined?(RSpec)
|
11
|
+
require "json_matchers/rspec"
|
12
|
+
end
|
data/lib/json/matchers.rb
CHANGED
@@ -1,20 +1,12 @@
|
|
1
|
-
|
2
|
-
require "json/matchers
|
3
|
-
require "json/matchers/errors"
|
4
|
-
require "active_support/all"
|
1
|
+
warn <<-WARNING
|
2
|
+
DEPRECATED: requiring the library via `require "json/matchers"` is deprecated.
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
To include the library, please add `require "json_matchers/rspec"` to your
|
5
|
+
`spec/spec_helper.rb`.
|
6
|
+
WARNING
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
def self.path_to_schema(schema_name)
|
13
|
-
Pathname(schema_root).join("#{schema_name}.json")
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
8
|
+
require "json_matchers"
|
17
9
|
|
18
10
|
if defined?(RSpec)
|
19
|
-
require "
|
11
|
+
require "json_matchers/rspec"
|
20
12
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "json_matchers/version"
|
2
|
+
require "json_matchers/matcher"
|
3
|
+
require "json_matchers/errors"
|
4
|
+
require "active_support/all"
|
5
|
+
|
6
|
+
module JsonMatchers
|
7
|
+
mattr_accessor :schema_root
|
8
|
+
|
9
|
+
self.schema_root = "#{Dir.pwd}/spec/support/api/schemas"
|
10
|
+
|
11
|
+
def self.path_to_schema(schema_name)
|
12
|
+
Pathname(schema_root).join("#{schema_name}.json")
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "json-schema"
|
2
|
+
|
3
|
+
module JsonMatchers
|
4
|
+
class Matcher
|
5
|
+
def initialize(schema_path, **options)
|
6
|
+
@schema_path = schema_path
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(response)
|
11
|
+
@response = response
|
12
|
+
|
13
|
+
validator_options = {
|
14
|
+
strict: true,
|
15
|
+
}.merge(options)
|
16
|
+
|
17
|
+
JSON::Validator.validate!(
|
18
|
+
schema_path.to_s,
|
19
|
+
response.body,
|
20
|
+
validator_options,
|
21
|
+
)
|
22
|
+
rescue JSON::Schema::ValidationError => ex
|
23
|
+
@validation_failure_message = ex.message
|
24
|
+
false
|
25
|
+
rescue JSON::ParserError
|
26
|
+
raise InvalidSchemaError
|
27
|
+
end
|
28
|
+
|
29
|
+
def validation_failure_message
|
30
|
+
@validation_failure_message.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
attr_reader :schema_path, :options
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require "json_matchers"
|
2
|
+
|
3
|
+
module JsonMatchers
|
4
|
+
class RSpec < SimpleDelegator
|
5
|
+
attr_reader :schema_name
|
6
|
+
|
7
|
+
def initialize(schema_name, **options)
|
8
|
+
@schema_name = schema_name
|
9
|
+
|
10
|
+
super(JsonMatchers::Matcher.new(schema_path, options))
|
11
|
+
end
|
12
|
+
|
13
|
+
def failure_message(response)
|
14
|
+
<<-FAIL.strip_heredoc
|
15
|
+
expected
|
16
|
+
|
17
|
+
#{response.body}
|
18
|
+
|
19
|
+
to match schema "#{schema_name}":
|
20
|
+
|
21
|
+
#{schema_body}
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
#{validation_failure_message}
|
26
|
+
|
27
|
+
FAIL
|
28
|
+
end
|
29
|
+
|
30
|
+
def failure_message_when_negated(response)
|
31
|
+
<<-FAIL.strip_heredoc
|
32
|
+
expected
|
33
|
+
|
34
|
+
#{response.body}
|
35
|
+
|
36
|
+
not to match schema "#{schema_name}":
|
37
|
+
|
38
|
+
#{schema_body}
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
#{validation_failure_message}
|
43
|
+
|
44
|
+
FAIL
|
45
|
+
end
|
46
|
+
|
47
|
+
def schema_path
|
48
|
+
JsonMatchers.path_to_schema(schema_name)
|
49
|
+
end
|
50
|
+
|
51
|
+
def schema_body
|
52
|
+
File.read(schema_path)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
if RSpec.respond_to?(:configure)
|
58
|
+
RSpec::Matchers.define :match_response_schema do |schema_name, **options|
|
59
|
+
matcher = JsonMatchers::RSpec.new(schema_name, options)
|
60
|
+
|
61
|
+
match do |response|
|
62
|
+
matcher.matches?(response)
|
63
|
+
end
|
64
|
+
|
65
|
+
if respond_to?(:failure_message)
|
66
|
+
failure_message do |response|
|
67
|
+
matcher.failure_message(response)
|
68
|
+
end
|
69
|
+
|
70
|
+
failure_message_when_negated do |response|
|
71
|
+
matcher.failure_message_when_negated(response)
|
72
|
+
end
|
73
|
+
else
|
74
|
+
failure_message_for_should do |response|
|
75
|
+
matcher.failure_message(response)
|
76
|
+
end
|
77
|
+
|
78
|
+
failure_message_for_should_not do |response|
|
79
|
+
matcher.failure_message_when_negated(response)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
describe
|
1
|
+
describe JsonMatchers, "#match_response_schema" do
|
2
2
|
it "fails with an invalid JSON body" do
|
3
3
|
create_schema("foo", "")
|
4
4
|
|
5
5
|
expect {
|
6
6
|
expect(response_for("")).to match_response_schema("foo")
|
7
|
-
}.to raise_error(
|
7
|
+
}.to raise_error(JsonMatchers::InvalidSchemaError)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "does not fail with an empty JSON body" do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module FileHelpers
|
2
|
-
ORIGINAL_SCHEMA_ROOT =
|
2
|
+
ORIGINAL_SCHEMA_ROOT = JsonMatchers.schema_root
|
3
3
|
|
4
4
|
def create_schema(name, json)
|
5
5
|
File.open("#{schema_root}/#{name}.json", "w") do |file|
|
@@ -23,7 +23,7 @@ module FileHelpers
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def schema_root
|
26
|
-
|
26
|
+
JsonMatchers.schema_root
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -31,12 +31,12 @@ RSpec.configure do |config|
|
|
31
31
|
config.include FileHelpers
|
32
32
|
|
33
33
|
config.around do |example|
|
34
|
-
|
35
|
-
FileUtils.mkdir_p(
|
34
|
+
JsonMatchers.schema_root = File.join(Dir.pwd, "spec", "fixtures", "schemas")
|
35
|
+
FileUtils.mkdir_p(JsonMatchers.schema_root)
|
36
36
|
|
37
37
|
example.run
|
38
38
|
|
39
|
-
FileUtils.rm_rf(
|
40
|
-
|
39
|
+
FileUtils.rm_rf(JsonMatchers.schema_root)
|
40
|
+
JsonMatchers.schema_root = FileHelpers::ORIGINAL_SCHEMA_ROOT
|
41
41
|
end
|
42
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Doyle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -112,12 +112,14 @@ files:
|
|
112
112
|
- README.md
|
113
113
|
- Rakefile
|
114
114
|
- json-matchers.gemspec
|
115
|
+
- lib/json-matchers.rb
|
115
116
|
- lib/json/matchers.rb
|
116
|
-
- lib/
|
117
|
-
- lib/
|
118
|
-
- lib/
|
119
|
-
- lib/
|
120
|
-
-
|
117
|
+
- lib/json_matchers.rb
|
118
|
+
- lib/json_matchers/errors.rb
|
119
|
+
- lib/json_matchers/matcher.rb
|
120
|
+
- lib/json_matchers/rspec.rb
|
121
|
+
- lib/json_matchers/version.rb
|
122
|
+
- spec/json_matchers/match_response_schema_spec.rb
|
121
123
|
- spec/spec_helper.rb
|
122
124
|
- spec/support/file_helpers.rb
|
123
125
|
homepage: https://github.com/thoughtbot/json-matchers
|
@@ -145,7 +147,7 @@ signing_key:
|
|
145
147
|
specification_version: 4
|
146
148
|
summary: Validate your Rails JSON API's JSON
|
147
149
|
test_files:
|
148
|
-
- spec/
|
150
|
+
- spec/json_matchers/match_response_schema_spec.rb
|
149
151
|
- spec/spec_helper.rb
|
150
152
|
- spec/support/file_helpers.rb
|
151
153
|
has_rdoc:
|
data/lib/json/matchers/errors.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require "json-schema"
|
2
|
-
|
3
|
-
module JSON
|
4
|
-
module Matchers
|
5
|
-
class Matcher
|
6
|
-
def initialize(schema_path, **options)
|
7
|
-
@schema_path = schema_path
|
8
|
-
@options = options
|
9
|
-
end
|
10
|
-
|
11
|
-
def matches?(response)
|
12
|
-
@response = response
|
13
|
-
|
14
|
-
validator_options = {
|
15
|
-
strict: true,
|
16
|
-
}.merge(options)
|
17
|
-
|
18
|
-
JSON::Validator.validate!(
|
19
|
-
schema_path.to_s,
|
20
|
-
response.body,
|
21
|
-
validator_options,
|
22
|
-
)
|
23
|
-
rescue JSON::Schema::ValidationError => ex
|
24
|
-
@validation_failure_message = ex.message
|
25
|
-
false
|
26
|
-
rescue JSON::ParserError
|
27
|
-
raise InvalidSchemaError
|
28
|
-
end
|
29
|
-
|
30
|
-
def validation_failure_message
|
31
|
-
@validation_failure_message.to_s
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
attr_reader :schema_path, :options
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
data/lib/json/matchers/rspec.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module JSON
|
2
|
-
module Matchers
|
3
|
-
class RSpec < SimpleDelegator
|
4
|
-
attr_reader :schema_name
|
5
|
-
|
6
|
-
def initialize(schema_name, **options)
|
7
|
-
@schema_name = schema_name
|
8
|
-
|
9
|
-
super(JSON::Matchers::Matcher.new(schema_path, options))
|
10
|
-
end
|
11
|
-
|
12
|
-
def failure_message(response)
|
13
|
-
<<-FAIL.strip_heredoc
|
14
|
-
expected
|
15
|
-
|
16
|
-
#{response.body}
|
17
|
-
|
18
|
-
to match schema "#{schema_name}":
|
19
|
-
|
20
|
-
#{schema_body}
|
21
|
-
|
22
|
-
---
|
23
|
-
|
24
|
-
#{validation_failure_message}
|
25
|
-
|
26
|
-
FAIL
|
27
|
-
end
|
28
|
-
|
29
|
-
def failure_message_when_negated(response)
|
30
|
-
<<-FAIL.strip_heredoc
|
31
|
-
expected
|
32
|
-
|
33
|
-
#{response.body}
|
34
|
-
|
35
|
-
not to match schema "#{schema_name}":
|
36
|
-
|
37
|
-
#{schema_body}
|
38
|
-
|
39
|
-
---
|
40
|
-
|
41
|
-
#{validation_failure_message}
|
42
|
-
|
43
|
-
FAIL
|
44
|
-
end
|
45
|
-
|
46
|
-
def schema_path
|
47
|
-
JSON::Matchers.path_to_schema(schema_name)
|
48
|
-
end
|
49
|
-
|
50
|
-
def schema_body
|
51
|
-
File.read(schema_path)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
if RSpec.respond_to?(:configure)
|
58
|
-
RSpec::Matchers.define :match_response_schema do |schema_name, **options|
|
59
|
-
matcher = JSON::Matchers::RSpec.new(schema_name, options)
|
60
|
-
|
61
|
-
match do |response|
|
62
|
-
matcher.matches?(response)
|
63
|
-
end
|
64
|
-
|
65
|
-
if respond_to?(:failure_message)
|
66
|
-
failure_message do |response|
|
67
|
-
matcher.failure_message(response)
|
68
|
-
end
|
69
|
-
|
70
|
-
failure_message_when_negated do |response|
|
71
|
-
matcher.failure_message_when_negated(response)
|
72
|
-
end
|
73
|
-
else
|
74
|
-
failure_message_for_should do |response|
|
75
|
-
matcher.failure_message(response)
|
76
|
-
end
|
77
|
-
|
78
|
-
failure_message_for_should_not do |response|
|
79
|
-
matcher.failure_message_when_negated(response)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|