Skip to content

Commit

Permalink
Include detected file mime type in validate_mime_type_inclusion errors
Browse files Browse the repository at this point in the history
  • Loading branch information
janklimo committed Nov 23, 2022
1 parent f7d9971 commit 1c5cff0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 7 additions & 4 deletions lib/shrine/plugins/validation_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ")}" },
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions test/plugin/validation_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -615,23 +615,26 @@
@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
@attacher.class.validate { validate_mime_type_inclusion(["video/mpeg"], message: "must be a video") }
@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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1c5cff0

Please sign in to comment.