Skip to content

Commit

Permalink
[Validator] Extend allowed content_types using Marcel (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Jan 27, 2025
1 parent d9c0d75 commit 227d3df
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
9 changes: 7 additions & 2 deletions lib/active_storage_validations/content_type_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def invalid_content_type_option_message(content_type)
if content_type.to_s.match?(/\//)
<<~ERROR_MESSAGE
You must pass valid content types to the validator
'#{content_type}' is not found in Marcel::TYPE_EXTS
'#{content_type}' is not found in Marcel content types (Marcel::TYPE_EXTS + Marcel::MAGIC)
ERROR_MESSAGE
else
<<~ERROR_MESSAGE
Expand All @@ -215,7 +215,12 @@ def invalid_content_type?(content_type)
raise ArgumentError, "'image/jpg' is not a valid content type, you should use 'image/jpeg' instead"
end

Marcel::TYPE_EXTS[content_type.to_s] == nil
all_available_marcel_content_types.keys.exclude?(content_type.to_s)
end

def all_available_marcel_content_types
Marcel::MAGIC.map {|dd| dd.first }
.each_with_object(Marcel::TYPE_EXTS) { |(k,v), h| h[k] = v unless h.key?(k) }
end

def invalid_extension?(content_type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: content_type_validator_check_validity_valid_ct_ole_storages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class ContentType::Validator::CheckValidityValidContentTypeOleStorage < ApplicationRecord
self.table_name = "content_type_validator_check_validity_valid_ct_ole_storages"

has_one_attached :valid
validates :valid, content_type: "application/x-ole-storage"
end

2 changes: 1 addition & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
end
end

%w(proc_option invalid_content_type_with invalid_content_type_in invalid_content_type_jpg invalid_extension regex_option).each do |invalid_case|
%w(proc_option invalid_content_type_with invalid_content_type_in invalid_content_type_jpg valid_ct_ole_storage invalid_extension regex_option).each do |invalid_case|
create_table :"content_type_validator_check_validity_#{invalid_case.pluralize}", force: :cascade do |t|
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
Expand Down
23 changes: 17 additions & 6 deletions test/validators/content_type_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,26 @@
end
end

describe "when the passed option is 'image/jpg'" do
subject { validator_test_class::CheckValidityInvalidContentTypeJpg.new(params) }
describe "Edge cases" do
describe "when the passed option is 'image/jpg'" do
subject { validator_test_class::CheckValidityInvalidContentTypeJpg.new(params) }

let(:error_message) do
"'image/jpg' is not a valid content type, you should use 'image/jpeg' instead"
let(:error_message) do
"'image/jpg' is not a valid content type, you should use 'image/jpeg' instead"
end

it 'raises an error at model initialization' do
error = assert_raises(ArgumentError) { subject }
assert_equal(error_message, error.message)
end
end

it 'raises an error at model initialization' do
assert_raises(ArgumentError, error_message) { subject }
describe "when the passed option is in Marcel::MAGIC (e.g. 'application/x-ole-storage')" do
subject { validator_test_class::CheckValidityValidContentTypeOleStorage.new(params) }

it 'does not raise an error at model initialization' do
assert_nothing_raised { subject }
end
end
end
end
Expand Down

0 comments on commit 227d3df

Please sign in to comment.