json_matchers 0.5.1 → 0.6.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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.travis.yml +1 -1
- data/Gemfile +2 -0
- data/NEWS.md +7 -0
- data/README.md +16 -1
- data/lib/json_matchers/matcher.rb +9 -3
- data/lib/json_matchers/version.rb +1 -1
- data/spec/json_matchers/match_response_schema_spec.rb +16 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7837005397def477004c328fc25ebf08f3442338
|
4
|
+
data.tar.gz: 6eabcabe3f0317d35ed02980b67d615e609933cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 463f19a601e24f35a43ca210636c9e68f80c6f33b5c3e402d4116a5e3bd9c60748a4df8755da1edbde4d570b48453a15bbcd76dccc42d3a643a8d26fe87f5820
|
7
|
+
data.tar.gz: fba3346e93283c75f917bfa3ee9fd49a1680f1767ef97a701a439eb9aed4596400b929c78b95c0effffc59547eede894a2d1ae67710297ee2e8eef49632f7640
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.0
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/NEWS.md
CHANGED
data/README.md
CHANGED
@@ -56,7 +56,7 @@ Define your [JSON Schema](http://json-schema.org/example1.html) in the schema di
|
|
56
56
|
}
|
57
57
|
```
|
58
58
|
|
59
|
-
Then, validate
|
59
|
+
Then, validate `response` against your schema with `match_response_schema`
|
60
60
|
|
61
61
|
```ruby
|
62
62
|
# spec/requests/posts_spec.rb
|
@@ -71,6 +71,21 @@ describe "GET /posts" do
|
|
71
71
|
end
|
72
72
|
```
|
73
73
|
|
74
|
+
Alternatively, `match_response_schema` accepts a string:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# spec/requests/posts_spec.rb
|
78
|
+
|
79
|
+
describe "GET /posts" do
|
80
|
+
it "returns Posts" do
|
81
|
+
get posts_path, format: :json
|
82
|
+
|
83
|
+
expect(response.status).to eq 200
|
84
|
+
expect(response.body).to match_json_schema("posts")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
74
89
|
### Passing options to the validator
|
75
90
|
|
76
91
|
The matcher accepts options, which it passes to the validator:
|
@@ -8,11 +8,9 @@ module JsonMatchers
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def matches?(response)
|
11
|
-
@response = response
|
12
|
-
|
13
11
|
JSON::Validator.validate!(
|
14
12
|
schema_path.to_s,
|
15
|
-
response.
|
13
|
+
json_from(response).to_s,
|
16
14
|
options,
|
17
15
|
)
|
18
16
|
rescue JSON::Schema::ValidationError => ex
|
@@ -29,5 +27,13 @@ module JsonMatchers
|
|
29
27
|
private
|
30
28
|
|
31
29
|
attr_reader :schema_path, :options
|
30
|
+
|
31
|
+
def json_from(response)
|
32
|
+
if response.respond_to?(:body)
|
33
|
+
response.body
|
34
|
+
else
|
35
|
+
response
|
36
|
+
end
|
37
|
+
end
|
32
38
|
end
|
33
39
|
end
|
@@ -39,6 +39,22 @@ describe JsonMatchers, "#match_response_schema" do
|
|
39
39
|
to match_response_schema("foo_schema", strict: false)
|
40
40
|
end
|
41
41
|
|
42
|
+
it "validates a JSON string" do
|
43
|
+
create_schema("foo_schema", {
|
44
|
+
"type" => "object",
|
45
|
+
"required" => [
|
46
|
+
"id",
|
47
|
+
],
|
48
|
+
"properties" => {
|
49
|
+
"id" => { "type" => "number" },
|
50
|
+
},
|
51
|
+
"additionalProperties" => false,
|
52
|
+
})
|
53
|
+
|
54
|
+
expect(response_for({ "id" => 1 }).body).
|
55
|
+
to match_response_schema("foo_schema")
|
56
|
+
end
|
57
|
+
|
42
58
|
it "fails when the body contains a property with the wrong type" do
|
43
59
|
create_schema("foo_schema", {
|
44
60
|
"type" => "object",
|
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.6.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: 2016-
|
11
|
+
date: 2016-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -103,6 +103,7 @@ extra_rdoc_files: []
|
|
103
103
|
files:
|
104
104
|
- ".gitignore"
|
105
105
|
- ".rspec"
|
106
|
+
- ".ruby-version"
|
106
107
|
- ".travis.yml"
|
107
108
|
- CONTRIBUTING.md
|
108
109
|
- Gemfile
|
@@ -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.
|
143
|
+
rubygems_version: 2.5.1
|
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:
|