diff --git a/lib/openapi_first/error_response.rb b/lib/openapi_first/error_response.rb index a986c136..bd573fe4 100644 --- a/lib/openapi_first/error_response.rb +++ b/lib/openapi_first/error_response.rb @@ -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:) diff --git a/lib/openapi_first/request_validation.rb b/lib/openapi_first/request_validation.rb index 692c9409..0eb7be38 100644 --- a/lib/openapi_first/request_validation.rb +++ b/lib/openapi_first/request_validation.rb @@ -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 diff --git a/lib/openapi_first/validation_result.rb b/lib/openapi_first/validation_result.rb index 834cc063..e8332df4 100644 --- a/lib/openapi_first/validation_result.rb +++ b/lib/openapi_first/validation_result.rb @@ -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 diff --git a/spec/schema_validation_spec.rb b/spec/schema_validation_spec.rb index b9aa0192..dad1caab 100644 --- a/spec/schema_validation_spec.rb +++ b/spec/schema_validation_spec.rb @@ -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 @@ -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