From 1c5cff093e1d4139c5c5104b3b73467178eb8f3e Mon Sep 17 00:00:00 2001 From: Jan Klimo Date: Wed, 23 Nov 2022 16:07:26 +0700 Subject: [PATCH] Include detected file mime type in validate_mime_type_inclusion errors --- lib/shrine/plugins/validation_helpers.rb | 11 +++++++---- test/plugin/validation_helpers_test.rb | 15 +++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/shrine/plugins/validation_helpers.rb b/lib/shrine/plugins/validation_helpers.rb index 53ab342b5..6f9d1cb46 100644 --- a/lib/shrine/plugins/validation_helpers.rb +++ b/lib/shrine/plugins/validation_helpers.rb @@ -13,7 +13,10 @@ module ValidationHelpers min_height: -> (min) { "height must not be less than #{min}px" }, max_dimensions: -> (dims) { "dimensions must not be greater than #{dims.join("x")}" }, min_dimensions: -> (dims) { "dimensions must not be less than #{dims.join("x")}" }, - mime_type_inclusion: -> (list) { "type must be one of: #{list.join(", ")}" }, + mime_type_inclusion: lambda do |list, type| + mime_type = type == '' || type.nil? ? "no" : type + "type must be one of: #{list.join(", ")}, (#{mime_type} content type detected)" + end, mime_type_exclusion: -> (list) { "type must not be one of: #{list.join(", ")}" }, extension_inclusion: -> (list) { "extension must be one of: #{list.join(", ")}" }, extension_exclusion: -> (list) { "extension must not be one of: #{list.join(", ")}" }, @@ -172,7 +175,7 @@ def validate_dimensions((width_range, height_range)) def validate_mime_type_inclusion(types, message: nil) validate_result( types.include?(file.mime_type), - :mime_type_inclusion, message, types + :mime_type_inclusion, message, types, file.mime_type ) end alias validate_mime_type validate_mime_type_inclusion @@ -230,9 +233,9 @@ def add_error(*args) # Returns the direct message if given, otherwise uses the default error # message. - def error_message(type, message, object) + def error_message(type, message, *args) message ||= self.class.default_validation_messages.fetch(type) - message.is_a?(String) ? message : message.call(object) + message.is_a?(String) ? message : message.call(*args) end end end diff --git a/test/plugin/validation_helpers_test.rb b/test/plugin/validation_helpers_test.rb index 5bd284c77..dc1f9e9ac 100644 --- a/test/plugin/validation_helpers_test.rb +++ b/test/plugin/validation_helpers_test.rb @@ -615,13 +615,13 @@ @attacher.attach(fakeio(content_type: nil)) @attacher.class.validate { validate_mime_type_inclusion(["image/jpeg"]) } @attacher.validate - assert_equal 1, @attacher.errors.size + assert_equal ["type must be one of: image/jpeg, (no content type detected)"], @attacher.errors end it "uses the default error message" do @attacher.class.validate { validate_mime_type_inclusion(["video/mpeg"]) } @attacher.validate - assert_equal ["type must be one of: video/mpeg"], @attacher.errors + assert_equal ["type must be one of: video/mpeg, (image/jpeg content type detected)"], @attacher.errors end it "accepts a custom error message" do @@ -629,9 +629,12 @@ @attacher.validate assert_equal ["must be a video"], @attacher.errors - @attacher.class.validate { validate_mime_type_inclusion(["video/mpeg"], message: ->(whitelist){"must be #{whitelist.join(", ")}"}) } + @attacher.class.validate do + validate_mime_type_inclusion(["video/mpeg"], + message: ->(whitelist, type) {"must be #{whitelist.join(", ")}, #{type} detected"}) + end @attacher.validate - assert_equal ["must be video/mpeg"], @attacher.errors + assert_equal ["must be video/mpeg, image/jpeg detected"], @attacher.errors end it "handles multiline mime types" do @@ -656,7 +659,7 @@ it "is aliased to #validate_mime_type" do @attacher.class.validate { validate_mime_type(["video/mpeg"]) } @attacher.validate - assert_equal ["type must be one of: video/mpeg"], @attacher.errors + assert_equal ["type must be one of: video/mpeg, (image/jpeg content type detected)"], @attacher.errors end end @@ -890,7 +893,7 @@ max_size: -> (max) { "is too big" } } @attacher.shrine_class.plugin :validation_helpers, default_messages: { - mime_type_inclusion: -> (list) { "is forbidden" } + mime_type_inclusion: -> (list, _type) { "is forbidden" } } @attacher.class.validate do validate_max_size 1