diff --git a/lib/shrine/plugins/validation_helpers.rb b/lib/shrine/plugins/validation_helpers.rb index 53ab342b..31da0541 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.nil? || type == "" ? "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 5bd284c7..dc1f9e9a 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