module Shrine::Plugins::ValidationHelpers::AttacherMethods

Public Instance Methods

validate_dimensions((width_range, height_range)) click to toggle source

Validates that the dimensions are in the given range.

validate_dimensions [100..5000, 100..5000]
# File lib/shrine/plugins/validation_helpers.rb, line 161
def validate_dimensions((width_range, height_range))
  min_dims = width_range.begin, height_range.begin
  max_dims = width_range.end,   height_range.end

  validate_min_dimensions(min_dims) && validate_max_dimensions(max_dims)
end
validate_extension(extensions, message: nil)
validate_extension_exclusion(extensions, message: nil) click to toggle source

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

validate_extension_exclusion %[php jar]
# File lib/shrine/plugins/validation_helpers.rb, line 207
def validate_extension_exclusion(extensions, message: nil)
  validate_result(
    extensions.none? { |extension| extension.casecmp(file.extension.to_s) == 0 },
    :extension_exclusion, message, extensions
  )
end
validate_extension_inclusion(extensions, message: nil) click to toggle source

Validates that the extension is included in the given list. Comparison is case insensitive.

validate_extension_inclusion %w[jpg jpeg png gif]
# File lib/shrine/plugins/validation_helpers.rb, line 195
def validate_extension_inclusion(extensions, message: nil)
  validate_result(
    extensions.any? { |extension| extension.casecmp(file.extension.to_s) == 0 },
    :extension_inclusion, message, extensions
  )
end
Also aliased as: validate_extension
validate_height(height_range) click to toggle source

Validates that the ‘height` metadata is in the given range.

validate_height 100..5000
# File lib/shrine/plugins/validation_helpers.rb, line 128
def validate_height(height_range)
  min_height, max_height = height_range.begin, height_range.end

  validate_min_height(min_height) && validate_max_height(max_height)
end
validate_max_dimensions((max_width, max_height), message: nil) click to toggle source

Validates that the dimensions are not larger than specified.

validate_max_dimensions [5000, 5000]
# File lib/shrine/plugins/validation_helpers.rb, line 137
def validate_max_dimensions((max_width, max_height), message: nil)
  fail Error, "width and/or height metadata is missing" unless file["width"] && file["height"]

  validate_result(
    file["width"] <= max_width && file["height"] <= max_height,
    :max_dimensions, message, [max_width, max_height]
  )
end
validate_max_height(max, message: nil) click to toggle source

Validates that the ‘height` metadata is not larger than `max`. Requires the `store_dimensions` plugin.

validate_max_height 5000
# File lib/shrine/plugins/validation_helpers.rb, line 109
def validate_max_height(max, message: nil)
  fail Error, "height metadata is missing" unless file["height"]

  validate_result(file["height"] <= max, :max_height, message, max)
end
validate_max_size(max, message: nil) click to toggle source

Validates that the ‘size` metadata is not larger than `max`.

validate_max_size 5*1024*1024
# File lib/shrine/plugins/validation_helpers.rb, line 54
def validate_max_size(max, message: nil)
  validate_result(file.size <= max, :max_size, message, max)
end
validate_max_width(max, message: nil) click to toggle source

Validates that the ‘width` metadata is not larger than `max`. Requires the `store_dimensions` plugin.

validate_max_width 5000
# File lib/shrine/plugins/validation_helpers.rb, line 79
def validate_max_width(max, message: nil)
  fail Error, "width metadata is missing" unless file["width"]

  validate_result(file["width"] <= max, :max_width, message, max)
end
validate_mime_type(types, message: nil)
validate_mime_type_exclusion(types, message: nil) click to toggle source

Validates that the ‘mime_type` metadata is not included in the given list.

validate_mime_type_exclusion %w[text/x-php]
# File lib/shrine/plugins/validation_helpers.rb, line 184
def validate_mime_type_exclusion(types, message: nil)
  validate_result(
    !types.include?(file.mime_type),
    :mime_type_exclusion, message, types
  )
end
validate_mime_type_inclusion(types, message: nil) click to toggle source

Validates that the ‘mime_type` metadata is included in the given list.

validate_mime_type_inclusion %w[audio/mp3 audio/flac]
# File lib/shrine/plugins/validation_helpers.rb, line 172
def validate_mime_type_inclusion(types, message: nil)
  validate_result(
    types.include?(file.mime_type),
    :mime_type_inclusion, message, types
  )
end
Also aliased as: validate_mime_type
validate_min_dimensions((min_width, min_height), message: nil) click to toggle source

Validates that the dimensions are not smaller than specified.

validate_min_dimensions [100, 100]
# File lib/shrine/plugins/validation_helpers.rb, line 149
def validate_min_dimensions((min_width, min_height), message: nil)
  fail Error, "width and/or height metadata is missing" unless file["width"] && file["height"]

  validate_result(
    file["width"] >= min_width && file["height"] >= min_height,
    :min_dimensions, message, [min_width, min_height]
  )
end
validate_min_height(min, message: nil) click to toggle source

Validates that the ‘height` metadata is not smaller than `min`. Requires the `store_dimensions` plugin.

validate_min_height 100
# File lib/shrine/plugins/validation_helpers.rb, line 119
def validate_min_height(min, message: nil)
  fail Error, "height metadata is missing" unless file["height"]

  validate_result(file["height"] >= min, :min_height, message, min)
end
validate_min_size(min, message: nil) click to toggle source

Validates that the ‘size` metadata is not smaller than `min`.

validate_min_size 1024
# File lib/shrine/plugins/validation_helpers.rb, line 61
def validate_min_size(min, message: nil)
  validate_result(file.size >= min, :min_size, message, min)
end
validate_min_width(min, message: nil) click to toggle source

Validates that the ‘width` metadata is not smaller than `min`. Requires the `store_dimensions` plugin.

validate_min_width 100
# File lib/shrine/plugins/validation_helpers.rb, line 89
def validate_min_width(min, message: nil)
  fail Error, "width metadata is missing" unless file["width"]

  validate_result(file["width"] >= min, :min_width, message, min)
end
validate_size(size_range) click to toggle source

Validates that the ‘size` metadata is in the given range.

validate_size 1024..5*1024*1024
# File lib/shrine/plugins/validation_helpers.rb, line 68
def validate_size(size_range)
  min_size, max_size = size_range.begin, size_range.end

  validate_min_size(min_size) && validate_max_size(max_size)
end
validate_width(width_range) click to toggle source

Validates that the ‘width` metadata is in the given range.

validate_width 100..5000
# File lib/shrine/plugins/validation_helpers.rb, line 98
def validate_width(width_range)
  min_width, max_width = width_range.begin, width_range.end

  validate_min_width(min_width) && validate_max_width(max_width)
end

Private Instance Methods

add_error(*args) click to toggle source

Generates an error message and appends it to errors array.

# File lib/shrine/plugins/validation_helpers.rb, line 227
def add_error(*args)
  errors << error_message(*args)
end
error_message(type, message, object) click to toggle source

Returns the direct message if given, otherwise uses the default error message.

# File lib/shrine/plugins/validation_helpers.rb, line 233
def error_message(type, message, object)
  message ||= self.class.default_validation_messages.fetch(type)
  message.is_a?(String) ? message : message.call(object)
end
validate_result(result, type, message, *args) click to toggle source

Adds an error if result is false and returns the result.

# File lib/shrine/plugins/validation_helpers.rb, line 217
def validate_result(result, type, message, *args)
  if result
    true
  else
    add_error(type, message, *args)
    false
  end
end