module Shrine::Plugins::ValidationHelpers::AttacherMethods

  1. lib/shrine/plugins/validation_helpers.rb

Public Instance methods

validate_extension_exclusion (blacklist, message: nil)

Validates that the extension is not in the given collection. Comparison is case insensitive.

validate_extension_exclusion %Q[php jar]
[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 148
def validate_extension_exclusion(blacklist, message: nil)
  blacklist.none? { |extension| regex(extension) =~ get.extension.to_s }              or add_error(:extension_exclusion, message, blacklist) && false
end
validate_extension_inclusion (whitelist, message: nil)

Validates that the extension is in the given collection. Comparison is case insensitive.

validate_extension_inclusion %w[jpg jpeg png gif]
[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 139
def validate_extension_inclusion(whitelist, message: nil)
  whitelist.any? { |extension| regex(extension) =~ get.extension.to_s }              or add_error(:extension_inclusion, message, whitelist) && false
end
validate_max_height (max, message: nil)

Validates that the file is not taller than max. Requires the store_dimensions plugin.

[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 99
def validate_max_height(max, message: nil)
  raise Error, ":store_dimensions plugin is required" if !get.respond_to?(:height)
  if get.height
    get.height <= max or add_error(:max_height, message, max) && false
  else
    Shrine.deprecation("Height of the uploaded file is nil, and Shrine skipped the validation. In Shrine 3 the validation will fail if height is nil.")
  end
end
validate_max_size (max, message: nil)

Validates that the file is not larger than max.

[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 66
def validate_max_size(max, message: nil)
  get.size <= max or add_error(:max_size, message, max) && false
end
validate_max_width (max, message: nil)

Validates that the file is not wider than max. Requires the store_dimensions plugin.

[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 77
def validate_max_width(max, message: nil)
  raise Error, ":store_dimensions plugin is required" if !get.respond_to?(:width)
  if get.width
    get.width <= max or add_error(:max_width, message, max) && false
  else
    Shrine.deprecation("Width of the uploaded file is nil, and Shrine skipped the validation. In Shrine 3 the validation will fail if width is nil.")
  end
end
validate_mime_type_exclusion (blacklist, message: nil)

Validates that the MIME type is not in the given collection.

validate_mime_type_exclusion %w[text/x-php]
[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 130
def validate_mime_type_exclusion(blacklist, message: nil)
  blacklist.none? { |mime_type| regex(mime_type) =~ get.mime_type.to_s }              or add_error(:mime_type_exclusion, message, blacklist) && false
end
validate_mime_type_inclusion (whitelist, message: nil)

Validates that the MIME type is in the given collection.

validate_mime_type_inclusion %w[audio/mp3 audio/flac]
[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 122
def validate_mime_type_inclusion(whitelist, message: nil)
  whitelist.any? { |mime_type| regex(mime_type) =~ get.mime_type.to_s }              or add_error(:mime_type_inclusion, message, whitelist) && false
end
validate_min_height (min, message: nil)

Validates that the file is not shorter than min. Requires the store_dimensions plugin.

[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 110
def validate_min_height(min, message: nil)
  raise Error, ":store_dimensions plugin is required" if !get.respond_to?(:height)
  if get.height
    get.height >= min or add_error(:min_height, message, min) && false
  else
    Shrine.deprecation("Height of the uploaded file is nil, and Shrine skipped the validation. In Shrine 3 the validation will fail if height is nil.")
  end
end
validate_min_size (min, message: nil)

Validates that the file is not smaller than min.

[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 71
def validate_min_size(min, message: nil)
  get.size >= min or add_error(:min_size, message, min) && false
end
validate_min_width (min, message: nil)

Validates that the file is not narrower than min. Requires the store_dimensions plugin.

[show source]
# File lib/shrine/plugins/validation_helpers.rb, line 88
def validate_min_width(min, message: nil)
  raise Error, ":store_dimensions plugin is required" if !get.respond_to?(:width)
  if get.width
    get.width >= min or add_error(:min_width, message, min) && false
  else
    Shrine.deprecation("Width of the uploaded file is nil, and Shrine skipped the validation. In Shrine 3 the validation will fail if width is nil.")
  end
end