module Shrine::Plugins::ValidationHelpers::AttacherMethods

  1. lib/shrine/plugins/validation_helpers.rb

Public Instance Aliases

validate_extension -> validate_extension_inclusion
validate_mime_type -> validate_mime_type_inclusion

Public Instance methods

validate_dimensions((width_range, height_range))

Validates that the dimensions are in the given range.

validate_dimensions [100..5000, 100..5000]
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
161 def validate_dimensions((width_range, height_range))
162   min_dims = width_range.begin, height_range.begin
163   max_dims = width_range.end,   height_range.end
164 
165   validate_min_dimensions(min_dims) && validate_max_dimensions(max_dims)
166 end
validate_extension_exclusion(extensions, message: nil)

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

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

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

validate_extension_inclusion %w[jpg jpeg png gif]
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
195 def validate_extension_inclusion(extensions, message: nil)
196   validate_result(
197     extensions.any? { |extension| extension.casecmp(file.extension.to_s) == 0 },
198     :extension_inclusion, message, extensions
199   )
200 end
validate_height(height_range)

Validates that the height metadata is in the given range.

validate_height 100..5000
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
128 def validate_height(height_range)
129   min_height, max_height = height_range.begin, height_range.end
130 
131   validate_min_height(min_height) && validate_max_height(max_height)
132 end
validate_max_dimensions((max_width, max_height), message: nil)

Validates that the dimensions are not larger than specified.

validate_max_dimensions [5000, 5000]
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
137 def validate_max_dimensions((max_width, max_height), message: nil)
138   fail Error, "width and/or height metadata is missing" unless file["width"] && file["height"]
139 
140   validate_result(
141     file["width"] <= max_width && file["height"] <= max_height,
142     :max_dimensions, message, [max_width, max_height]
143   )
144 end
validate_max_height(max, message: nil)

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

validate_max_height 5000
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
109 def validate_max_height(max, message: nil)
110   fail Error, "height metadata is missing" unless file["height"]
111 
112   validate_result(file["height"] <= max, :max_height, message, max)
113 end
validate_max_size(max, message: nil)

Validates that the size metadata is not larger than max.

validate_max_size 5*1024*1024
[show source]
   # File lib/shrine/plugins/validation_helpers.rb
54 def validate_max_size(max, message: nil)
55   validate_result(file.size <= max, :max_size, message, max)
56 end
validate_max_width(max, message: nil)

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

validate_max_width 5000
[show source]
   # File lib/shrine/plugins/validation_helpers.rb
79 def validate_max_width(max, message: nil)
80   fail Error, "width metadata is missing" unless file["width"]
81 
82   validate_result(file["width"] <= max, :max_width, message, max)
83 end
validate_mime_type_exclusion(types, message: nil)

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

validate_mime_type_exclusion %w[text/x-php]
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
184 def validate_mime_type_exclusion(types, message: nil)
185   validate_result(
186     !types.include?(file.mime_type),
187     :mime_type_exclusion, message, types
188   )
189 end
validate_mime_type_inclusion(types, message: nil)

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

validate_mime_type_inclusion %w[audio/mp3 audio/flac]
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
172 def validate_mime_type_inclusion(types, message: nil)
173   validate_result(
174     types.include?(file.mime_type),
175     :mime_type_inclusion, message, types
176   )
177 end
validate_min_dimensions((min_width, min_height), message: nil)

Validates that the dimensions are not smaller than specified.

validate_min_dimensions [100, 100]
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
149 def validate_min_dimensions((min_width, min_height), message: nil)
150   fail Error, "width and/or height metadata is missing" unless file["width"] && file["height"]
151 
152   validate_result(
153     file["width"] >= min_width && file["height"] >= min_height,
154     :min_dimensions, message, [min_width, min_height]
155   )
156 end
validate_min_height(min, message: nil)

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

validate_min_height 100
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
119 def validate_min_height(min, message: nil)
120   fail Error, "height metadata is missing" unless file["height"]
121 
122   validate_result(file["height"] >= min, :min_height, message, min)
123 end
validate_min_size(min, message: nil)

Validates that the size metadata is not smaller than min.

validate_min_size 1024
[show source]
   # File lib/shrine/plugins/validation_helpers.rb
61 def validate_min_size(min, message: nil)
62   validate_result(file.size >= min, :min_size, message, min)
63 end
validate_min_width(min, message: nil)

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

validate_min_width 100
[show source]
   # File lib/shrine/plugins/validation_helpers.rb
89 def validate_min_width(min, message: nil)
90   fail Error, "width metadata is missing" unless file["width"]
91 
92   validate_result(file["width"] >= min, :min_width, message, min)
93 end
validate_size(size_range)

Validates that the size metadata is in the given range.

validate_size 1024..5*1024*1024
[show source]
   # File lib/shrine/plugins/validation_helpers.rb
68 def validate_size(size_range)
69   min_size, max_size = size_range.begin, size_range.end
70 
71   validate_min_size(min_size) && validate_max_size(max_size)
72 end
validate_width(width_range)

Validates that the width metadata is in the given range.

validate_width 100..5000
[show source]
    # File lib/shrine/plugins/validation_helpers.rb
 98 def validate_width(width_range)
 99   min_width, max_width = width_range.begin, width_range.end
100 
101   validate_min_width(min_width) && validate_max_width(max_width)
102 end