json-matchers 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be6efc3180dbb7fd3f49c438edb2c1d5c096342d
4
+ data.tar.gz: 1808696767e661d8393224272dd30a7cc5dd9a23
5
+ SHA512:
6
+ metadata.gz: ed7c67b4b54f84cf64cabe106c54aac6da88fda5432e948e7bf993a8e5caef1aa20fa3a8a151eedc0efff256d6646cb12c4980df3161d1d91b44fe60e40b4c29
7
+ data.tar.gz: 6575e174274e7be924af6dce791b590d10d6f4fb16d392c24e497afe8c171c7a892f0238cc37f3f1d3261c98ad99d66ebf691221f0312c3132a0e80f23647d33
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/fixtures/schemas
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shoulda-matchers-json.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sean Doyle
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/NEWS.md ADDED
@@ -0,0 +1,5 @@
1
+ # 0.0.1
2
+
3
+ ## Features
4
+
5
+ * Validate your Rails response JSON with `match_response_schema`
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # JSON::Matchers
2
+
3
+ Validate the JSON returned by your Rails JSON APIs
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'json-matchers', require: false
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install json-matchers
20
+
21
+ ## Usage
22
+
23
+ Inspired by [Validating JSON Schemas with an RSpec Matcher](http://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher)
24
+
25
+ First, include it in your `spec_helper`:
26
+
27
+ ```ruby
28
+ # spec/spec_helper.rb
29
+
30
+ require "json/matchers"
31
+ ```
32
+
33
+ Define your [JSON Schema](http://json-schema.org/example1.html) in the schema directory:
34
+
35
+ ```json
36
+ # spec/support/api/schemas/posts.json
37
+
38
+ {
39
+ "type": "object",
40
+ "required": ["posts"],
41
+ "properties": {
42
+ "type": "object",
43
+ "required": ["id", "title", "body"],
44
+ "properties": {
45
+ "id": { "type": "integer" },
46
+ "title": { "type": "string" },
47
+ "body": { "type": "string" }
48
+ }
49
+ }
50
+ }
51
+ ```
52
+
53
+ Then, validate your response against your schema with `match_response_schema`
54
+
55
+ ```ruby
56
+ # spec/requests/posts_spec.rb
57
+
58
+ describe "GET /posts" do
59
+ it "returns Posts" do
60
+ get posts_path, format: :json
61
+
62
+ expect(response.status).to eq 200
63
+ expect(response).to match_response_schema("posts")
64
+ end
65
+ end
66
+ ```
67
+
68
+ You can even embed schemas inside other schemas!
69
+
70
+ First, declare the singular version of your schema.
71
+
72
+ ```json
73
+ # spec/support/api/schemas/post.json
74
+
75
+ {
76
+ "type": "object",
77
+ "required": ["id", "title", "body"],
78
+ "properties": {
79
+ "id": { "type": "integer" },
80
+ "title": { "type": "string" },
81
+ "body": { "type": "string" }
82
+ }
83
+ }
84
+ ```
85
+
86
+ Then, when you declare your collection schema, embed the singular schema with
87
+ `ERB` and `schema_for`!
88
+
89
+ ```json
90
+ # spec/support/api/schemas/posts.json
91
+
92
+ {
93
+ "type": "object",
94
+ "required": ["posts"],
95
+ "properties": {
96
+ "posts": {
97
+ "type": "array",
98
+ "items": <%= schema_for("post") %>
99
+ }
100
+ }
101
+ }
102
+ ```
103
+
104
+ ## Configuration
105
+
106
+ By default, the schema directory is `spec/support/api/schemas`.
107
+
108
+ This can be configured via `JSON::Matchers.schema_root`.
109
+
110
+
111
+ ```ruby
112
+ # spec/support/shoulda-matchers-json.rb
113
+
114
+ JSON::Matchers.schema_root = "docs/api/schemas"
115
+ ```
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it ( https://github.com/[my-github-username]/shoulda-matchers-json/fork )
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ end
7
+
8
+ task(:default).clear
9
+ task default: [:spec]
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "json/matchers/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json-matchers"
8
+ spec.version = JSON::Matchers::VERSION
9
+ spec.authors = ["Sean Doyle"]
10
+ spec.email = ["seandoyle@thoughtbot.com"]
11
+ spec.summary = %q{Validate your Rails JSON API's JSON}
12
+ spec.description = %q{Validate your Rails JSON API's JSON}
13
+ spec.homepage = "https://github.com/seanpdoyle/json-matchers"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency("json-schema", ">= 1.2.1")
22
+ spec.add_dependency("activesupport", '>= 3.0.0')
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec-rails", ">= 2.0"
28
+ end
@@ -0,0 +1,24 @@
1
+ require "json/matchers/version"
2
+ require "json/matchers/matcher"
3
+ require "json/matchers/schema_parser"
4
+ require "json/matchers/errors"
5
+ require "active_support/all"
6
+
7
+ module JSON
8
+ module Matchers
9
+ mattr_accessor :schema_root
10
+
11
+ self.schema_root = "#{Dir.pwd}/spec/support/api/schemas"
12
+
13
+ def match_response_schema(schema_name)
14
+ schema_parser = SchemaParser.new(schema_root)
15
+
16
+ Matcher.new(schema_parser.schema_for(schema_name))
17
+ end
18
+ alias match_json_schema match_response_schema
19
+ end
20
+ end
21
+
22
+ if defined?(RSpec)
23
+ require "json/matchers/rspec"
24
+ end
@@ -0,0 +1,7 @@
1
+ module JSON
2
+ module Matchers
3
+ InvalidError = Class.new(StandardError)
4
+ DoesNotMatch = Class.new(InvalidError)
5
+ MissingSchema = Class.new(Errno::ENOENT)
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ require "json-schema"
2
+
3
+ module JSON
4
+ module Matchers
5
+ Matcher = Struct.new(:schema) do
6
+ def matches?(response)
7
+ JSON::Validator.validate!(json_schema, response.body, strict: true)
8
+ rescue JSON::Schema::ValidationError
9
+ raise DoesNotMatch, response.body
10
+ rescue JSON::ParserError
11
+ raise InvalidError
12
+ end
13
+
14
+ private
15
+
16
+ def json_schema
17
+ JSON.parse(schema)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ if RSpec.respond_to?(:configure)
2
+ RSpec.configure do |config|
3
+ config.include JSON::Matchers
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ module JSON
2
+ module Matchers
3
+ class SchemaParser
4
+
5
+ attr_reader :schema_path
6
+
7
+ def initialize(schema_path)
8
+ @schema_path = Pathname(schema_path)
9
+ end
10
+
11
+ def schema_for(schema_name)
12
+ file = schema_path.join("#{schema_name}.json")
13
+
14
+ if file.exist?
15
+ ERB.new(file.read).result(binding)
16
+ else
17
+ raise MissingSchema, file.to_s
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module JSON
2
+ module Matchers
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,59 @@
1
+ describe JSON::Matchers, "#match_response_schema" do
2
+ it "fails with an invalid JSON body" do
3
+ create_schema("foo", "")
4
+
5
+ expect {
6
+ expect(response_for("")).to match_response_schema("foo")
7
+ }.to raise_error(JSON::Matchers::InvalidError)
8
+ end
9
+
10
+ it "does not fail with an empty JSON body" do
11
+ create_schema("foo", {})
12
+
13
+ expect(response_for({})).to match_response_schema("foo")
14
+ end
15
+
16
+ it "fails when the body is missing a required property" do
17
+ create_schema("array_schema", {
18
+ type: "object",
19
+ required: ["foo"],
20
+ })
21
+
22
+ expect {
23
+ expect(response_for({})).to match_response_schema("array_schema")
24
+ }.to raise_error(JSON::Matchers::DoesNotMatch, /{}/)
25
+ end
26
+
27
+ it "fails when the body contains a property with the wrong type" do
28
+ create_schema("array_schema", {
29
+ type: "object",
30
+ properties: {
31
+ foo: { type: "string" }
32
+ }
33
+ })
34
+
35
+ expect {
36
+ expect(response_for({foo: 1})).to match_response_schema("array_schema")
37
+ }.to raise_error(JSON::Matchers::DoesNotMatch, /{"foo":1}/)
38
+ end
39
+
40
+ it "does not fail when the schema matches" do
41
+ create_schema("array_schema", {
42
+ type: "array",
43
+ items: { type: "string" }
44
+ })
45
+
46
+ expect(response_for(["valid"])).to match_response_schema("array_schema")
47
+ end
48
+
49
+ it "supports ERB" do
50
+ create_schema "array_schema", <<-JSON.strip_heredoc
51
+ {
52
+ "type": "array",
53
+ "items": <%= { type: "string" }.to_json %>
54
+ }
55
+ JSON
56
+
57
+ expect(response_for(["valid"])).to match_response_schema("array_schema")
58
+ end
59
+ end
@@ -0,0 +1,68 @@
1
+ require "json/matchers/schema_parser"
2
+
3
+ describe JSON::Matchers::SchemaParser do
4
+ describe "#schema_for" do
5
+ it "returns the JSON string for a given schema" do
6
+ create_schema("foo", { type: "array" })
7
+
8
+ schema_parser = JSON::Matchers::SchemaParser.new(schema_root)
9
+
10
+ expect(schema_parser.schema_for("foo")).to eq '{"type":"array"}'
11
+ end
12
+
13
+ it "can embed other schemas" do
14
+ create_schema "post", <<-JSON.strip
15
+ {
16
+ "type": "object",
17
+ "required": ["id", "title", "body"],
18
+ "properties": {
19
+ "id": { "type": "integer" },
20
+ "title": { "type": "string" },
21
+ "body": { "type": "string" }
22
+ }
23
+ }
24
+ JSON
25
+ create_schema "posts", <<-JSON.strip
26
+ {
27
+ "type": "object",
28
+ "required": ["posts"],
29
+ "properties": {
30
+ "posts": {
31
+ "type": "array",
32
+ "items": <%= schema_for("post") %>
33
+ }
34
+ }
35
+ }
36
+ JSON
37
+
38
+ schema_parser = JSON::Matchers::SchemaParser.new(schema_root)
39
+ schema = schema_parser.schema_for("posts")
40
+
41
+ expect(JSON.parse(schema)).to eq({
42
+ "type" => "object",
43
+ "required" => ["posts"],
44
+ "properties" => {
45
+ "posts" => {
46
+ "type" => "array",
47
+ "items" => {
48
+ "type" => "object",
49
+ "required" => ["id", "title", "body"],
50
+ "properties" => {
51
+ "id" => { "type" => "integer" },
52
+ "title" => { "type" => "string" },
53
+ "body" => { "type" => "string" }
54
+ }
55
+ }
56
+ }
57
+ }
58
+ })
59
+ end
60
+
61
+ it "fails when the schema is missing" do
62
+ schema_parser = JSON::Matchers::SchemaParser.new(schema_root)
63
+ expect {
64
+ schema_parser.schema_for("missing")
65
+ }.to raise_error(JSON::Matchers::MissingSchema, /missing.json/)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,15 @@
1
+ require "json/matchers"
2
+
3
+ Dir["./spec/support/*"].each { |file| require file }
4
+
5
+ RSpec.configure do |config|
6
+ config.include JSON::Matchers
7
+ config.expect_with :rspec do |expectations|
8
+
9
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
10
+ end
11
+
12
+ config.mock_with :rspec do |mocks|
13
+ mocks.verify_partial_doubles = true
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ module FileHelpers
2
+ def create_schema(name, json)
3
+ File.open("#{schema_root}/#{name}.json", "w") do |file|
4
+ case json
5
+ when NilClass, String
6
+ file.write(json.to_s)
7
+ else
8
+ file.write(json.to_json)
9
+ end
10
+ end
11
+ end
12
+
13
+ def response_for(json)
14
+ response_body = case json
15
+ when String, NilClass
16
+ json.to_s
17
+ else
18
+ json.to_json
19
+ end
20
+ double(body: response_body)
21
+ end
22
+
23
+ def schema_root
24
+ JSON::Matchers.schema_root
25
+ end
26
+ end
27
+
28
+ RSpec.configure do |config|
29
+ config.include FileHelpers
30
+
31
+ config.around do |example|
32
+ original_schema_root = JSON::Matchers.schema_root
33
+ JSON::Matchers.schema_root = "#{Dir.pwd}/spec/fixtures/schemas"
34
+ FileUtils.mkdir_p(JSON::Matchers.schema_root)
35
+
36
+ example.run
37
+
38
+ FileUtils.rm_rf(JSON::Matchers.schema_root)
39
+ JSON::Matchers.schema_root = original_schema_root
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean Doyle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ description: Validate your Rails JSON API's JSON
98
+ email:
99
+ - seandoyle@thoughtbot.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - NEWS.md
110
+ - README.md
111
+ - Rakefile
112
+ - json-matchers.gemspec
113
+ - lib/json/matchers.rb
114
+ - lib/json/matchers/errors.rb
115
+ - lib/json/matchers/matcher.rb
116
+ - lib/json/matchers/rspec.rb
117
+ - lib/json/matchers/schema_parser.rb
118
+ - lib/json/matchers/version.rb
119
+ - spec/json/matchers/match_response_schema_spec.rb
120
+ - spec/json/matchers/schema_parser_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/support/file_helpers.rb
123
+ homepage: https://github.com/seanpdoyle/json-matchers
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.4.1
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Validate your Rails JSON API's JSON
147
+ test_files:
148
+ - spec/json/matchers/match_response_schema_spec.rb
149
+ - spec/json/matchers/schema_parser_spec.rb
150
+ - spec/spec_helper.rb
151
+ - spec/support/file_helpers.rb
152
+ has_rdoc: