json-schema-rspec 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +13 -0
- data/README.md +10 -0
- data/json-schema-rspec.gemspec +3 -3
- data/lib/json-schema-rspec/matchers/json_schema_matcher.rb +9 -4
- data/lib/json-schema-rspec/version.rb +1 -1
- data/spec/json-schema-rspec/matchers/json_schema_matcher_spec.rb +73 -0
- data/spec/spec_helper.rb +14 -0
- metadata +22 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 78897a15eee63f17fbe327166c1cc79a4cab7d98
|
4
|
+
data.tar.gz: 365ee5bf15d49a0d4a8c534122570babd2669613
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7bda5f7a71289c987070eef0212c03e12915cf8b5ac318a7c96c235a4f436a004279a1371dd1653f0695fb8f30a5c4d8ff4f00f6fd68dc9c1c892440538c564e
|
7
|
+
data.tar.gz: 23f7b5a39d5c14036eb49063736cb0dc941b9097e5e0568a134411184850c439b5bfaf885beda9101e0d526b773faed99672ca7bd1464d44eb830fe3c5ca01bd
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
0.0.2 / 2015-05-06
|
2
|
+
==================
|
3
|
+
|
4
|
+
* Updated to json-schema gem version to 2.5
|
5
|
+
* Added description method on matchers
|
6
|
+
* Allow for passing in validation options
|
7
|
+
* Added tests
|
8
|
+
|
9
|
+
0.0.1 / 2014-04-18
|
10
|
+
==================
|
11
|
+
|
12
|
+
* Initial commit
|
13
|
+
|
data/README.md
CHANGED
@@ -36,6 +36,16 @@ You can then write tests such as:
|
|
36
36
|
|
37
37
|
#failing spec
|
38
38
|
expect('[1, 2, 3]').to match_json_schema(:inline_schema)
|
39
|
+
|
40
|
+
#### Strict schema validation
|
41
|
+
|
42
|
+
If you wish to use strict schema validation you can do so by passing an additional argument to the matcher.
|
43
|
+
strict validation can be useful if you want to ensure there are no additional properties in the JSON than
|
44
|
+
those explicitly defined in your schema
|
45
|
+
|
46
|
+
```
|
47
|
+
expect(response.body).to match_json_schema(:my_schema, strict: true)
|
48
|
+
```
|
39
49
|
|
40
50
|
### Schema in a file
|
41
51
|
You can also use rails path utilities such as `Rails.root.join("spec/support/schemas/my_business_object.schema.json").to_s` when defining schema locations. This gem is backed by the [json-schema](http://github.com/hoxworth/json-schema) gem, so whatever that validator accepts for paths should work.
|
data/json-schema-rspec.gemspec
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
require File.expand_path('../lib/json-schema-rspec/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = ["Michael Jensen"]
|
6
|
-
gem.email = ["emjay1988@gmail.com"]
|
5
|
+
gem.authors = ["Sharethrough Engineering", "Michael Jensen"]
|
6
|
+
gem.email = ["engineers@sharethrough.com", "emjay1988@gmail.com"]
|
7
7
|
gem.description = %q{Adds RSpec matchers for validating JSON schemas}
|
8
8
|
gem.summary = %q{JSON Schema RSpec matchers}
|
9
9
|
gem.homepage = ""
|
@@ -15,5 +15,5 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Json::Schema::Rspec::VERSION
|
17
17
|
gem.add_dependency "rspec"
|
18
|
-
gem.add_dependency "json-schema", "~> 2.
|
18
|
+
gem.add_dependency "json-schema", "~> 2.5"
|
19
19
|
end
|
@@ -8,8 +8,9 @@ module JSON
|
|
8
8
|
end
|
9
9
|
|
10
10
|
class MatchJsonSchemaMatcher
|
11
|
-
def initialize(schema_name)
|
11
|
+
def initialize(schema_name, validation_opts = {})
|
12
12
|
@schema_name = schema_name
|
13
|
+
@validation_opts = validation_opts
|
13
14
|
end
|
14
15
|
|
15
16
|
def matches?(actual)
|
@@ -25,7 +26,7 @@ module JSON
|
|
25
26
|
" end"]
|
26
27
|
return false
|
27
28
|
end
|
28
|
-
@errors = JSON::Validator.fully_validate(schema_for_name(@schema_name), @actual)
|
29
|
+
@errors = JSON::Validator.fully_validate(schema_for_name(@schema_name), @actual, @validation_opts)
|
29
30
|
|
30
31
|
if @errors.any?
|
31
32
|
@errors.unshift("Expected JSON object to match schema identified by #{@schema_name}, #{@errors.count} errors in validating")
|
@@ -43,13 +44,17 @@ module JSON
|
|
43
44
|
"Expected JSON object not to match schema identified by #{@schema_name}"
|
44
45
|
end
|
45
46
|
|
47
|
+
def description
|
48
|
+
"match JSON schema identified by #{@schema_name}"
|
49
|
+
end
|
50
|
+
|
46
51
|
def schema_for_name(schema)
|
47
52
|
RSpec.configuration.json_schemas[schema]
|
48
53
|
end
|
49
54
|
end
|
50
55
|
|
51
|
-
def match_json_schema(schema_name)
|
52
|
-
MatchJsonSchemaMatcher.new(schema_name)
|
56
|
+
def match_json_schema(schema_name, validation_opts = {})
|
57
|
+
MatchJsonSchemaMatcher.new(schema_name, validation_opts)
|
53
58
|
end
|
54
59
|
end
|
55
60
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe JSON::SchemaMatchers::MatchJsonSchemaMatcher do
|
4
|
+
let(:valid_json) { '"hello world"' }
|
5
|
+
let(:invalid_json) { '{"key": "value"}' }
|
6
|
+
let(:inline_schema) { '{"type": "string"}' }
|
7
|
+
|
8
|
+
context 'without valid schema_name' do
|
9
|
+
let(:unconfigured_schema) { :unconfigured_schema }
|
10
|
+
specify 'matches fail' do
|
11
|
+
expect(valid_json).not_to match_json_schema(unconfigured_schema)
|
12
|
+
end
|
13
|
+
|
14
|
+
specify 'strict matches fail' do
|
15
|
+
expect(valid_json).not_to match_json_schema(unconfigured_schema, strict: true)
|
16
|
+
end
|
17
|
+
|
18
|
+
specify 'assigns a failure message' do
|
19
|
+
matcher = match_json_schema(unconfigured_schema)
|
20
|
+
expect(matcher.matches?(valid_json)).to eq(false)
|
21
|
+
expect(matcher.failure_message).to match(/^No schema defined for #{unconfigured_schema}/)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with valid schema_name' do
|
26
|
+
before :each do
|
27
|
+
RSpec.configuration.json_schemas[:inline_schema] = inline_schema
|
28
|
+
end
|
29
|
+
|
30
|
+
specify 'calls JSON::Validator' do
|
31
|
+
schema_value = inline_schema
|
32
|
+
json_value = valid_json
|
33
|
+
validation_options = {strict: true}
|
34
|
+
no_errors = []
|
35
|
+
|
36
|
+
expect(JSON::Validator).to receive(:fully_validate) do |schema_value_arg, json_value_arg, validation_options_arg|
|
37
|
+
expect(schema_value_arg).to eq(schema_value)
|
38
|
+
expect(json_value_arg).to eq(json_value)
|
39
|
+
expect(validation_options_arg).to eq(validation_options)
|
40
|
+
no_errors
|
41
|
+
end
|
42
|
+
|
43
|
+
expect(valid_json).to match_json_schema(:inline_schema, validation_options)
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'finds a match' do
|
47
|
+
specify 'when tested against valid json' do
|
48
|
+
expect(valid_json).to match_json_schema(:inline_schema)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'does not find a match' do
|
53
|
+
let(:validator_errors) { [:error1, :error2] }
|
54
|
+
|
55
|
+
specify 'when tested against invalid json' do
|
56
|
+
expect(invalid_json).not_to match_json_schema(:inline_schema)
|
57
|
+
end
|
58
|
+
|
59
|
+
specify 'assigns a failure message' do
|
60
|
+
expect(JSON::Validator).to receive(:fully_validate) { validator_errors.clone }
|
61
|
+
matcher = match_json_schema(:inline_schema)
|
62
|
+
expect(matcher.matches?(invalid_json)).to eq(false)
|
63
|
+
expect(matcher.failure_message).to eq(expected_failure_message)
|
64
|
+
end
|
65
|
+
|
66
|
+
def expected_failure_message
|
67
|
+
first_failure_message = "Expected JSON object to match schema identified by inline_schema, #{validator_errors.count} errors in validating"
|
68
|
+
all_failure_messages = [first_failure_message, *validator_errors]
|
69
|
+
all_failure_messages.join("\n")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'json-schema-rspec'
|
2
|
+
|
3
|
+
Dir[File.expand_path("../../spec/support/**/*.rb",__FILE__)].each { |f| require f }
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
|
7
|
+
config.expect_with :rspec do |c|
|
8
|
+
c.syntax = :expect
|
9
|
+
end
|
10
|
+
|
11
|
+
config.filter_run_excluding wip: true
|
12
|
+
|
13
|
+
config.include JSON::SchemaMatchers
|
14
|
+
end
|
metadata
CHANGED
@@ -1,56 +1,54 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
7
|
+
- Sharethrough Engineering
|
8
8
|
- Michael Jensen
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-05-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
17
|
requirements:
|
19
|
-
- -
|
18
|
+
- - ">="
|
20
19
|
- !ruby/object:Gem::Version
|
21
20
|
version: '0'
|
22
21
|
type: :runtime
|
23
22
|
prerelease: false
|
24
23
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
24
|
requirements:
|
27
|
-
- -
|
25
|
+
- - ">="
|
28
26
|
- !ruby/object:Gem::Version
|
29
27
|
version: '0'
|
30
28
|
- !ruby/object:Gem::Dependency
|
31
29
|
name: json-schema
|
32
30
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
31
|
requirements:
|
35
|
-
- - ~>
|
32
|
+
- - "~>"
|
36
33
|
- !ruby/object:Gem::Version
|
37
|
-
version: 2.
|
34
|
+
version: '2.5'
|
38
35
|
type: :runtime
|
39
36
|
prerelease: false
|
40
37
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
38
|
requirements:
|
43
|
-
- - ~>
|
39
|
+
- - "~>"
|
44
40
|
- !ruby/object:Gem::Version
|
45
|
-
version: 2.
|
41
|
+
version: '2.5'
|
46
42
|
description: Adds RSpec matchers for validating JSON schemas
|
47
43
|
email:
|
44
|
+
- engineers@sharethrough.com
|
48
45
|
- emjay1988@gmail.com
|
49
46
|
executables: []
|
50
47
|
extensions: []
|
51
48
|
extra_rdoc_files: []
|
52
49
|
files:
|
53
|
-
- .gitignore
|
50
|
+
- ".gitignore"
|
51
|
+
- CHANGELOG.md
|
54
52
|
- Gemfile
|
55
53
|
- LICENSE
|
56
54
|
- README.md
|
@@ -59,28 +57,31 @@ files:
|
|
59
57
|
- lib/json-schema-rspec.rb
|
60
58
|
- lib/json-schema-rspec/matchers/json_schema_matcher.rb
|
61
59
|
- lib/json-schema-rspec/version.rb
|
60
|
+
- spec/json-schema-rspec/matchers/json_schema_matcher_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
62
|
homepage: ''
|
63
63
|
licenses: []
|
64
|
+
metadata: {}
|
64
65
|
post_install_message:
|
65
66
|
rdoc_options: []
|
66
67
|
require_paths:
|
67
68
|
- lib
|
68
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
70
|
requirements:
|
71
|
-
- -
|
71
|
+
- - ">="
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
75
|
requirements:
|
77
|
-
- -
|
76
|
+
- - ">="
|
78
77
|
- !ruby/object:Gem::Version
|
79
78
|
version: '0'
|
80
79
|
requirements: []
|
81
80
|
rubyforge_project:
|
82
|
-
rubygems_version:
|
81
|
+
rubygems_version: 2.2.2
|
83
82
|
signing_key:
|
84
|
-
specification_version:
|
83
|
+
specification_version: 4
|
85
84
|
summary: JSON Schema RSpec matchers
|
86
|
-
test_files:
|
85
|
+
test_files:
|
86
|
+
- spec/json-schema-rspec/matchers/json_schema_matcher_spec.rb
|
87
|
+
- spec/spec_helper.rb
|