Module: Shrine::Plugins::ValidationHelpers::AttacherMethods

Defined in:
lib/shrine/plugins/validation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#validate_dimensions(width_range, height_range) ⇒ Object

Validates that the dimensions are in the given range.

validate_dimensions [100..5000, 100..5000]


174
175
176
177
178
179
# File 'lib/shrine/plugins/validation_helpers.rb', line 174

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_exclusion(extensions, message: nil) ⇒ Object

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

validate_extension_exclusion %[php jar]


220
221
222
223
224
225
# File 'lib/shrine/plugins/validation_helpers.rb', line 220

def validate_extension_exclusion(extensions, message: nil)
  validate_result(
    extensions.none? { |extension| regex(extension) =~ get.extension.to_s },
    :extension_exclusion, message, extensions
  )
end

#validate_extension_inclusion(extensions, message: nil) ⇒ Object Also known as: validate_extension

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

validate_extension_inclusion %w[jpg jpeg png gif]


208
209
210
211
212
213
# File 'lib/shrine/plugins/validation_helpers.rb', line 208

def validate_extension_inclusion(extensions, message: nil)
  validate_result(
    extensions.any? { |extension| regex(extension) =~ get.extension.to_s },
    :extension_inclusion, message, extensions
  )
end

#validate_height(height_range) ⇒ Object

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

validate_height 100..5000


139
140
141
142
143
# File 'lib/shrine/plugins/validation_helpers.rb', line 139

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) ⇒ Object

Validates that the dimensions are not larger than specified.

validate_max_dimensions [5000, 5000]


148
149
150
151
152
153
154
155
156
# File 'lib/shrine/plugins/validation_helpers.rb', line 148

def validate_max_dimensions((max_width, max_height), message: nil)
  fail Error, ":store_dimensions plugin is required" unless get.respond_to?(:width) && get.respond_to?(:height)
  fail Error, "width or height metadata is nil" unless get.width && get.height

  validate_result(
    get.width <= max_width && get.height <= max_height,
    :max_dimensions, message, [max_width, max_height]
  )
end

#validate_max_height(max, message: nil) ⇒ Object

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

validate_max_height 5000


114
115
116
117
118
119
120
121
# File 'lib/shrine/plugins/validation_helpers.rb', line 114

def validate_max_height(max, message: nil)
  fail Error, ":store_dimensions plugin is required" unless get.respond_to?(:height)
  if get.height
    validate_result(get.height <= max, :max_height, message, max)
  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) ⇒ Object

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

validate_max_size 5*1024*1024


53
54
55
# File 'lib/shrine/plugins/validation_helpers.rb', line 53

def validate_max_size(max, message: nil)
  validate_result(get.size <= max, :max_size, message, max)
end

#validate_max_width(max, message: nil) ⇒ Object

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

validate_max_width 5000


78
79
80
81
82
83
84
85
# File 'lib/shrine/plugins/validation_helpers.rb', line 78

def validate_max_width(max, message: nil)
  fail Error, ":store_dimensions plugin is required" unless get.respond_to?(:width)
  if get.width
    validate_result(get.width <= max, :max_width, message, max)
  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(types, message: nil) ⇒ Object

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

validate_mime_type_exclusion %w[text/x-php]


197
198
199
200
201
202
# File 'lib/shrine/plugins/validation_helpers.rb', line 197

def validate_mime_type_exclusion(types, message: nil)
  validate_result(
    types.none? { |type| regex(type) =~ get.mime_type.to_s },
    :mime_type_exclusion, message, types
  )
end

#validate_mime_type_inclusion(types, message: nil) ⇒ Object Also known as: validate_mime_type

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

validate_mime_type_inclusion %w[audio/mp3 audio/flac]


185
186
187
188
189
190
# File 'lib/shrine/plugins/validation_helpers.rb', line 185

def validate_mime_type_inclusion(types, message: nil)
  validate_result(
    types.any? { |type| regex(type) =~ get.mime_type.to_s },
    :mime_type_inclusion, message, types
  )
end

#validate_min_dimensions(min_width, min_height, message: nil) ⇒ Object

Validates that the dimensions are not smaller than specified.

validate_max_dimensions [100, 100]


161
162
163
164
165
166
167
168
169
# File 'lib/shrine/plugins/validation_helpers.rb', line 161

def validate_min_dimensions((min_width, min_height), message: nil)
  fail Error, ":store_dimensions plugin is required" unless get.respond_to?(:width) && get.respond_to?(:height)
  fail Error, "width or height metadata is nil" unless get.width && get.height

  validate_result(
    get.width >= min_width && get.height >= min_height,
    :min_dimensions, message, [min_width, min_height]
  )
end

#validate_min_height(min, message: nil) ⇒ Object

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

validate_min_height 100


127
128
129
130
131
132
133
134
# File 'lib/shrine/plugins/validation_helpers.rb', line 127

def validate_min_height(min, message: nil)
  fail Error, ":store_dimensions plugin is required" unless get.respond_to?(:height)
  if get.height
    validate_result(get.height >= min, :min_height, message, min)
  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) ⇒ Object

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

validate_min_size 1024


60
61
62
# File 'lib/shrine/plugins/validation_helpers.rb', line 60

def validate_min_size(min, message: nil)
  validate_result(get.size >= min, :min_size, message, min)
end

#validate_min_width(min, message: nil) ⇒ Object

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

validate_min_width 100


91
92
93
94
95
96
97
98
# File 'lib/shrine/plugins/validation_helpers.rb', line 91

def validate_min_width(min, message: nil)
  fail Error, ":store_dimensions plugin is required" unless get.respond_to?(:width)
  if get.width
    validate_result(get.width >= min, :min_width, message, min)
  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_size(size_range) ⇒ Object

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

validate_size 1024..5*1024*1024


67
68
69
70
71
# File 'lib/shrine/plugins/validation_helpers.rb', line 67

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) ⇒ Object

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

validate_width 100..5000


103
104
105
106
107
# File 'lib/shrine/plugins/validation_helpers.rb', line 103

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