json-matchers 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -2
- data/CHANGELOG.md +5 -0
- data/CONTRIBUTING.md +10 -0
- data/LICENSE.txt +1 -0
- data/README.md +56 -5
- data/json-matchers.gemspec +2 -2
- data/lib/json/matchers/matcher.rb +19 -2
- data/lib/json/matchers/rspec.rb +4 -4
- data/lib/json/matchers/version.rb +1 -1
- data/spec/json/matchers/match_response_schema_spec.rb +36 -18
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2bc10af4131d3f0ed42c314a9a9647547051e6f
|
4
|
+
data.tar.gz: bd823542ce64cab597d9f9689a7b49bce21494e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ce7b02e972556b4d961a245b4766e537d21b7adef732c4f0cab6aecc990ca01258a062acbb73a6773fc051769f374ead55a474aeb402a4d8311fdc2f52a4a30
|
7
|
+
data.tar.gz: e8b8f5c80dfe722447f5bea3cf95c369b23c1eab49439383e5a31a87d2ab27126a5ac1d6b38d685f31883629aa4e558e4d4bce06d3589c384323b419b6ee19ac
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Contributing to json-matchers:
|
2
|
+
|
3
|
+
1. Fork the [official repository](https://github.com/thoughtbot/json-matchers/tree/master).
|
4
|
+
2. Make your changes in a topic branch.
|
5
|
+
3. Send a pull request.
|
6
|
+
|
7
|
+
Notes:
|
8
|
+
|
9
|
+
* Contributions without tests won't be accepted.
|
10
|
+
* Please don't update the Gem version.
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -65,6 +65,27 @@ describe "GET /posts" do
|
|
65
65
|
end
|
66
66
|
```
|
67
67
|
|
68
|
+
### Passing options to the validator
|
69
|
+
|
70
|
+
The matcher accepts options, which it'll pass to the validator:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# spec/requests/posts_spec.rb
|
74
|
+
|
75
|
+
describe "GET /posts" do
|
76
|
+
it "returns Posts" do
|
77
|
+
get posts_path, format: :json
|
78
|
+
|
79
|
+
expect(response.status).to eq 200
|
80
|
+
expect(response).to match_response_schema("posts", strict: false)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
A list of available options can be found [here][options]
|
86
|
+
|
87
|
+
[options]: https://github.com/ruby-json-schema/json-schema/blob/2.2.4/lib/json-schema/validator.rb#L160-L162
|
88
|
+
|
68
89
|
### Embedding other Schemas
|
69
90
|
|
70
91
|
To DRY up your schema definitions, use JSON schema's `$ref`.
|
@@ -124,8 +145,38 @@ JSON::Matchers.schema_root = "docs/api/schemas"
|
|
124
145
|
|
125
146
|
## Contributing
|
126
147
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
148
|
+
Please see [CONTRIBUTING].
|
149
|
+
|
150
|
+
`json-matchers` was inspired by [Validating JSON Schemas with an
|
151
|
+
RSpec Matcher][blog post] by Laila Winner.
|
152
|
+
|
153
|
+
`json-matchers` was written and is maintained by Sean Doyle.
|
154
|
+
|
155
|
+
Many improvements and bugfixes were contributed by the [open source community].
|
156
|
+
|
157
|
+
[blog post]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher
|
158
|
+
[CONTRIBUTING]: https://github.com/thoughtbot/json-matchers/blob/master/CONTRIBUTING.md
|
159
|
+
[open source community]: https://github.com/thoughtbot/json-matchers/graphs/contributors
|
160
|
+
|
161
|
+
## License
|
162
|
+
|
163
|
+
json-matchers is Copyright © 2015 Sean Doyle and thoughtbot.
|
164
|
+
|
165
|
+
It is free software, and may be redistributed under the terms specified in the
|
166
|
+
[LICENSE] file.
|
167
|
+
|
168
|
+
[LICENSE]: LICENSE.txt
|
169
|
+
|
170
|
+
## About thoughtbot
|
171
|
+
|
172
|
+
![thoughtbot](https://thoughtbot.com/logo.png)
|
173
|
+
|
174
|
+
`json-matchers` is maintained and funded by thoughtbot, inc.
|
175
|
+
The names and logos for thoughtbot are trademarks of thoughtbot, inc.
|
176
|
+
|
177
|
+
We love open source software!
|
178
|
+
See [our other projects][community].
|
179
|
+
We are [available for hire][hire].
|
180
|
+
|
181
|
+
[community]: https://thoughtbot.com/community?utm_source=github
|
182
|
+
[hire]: https://thoughtbot.com?utm_source=github
|
data/json-matchers.gemspec
CHANGED
@@ -7,10 +7,10 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "json-matchers"
|
8
8
|
spec.version = JSON::Matchers::VERSION
|
9
9
|
spec.authors = ["Sean Doyle"]
|
10
|
-
spec.email = ["
|
10
|
+
spec.email = ["sean.p.doyle24@gmail.com"]
|
11
11
|
spec.summary = %q{Validate your Rails JSON API's JSON}
|
12
12
|
spec.description = %q{Validate your Rails JSON API's JSON}
|
13
|
-
spec.homepage = "https://github.com/
|
13
|
+
spec.homepage = "https://github.com/thoughtbot/json-matchers"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -2,11 +2,24 @@ require "json-schema"
|
|
2
2
|
|
3
3
|
module JSON
|
4
4
|
module Matchers
|
5
|
-
Matcher
|
5
|
+
class Matcher
|
6
|
+
def initialize(schema_path, **options)
|
7
|
+
@schema_path = schema_path
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
6
11
|
def matches?(response)
|
7
12
|
@response = response
|
8
13
|
|
9
|
-
|
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
|
+
)
|
10
23
|
rescue JSON::Schema::ValidationError => ex
|
11
24
|
@validation_failure_message = ex.message
|
12
25
|
false
|
@@ -17,6 +30,10 @@ module JSON
|
|
17
30
|
def validation_failure_message
|
18
31
|
@validation_failure_message.to_s
|
19
32
|
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :schema_path, :options
|
20
37
|
end
|
21
38
|
end
|
22
39
|
end
|
data/lib/json/matchers/rspec.rb
CHANGED
@@ -3,10 +3,10 @@ module JSON
|
|
3
3
|
class RSpec < SimpleDelegator
|
4
4
|
attr_reader :schema_name
|
5
5
|
|
6
|
-
def initialize(schema_name)
|
6
|
+
def initialize(schema_name, **options)
|
7
7
|
@schema_name = schema_name
|
8
8
|
|
9
|
-
super(JSON::Matchers::Matcher.new(schema_path))
|
9
|
+
super(JSON::Matchers::Matcher.new(schema_path, options))
|
10
10
|
end
|
11
11
|
|
12
12
|
def failure_message(response)
|
@@ -55,8 +55,8 @@ module JSON
|
|
55
55
|
end
|
56
56
|
|
57
57
|
if RSpec.respond_to?(:configure)
|
58
|
-
RSpec::Matchers.define :match_response_schema do |schema_name|
|
59
|
-
matcher = JSON::Matchers::RSpec.new(schema_name)
|
58
|
+
RSpec::Matchers.define :match_response_schema do |schema_name, **options|
|
59
|
+
matcher = JSON::Matchers::RSpec.new(schema_name, options)
|
60
60
|
|
61
61
|
match do |response|
|
62
62
|
matcher.matches?(response)
|
@@ -15,34 +15,52 @@ describe JSON::Matchers, "#match_response_schema" do
|
|
15
15
|
|
16
16
|
it "fails when the body is missing a required property" do
|
17
17
|
create_schema("foo_schema", {
|
18
|
-
type
|
19
|
-
required
|
18
|
+
"type" => "object",
|
19
|
+
"required" => ["foo"],
|
20
20
|
})
|
21
21
|
|
22
22
|
expect(response_for({})).not_to match_response_schema("foo_schema")
|
23
23
|
end
|
24
24
|
|
25
|
+
it "accepts options for the validator" do
|
26
|
+
create_schema("foo_schema", {
|
27
|
+
"type" => "object",
|
28
|
+
"required" => [
|
29
|
+
"id",
|
30
|
+
],
|
31
|
+
"properties" => {
|
32
|
+
"id" => { "type" => "number" },
|
33
|
+
"title" => {"type" => "string"},
|
34
|
+
},
|
35
|
+
"additionalProperties" => false,
|
36
|
+
})
|
37
|
+
|
38
|
+
expect(response_for({ "id" => 1, "title" => "bar" })).
|
39
|
+
to match_response_schema("foo_schema", strict: false)
|
40
|
+
end
|
41
|
+
|
25
42
|
it "fails when the body contains a property with the wrong type" do
|
26
43
|
create_schema("foo_schema", {
|
27
|
-
type
|
28
|
-
properties
|
29
|
-
foo
|
44
|
+
"type" => "object",
|
45
|
+
"properties" => {
|
46
|
+
"foo" => { "type" => "string" },
|
30
47
|
}
|
31
48
|
})
|
32
49
|
|
33
|
-
expect(response_for(
|
50
|
+
expect(response_for("foo" => 1)).
|
51
|
+
not_to match_response_schema("foo_schema")
|
34
52
|
end
|
35
53
|
|
36
54
|
it "contains the body in the failure message" do
|
37
|
-
create_schema("foo", { type
|
55
|
+
create_schema("foo", { "type" => "array" })
|
38
56
|
|
39
57
|
expect {
|
40
|
-
expect(response_for(bar
|
58
|
+
expect(response_for("bar" => 5)).to match_response_schema("foo")
|
41
59
|
}.to raise_error(/{"bar":5}/)
|
42
60
|
end
|
43
61
|
|
44
62
|
it "contains the body in the failure message when negated" do
|
45
|
-
create_schema("foo", { type
|
63
|
+
create_schema("foo", { "type" => "array" })
|
46
64
|
|
47
65
|
expect {
|
48
66
|
expect(response_for([])).not_to match_response_schema("foo")
|
@@ -50,16 +68,16 @@ describe JSON::Matchers, "#match_response_schema" do
|
|
50
68
|
end
|
51
69
|
|
52
70
|
it "contains the schema in the failure message" do
|
53
|
-
schema = { type
|
71
|
+
schema = { "type" => "array" }
|
54
72
|
create_schema("foo", schema)
|
55
73
|
|
56
74
|
expect {
|
57
|
-
expect(response_for(bar
|
75
|
+
expect(response_for("bar" => 5)).to match_response_schema("foo")
|
58
76
|
}.to raise_error(/#{schema.to_json}/)
|
59
77
|
end
|
60
78
|
|
61
79
|
it "contains the schema in the failure message when negated" do
|
62
|
-
schema = { type
|
80
|
+
schema = { "type" => "array" }
|
63
81
|
create_schema("foo", schema)
|
64
82
|
|
65
83
|
expect {
|
@@ -69,8 +87,8 @@ describe JSON::Matchers, "#match_response_schema" do
|
|
69
87
|
|
70
88
|
it "does not fail when the schema matches" do
|
71
89
|
create_schema("array_schema", {
|
72
|
-
type
|
73
|
-
items
|
90
|
+
"type" => "array",
|
91
|
+
"items" => { "type" => "string" },
|
74
92
|
})
|
75
93
|
|
76
94
|
expect(response_for(["valid"])).to match_response_schema("array_schema")
|
@@ -81,16 +99,16 @@ describe JSON::Matchers, "#match_response_schema" do
|
|
81
99
|
"type" => "object",
|
82
100
|
"required" => ["foo"],
|
83
101
|
"properties" => {
|
84
|
-
"foo" => { "type" => "string" }
|
102
|
+
"foo" => { "type" => "string" },
|
85
103
|
}
|
86
104
|
})
|
87
105
|
create_schema("collection", {
|
88
106
|
"type" => "array",
|
89
|
-
"items" => { "$ref" => "single.json" }
|
107
|
+
"items" => { "$ref" => "single.json" },
|
90
108
|
})
|
91
109
|
|
92
|
-
valid_response = response_for([{foo
|
93
|
-
invalid_response = response_for([{foo
|
110
|
+
valid_response = response_for([{ "foo" => "is a string" }])
|
111
|
+
invalid_response = response_for([{ "foo" => 0 }])
|
94
112
|
|
95
113
|
expect(valid_response).to match_response_schema("collection")
|
96
114
|
expect(invalid_response).not_to match_response_schema("collection")
|
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.
|
4
|
+
version: 0.3.0
|
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-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -96,7 +96,7 @@ dependencies:
|
|
96
96
|
version: '2.0'
|
97
97
|
description: Validate your Rails JSON API's JSON
|
98
98
|
email:
|
99
|
-
-
|
99
|
+
- sean.p.doyle24@gmail.com
|
100
100
|
executables: []
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- ".rspec"
|
106
106
|
- ".travis.yml"
|
107
107
|
- CHANGELOG.md
|
108
|
+
- CONTRIBUTING.md
|
108
109
|
- Gemfile
|
109
110
|
- LICENSE.txt
|
110
111
|
- NEWS.md
|
@@ -119,7 +120,7 @@ files:
|
|
119
120
|
- spec/json/matchers/match_response_schema_spec.rb
|
120
121
|
- spec/spec_helper.rb
|
121
122
|
- spec/support/file_helpers.rb
|
122
|
-
homepage: https://github.com/
|
123
|
+
homepage: https://github.com/thoughtbot/json-matchers
|
123
124
|
licenses:
|
124
125
|
- MIT
|
125
126
|
metadata: {}
|
@@ -139,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
140
|
version: '0'
|
140
141
|
requirements: []
|
141
142
|
rubyforge_project:
|
142
|
-
rubygems_version: 2.4.
|
143
|
+
rubygems_version: 2.4.8
|
143
144
|
signing_key:
|
144
145
|
specification_version: 4
|
145
146
|
summary: Validate your Rails JSON API's JSON
|
@@ -147,3 +148,4 @@ test_files:
|
|
147
148
|
- spec/json/matchers/match_response_schema_spec.rb
|
148
149
|
- spec/spec_helper.rb
|
149
150
|
- spec/support/file_helpers.rb
|
151
|
+
has_rdoc:
|