Skip to content

Commit

Permalink
DRY Generating the response error title
Browse files Browse the repository at this point in the history
  • Loading branch information
ahx committed Oct 27, 2023
1 parent 4a5dffd commit 26e0baa
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/openapi_first/error_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module OpenapiFirst
# This is the base class for error responses
class ErrorResponse
## @param status [Integer] The HTTP status code.
## @param title [String] The title of the error. Usually the name of the HTTP status code.
## @param status [Integer] The intended HTTP status code.
## @param title [String] A descriptive title for the error.
## @param location [Symbol] The location of the error (:body, :query, :header, :cookie, :path).
## @param validation_result [ValidationResult]
def initialize(status:, location:, title:, validation_result:)
Expand Down
2 changes: 1 addition & 1 deletion lib/openapi_first/request_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.fail!(status = 400, location = nil, title: nil, validation_result: nil)
throw FAIL, {
status:,
location:,
title: title || validation_result&.output&.fetch('error') || Rack::Utils::HTTP_STATUS_CODES[status],
title: title || validation_result&.message || Rack::Utils::HTTP_STATUS_CODES[status],
validation_result:
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openapi_first/validation_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def error? = !output['valid']
def message
return if valid?

(output['errors']&.map { |e| e['error'] }&.join('. ') || output['error'])&.concat('.')
(output['errors']&.map { |e| e['error'] }&.join('. ') || output['error'])
end
end
end
6 changes: 3 additions & 3 deletions spec/schema_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def validate(data)
end

it 'returns a message if data is invalid' do
expect(validate({}).message).to eq 'object at root is missing required properties: count.'
expect(validate({}).message).to eq 'object at root is missing required properties: count'
end

it 'returns a longer message with nested errors' do
Expand All @@ -82,10 +82,10 @@ def validate(data)
}
validator = described_class.new(schema, openapi_version: '3.1')
validation = validator.validate({ 'count' => 'a', 'pets' => [{ name: 12 }] })
expect(validation.message).to eq 'value at `/count` is not an integer. value at `/pets/0/name` is not a string.'
expect(validation.message).to eq 'value at `/count` is not an integer. value at `/pets/0/name` is not a string'

validation = validator.validate({})
expect(validation.message).to eq 'object at root is missing required properties: pets.'
expect(validation.message).to eq 'object at root is missing required properties: pets'
end
end

Expand Down

0 comments on commit 26e0baa

Please sign in to comment.