Class: JSON::SchemaMatchers::MatchJsonSchemaMatcher
- Inherits:
-
Object
- Object
- JSON::SchemaMatchers::MatchJsonSchemaMatcher
- Defined in:
- lib/json-schema-rspec/matchers/json_schema_matcher.rb
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(schema_name, validation_opts = {}) ⇒ MatchJsonSchemaMatcher
constructor
A new instance of MatchJsonSchemaMatcher.
- #matches?(actual) ⇒ Boolean
- #schema_for_name(schema) ⇒ Object
Constructor Details
#initialize(schema_name, validation_opts = {}) ⇒ MatchJsonSchemaMatcher
11 12 13 14 |
# File 'lib/json-schema-rspec/matchers/json_schema_matcher.rb', line 11 def initialize(schema_name, validation_opts = {}) @schema_name = schema_name @validation_opts = validation_opts end |
Instance Method Details
#description ⇒ Object
47 48 49 |
# File 'lib/json-schema-rspec/matchers/json_schema_matcher.rb', line 47 def description "match JSON schema identified by #{@schema_name}" end |
#failure_message ⇒ Object
39 40 41 |
# File 'lib/json-schema-rspec/matchers/json_schema_matcher.rb', line 39 def @errors.join("\n") end |
#failure_message_when_negated ⇒ Object
43 44 45 |
# File 'lib/json-schema-rspec/matchers/json_schema_matcher.rb', line 43 def "Expected JSON object not to match schema identified by #{@schema_name}" end |
#matches?(actual) ⇒ Boolean
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/json-schema-rspec/matchers/json_schema_matcher.rb', line 16 def matches?(actual) @actual = actual schema = schema_for_name(@schema_name) if schema.nil? @errors = [ "No schema defined for #{@schema_name}", "Add a line to your RSpec.configure block to define the schema:", " RSpec.configure do |config|", " config.json_schemas[:my_remote_schema] = 'path/to/schema'", " config.json_schemas[:my_inline_schema] = '{\"json\": \"schema\"}'", " end"] return false end @errors = JSON::Validator.fully_validate(schema_for_name(@schema_name), @actual, @validation_opts) if @errors.any? @errors.unshift("Expected JSON object to match schema identified by #{@schema_name}, #{@errors.count} errors in validating") return false else return true end end |
#schema_for_name(schema) ⇒ Object
51 52 53 |
# File 'lib/json-schema-rspec/matchers/json_schema_matcher.rb', line 51 def schema_for_name(schema) RSpec.configuration.json_schemas[schema] end |