Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(link, options = {})
@validators = {}
if link.is_a? Drivers::OpenAPI2::Link
link.target_schemas.each do |status, schema|
@validators[status] = JsonSchema::Validator.new(target_schema(link))
@validators[status] = JsonSchema::Validator.new(schema)
end
else
@validator = JsonSchema::Validator.new(target_schema(link))
Expand Down
16 changes: 16 additions & 0 deletions test/schema_validator/hyper_schema/response_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@
assert_equal message, e.message
end

it "validates OpenAPI 2 responses against the schema for their status" do
link = Committee::Drivers::OpenAPI2::Link.new
link.href = "/apps"
link.media_type = "application/json"
link.status_success = 200
success_schema = JsonSchema.parse!({ "type" => "object", "required" => ["id"], "properties" => { "id" => { "type" => "integer" } } })
error_schema = JsonSchema.parse!({ "type" => "object", "required" => ["error"], "properties" => { "error" => { "type" => "string" } } })
link.target_schemas = { 200 => success_schema, 400 => error_schema }
validator = Committee::SchemaValidator::HyperSchema::ResponseValidator.new(link, validate_success_only: false)

validator.call(200, @headers, { "id" => 1 })
validator.call(400, @headers, { "error" => "invalid request" })
assert_raises(Committee::InvalidResponse) { validator.call(200, @headers, { "error" => "invalid request" }) }
assert_raises(Committee::InvalidResponse) { validator.call(400, @headers, { "id" => 1, "error" => 1 }) }
end

private

def call
Expand Down