active_storage_validations 1.4.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +619 -213
  3. data/config/locales/da.yml +50 -30
  4. data/config/locales/de.yml +50 -30
  5. data/config/locales/en.yml +50 -30
  6. data/config/locales/es.yml +50 -30
  7. data/config/locales/fr.yml +50 -30
  8. data/config/locales/it.yml +50 -30
  9. data/config/locales/ja.yml +50 -30
  10. data/config/locales/nl.yml +50 -30
  11. data/config/locales/pl.yml +50 -30
  12. data/config/locales/pt-BR.yml +50 -30
  13. data/config/locales/ru.yml +50 -30
  14. data/config/locales/sv.yml +50 -30
  15. data/config/locales/tr.yml +50 -30
  16. data/config/locales/uk.yml +50 -30
  17. data/config/locales/vi.yml +50 -30
  18. data/config/locales/zh-CN.yml +50 -30
  19. data/lib/active_storage_validations/analyzer/audio_analyzer.rb +58 -0
  20. data/lib/active_storage_validations/analyzer/content_type_analyzer.rb +60 -0
  21. data/lib/active_storage_validations/analyzer/image_analyzer/image_magick.rb +4 -4
  22. data/lib/active_storage_validations/analyzer/image_analyzer/vips.rb +11 -12
  23. data/lib/active_storage_validations/analyzer/image_analyzer.rb +9 -53
  24. data/lib/active_storage_validations/analyzer/null_analyzer.rb +2 -2
  25. data/lib/active_storage_validations/analyzer/shared/asv_ff_probable.rb +61 -0
  26. data/lib/active_storage_validations/analyzer/video_analyzer.rb +130 -0
  27. data/lib/active_storage_validations/analyzer.rb +54 -1
  28. data/lib/active_storage_validations/aspect_ratio_validator.rb +15 -11
  29. data/lib/active_storage_validations/{base_size_validator.rb → base_comparison_validator.rb} +18 -16
  30. data/lib/active_storage_validations/content_type_validator.rb +56 -23
  31. data/lib/active_storage_validations/dimension_validator.rb +20 -19
  32. data/lib/active_storage_validations/duration_validator.rb +55 -0
  33. data/lib/active_storage_validations/extensors/asv_blob_metadatable.rb +49 -0
  34. data/lib/active_storage_validations/{marcel_extensor.rb → extensors/asv_marcelable.rb} +3 -0
  35. data/lib/active_storage_validations/limit_validator.rb +14 -2
  36. data/lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb +1 -1
  37. data/lib/active_storage_validations/matchers/{base_size_validator_matcher.rb → base_comparison_validator_matcher.rb} +31 -25
  38. data/lib/active_storage_validations/matchers/content_type_validator_matcher.rb +7 -3
  39. data/lib/active_storage_validations/matchers/dimension_validator_matcher.rb +1 -1
  40. data/lib/active_storage_validations/matchers/duration_validator_matcher.rb +39 -0
  41. data/lib/active_storage_validations/matchers/{processable_image_validator_matcher.rb → processable_file_validator_matcher.rb} +5 -5
  42. data/lib/active_storage_validations/matchers/size_validator_matcher.rb +18 -2
  43. data/lib/active_storage_validations/matchers/total_size_validator_matcher.rb +18 -2
  44. data/lib/active_storage_validations/matchers.rb +5 -3
  45. data/lib/active_storage_validations/{processable_image_validator.rb → processable_file_validator.rb} +4 -3
  46. data/lib/active_storage_validations/railtie.rb +5 -0
  47. data/lib/active_storage_validations/shared/asv_analyzable.rb +38 -3
  48. data/lib/active_storage_validations/shared/asv_attachable.rb +36 -15
  49. data/lib/active_storage_validations/size_validator.rb +11 -3
  50. data/lib/active_storage_validations/total_size_validator.rb +9 -3
  51. data/lib/active_storage_validations/version.rb +1 -1
  52. data/lib/active_storage_validations.rb +7 -3
  53. metadata +14 -8
  54. data/lib/active_storage_validations/content_type_spoof_detector.rb +0 -96
data/README.md CHANGED
@@ -6,331 +6,727 @@
6
6
  [![MiniTest](https://github.com/igorkasyanchuk/active_storage_validations/workflows/MiniTest/badge.svg)](https://github.com/igorkasyanchuk/active_storage_validations/actions)
7
7
  [![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)
8
8
  [![https://www.patreon.com/igorkasyanchuk](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/patron.svg?raw=true)](https://www.patreon.com/igorkasyanchuk)
9
- [![Listed on OpenSource-Heroes.com](https://opensource-heroes.com/badge-v1.svg)](https://opensource-heroes.com/r/igorkasyanchuk/active_storage_validations)
10
9
 
11
- If you are using `active_storage` gem and you want to add simple validations for it, like presence or content_type you need to write a custom validation method.
12
10
 
13
- This gems doing it for you. Just use `attached: true` or `content_type: 'image/png'` validation.
11
+ Active Storage Validations is a gem that allows you to add validations for Active Storage attributes.
14
12
 
15
- ## What it can do
13
+ This gems is doing it right for you! Just use `validates :avatar, attached: true, content_type: 'image/png'` and that's it!
16
14
 
17
- * validates if file(s) attached
18
- * validates content type
19
- * validates size of files
20
- * validates total size of files
21
- * validates dimension of images/videos
22
- * validates number of uploaded files (min/max required)
23
- * validates aspect ratio (if square, portrait, landscape, is_16_9, ...)
24
- * validates if file can be processed by MiniMagick or Vips
25
- * custom error messages
26
- * allow procs for dynamic determination of values
15
+ ## Table of Contents
27
16
 
28
- ## Usage
17
+ - [Getting started](#getting-started)
18
+ - [Installation](#installation)
19
+ - [Error messages (I18n)](#error-messages-i18n)
20
+ - [Using image metadata validators](#using-image-metadata-validators)
21
+ - [Using video and audio metadata validators](#using-video-and-audio-metadata-validators)
22
+ - [Validators](#validators)
23
+ - [Attached](#attached)
24
+ - [Limit](#limit)
25
+ - [Content type](#content-type)
26
+ - [Size](#size)
27
+ - [Total size](#total-size)
28
+ - [Dimension](#dimension)
29
+ - [Duration](#duration)
30
+ - [Aspect ratio](#aspect-ratio)
31
+ - [Processable file](#processable-file)
32
+ - [Upgrading from 1.x to 2.x](#upgrading-from-1x-to-2x)
33
+ - [Internationalization (I18n)](#internationalization-i18n)
34
+ - [Test matchers](#test-matchers)
35
+ - [Contributing](#contributing)
36
+ - [Additional information](#additional-information)
29
37
 
30
- For example you have a model like this and you want to add validation.
38
+ ## Getting started
39
+
40
+ ### Installation
41
+
42
+ Active Storage Validations work with Rails 6.1.4 onwards. Add this line to your application's Gemfile:
31
43
 
32
44
  ```ruby
33
- class User < ApplicationRecord
34
- has_one_attached :avatar
35
- has_many_attached :photos
36
- has_one_attached :image
37
-
38
- validates :name, presence: true
39
-
40
- validates :avatar, attached: true, content_type: 'image/png',
41
- dimension: { width: 200, height: 200 }
42
- validates :photos, attached: true, content_type: ['image/png', 'image/jpeg'],
43
- dimension: { width: { min: 800, max: 2400 },
44
- height: { min: 600, max: 1800 }, message: 'is not given between dimension' }
45
- validates :image, attached: true,
46
- processable_image: true,
47
- content_type: ['image/png', 'image/jpeg'],
48
- aspect_ratio: :landscape
49
- end
45
+ gem 'active_storage_validations'
46
+ ```
47
+
48
+ And then execute:
49
+
50
+ ```sh
51
+ $ bundle
50
52
  ```
51
53
 
52
- or
54
+ ### Error messages (I18n)
55
+
56
+ Once you have installed the gem, you need to add the gem I18n error messages to your app. See [Internationalization (I18n)](#internationalization-i18n) section for more details.
57
+
58
+ ### Using image metadata validators
59
+
60
+ Optionally, to use the image metadata validators (`dimension`, `aspect_ratio` and `processable_file`), you will have to add one of the corresponding gems:
61
+
62
+ ```ruby
63
+ gem 'mini_magick', '>= 4.9.5'
64
+ # Or
65
+ gem 'ruby-vips', '>= 2.1.0'
66
+ ```
67
+
68
+ Plus, you have to be sure to have the corresponding command-line tool installed on your system. For example, to use `mini_magick` gem, you need to have `imagemagick` installed on your system (both on your local and in your CI / production environments).
69
+
70
+ ### Using video and audio metadata validators
71
+
72
+ To use the video and audio metadata validators (`dimension`, `aspect_ratio`, `processable_file` and `duration`), you will not need to add any gems. However you will need to have the `ffmpeg` command-line tool installed on your system (once again, be sure to have it installed both on your local and in your CI / production environments).
73
+
74
+ ### Using content type spoofing protection validator option
75
+
76
+ To use the `spoofing_protection` option with the `content_type` validator, you only need to have the UNIX `file` command on your system.
77
+
78
+ ## Validators
79
+
80
+ **List of validators:**
81
+ - [Attached](#attached): validates if file(s) attached
82
+ - [Limit](#limit): validates number of uploaded files
83
+ - [Content type](#content-type): validates file content type
84
+ - [Size](#size): validates file size
85
+ - [Total size](#total-size): validates total file size for several files
86
+ - [Dimension](#dimension): validates image / video dimensions
87
+ - [Duration](#duration): validates video / audio duration
88
+ - [Aspect ratio](#aspect-ratio): validates image / video aspect ratio
89
+ - [Processable file](#processable-file): validates if a file can be processed
90
+ <br>
91
+ <br>
53
92
 
93
+ **Proc usage**<br>
94
+ Every validator can use procs instead of values in all the validator examples:
54
95
  ```ruby
55
- class Project < ApplicationRecord
56
- has_one_attached :logo
57
- has_one_attached :preview
58
- has_one_attached :attachment
59
- has_many_attached :documents
60
-
61
- validates :title, presence: true
62
-
63
- validates :logo, attached: true, size: { less_than: 100.megabytes , message: 'is too large' }
64
- validates :preview, attached: true, size: { between: 1.kilobyte..100.megabytes , message: 'is not given between size' }
65
- validates :attachment, attached: true, content_type: { in: 'application/pdf', message: 'is not a PDF' }
66
- validates :documents, limit: { min: 1, max: 3 }, total_size: { less_than: 5.megabytes }
96
+ class User < ApplicationRecord
97
+ has_many_attached :files
98
+
99
+ validates :files, limit: { max: -> (record) { record.admin? ? 100 : 10 } }
67
100
  end
68
101
  ```
69
102
 
70
- ### More examples
103
+ **Performance optimization**<br>
104
+ Some validators rely on an expensive operation (metadata analysis and content type analysis). To mitigate the performance cost, the gem leverages the `ActiveStorage::Blob.metadata` method to store retrieved metadata. Therefore, once the file has been analyzed by our gem, the expensive analysis operation will not be triggered again for new validations.
105
+
106
+ As stated in the Rails documentation: "Blobs are intended to be immutable in so far as their reference to a specific file goes". We based our performance optimization on the same assumption, so if you do not follow it, the gem will not work as expected.
107
+
108
+ ---
109
+
110
+ ### Attached
111
+
112
+ Validates if the attachment is present.
113
+
114
+ #### Options
71
115
 
72
- - Content type validation using symbols or regex.
116
+ The `attached` validator has no options.
73
117
 
118
+ #### Examples
119
+
120
+ Use it like this:
74
121
  ```ruby
75
122
  class User < ApplicationRecord
76
123
  has_one_attached :avatar
77
- has_many_attached :photos
78
124
 
79
- validates :avatar, attached: true, content_type: :png
80
- # or
81
- validates :photos, attached: true, content_type: [:png, :jpg, :jpeg]
82
- # or
83
- validates :avatar, content_type: /\Aimage\/.*\z/
125
+ validates :avatar, attached: true # ensures that avatar has an attached file
84
126
  end
85
127
  ```
86
- Please note that the symbol types must be registered by [`Marcel::EXTENSIONS`](https://github.com/rails/marcel/blob/main/lib/marcel/tables.rb) that's used by this gem to infer the full content type.
87
- Example code for adding a new content type to Marcel:
88
- ```ruby
89
- # config/initializers/mime_types.rb
90
- Marcel::MimeType.extend "application/ino", extensions: %w(ino), parents: "text/plain" # Registering arduino INO files
128
+
129
+ #### Error messages (I18n)
130
+
131
+ ```yml
132
+ en:
133
+ errors:
134
+ messages:
135
+ blank: "can't be blank"
91
136
  ```
92
137
 
93
- **Content type spoofing protection**
138
+ The error message for this validator relies on Rails own `blank` error message.
94
139
 
95
- File content type spoofing happens when an ill-intentioned user uploads a file which hides its true content type by faking its extension and its declared content type value. For example, a user may try to upload a `.exe` file (application/x-msdownload content type) dissimulated as a `.jpg` file (image/jpeg content type).
140
+ ---
96
141
 
97
- By default, the gem does not prevent content type spoofing (prevent it by default is a breaking change that will be implemented in v2). The spoofing protection relies on both the linux `file` command and `Marcel` gem. Be careful, since it needs to load the whole file io to perform the analysis, it will use a lot of RAM for very large files. Therefore it could be a wise decision not to enable it in this case.
142
+ ### Limit
98
143
 
99
- Take note that the `file` analyzer will not find the exactly same content type as the ActiveStorage blob (its content type detection relies on a different logic using content+filename+extension). To handle this issue, we consider a close parent content type to be a match. For example, for an ActiveStorage blob which content type is `video/x-ms-wmv`, the `file` analyzer will probably detect a `video/x-ms-asf` content type, this will be considered as a valid match because these 2 content types are closely related. The correlation mapping is based on `Marcel::TYPE_PARENTS`.
144
+ Validates the number of uploaded files.
100
145
 
101
- The difficulty to accurately predict a mime type may generate false positives, if so there are two solutions available:
102
- - If the ActiveStorage blob content type is closely related to the detected content type using the `file` analyzer, you can enhance `Marcel::TYPE_PARENTS` mapping using `Marcel::MimeType.extend "application/x-rar-compressed", parents: %(application/x-rar)` in the `config/initializers/mime_types.rb` file. (Please drop an issue so we can add it to the gem for everyone!)
103
- - If the ActiveStorage blob content type is not closely related, you still can disable the content type spoofing protection in the validator, if so, please drop us an issue so we can fix it for everyone!
146
+ #### Options
104
147
 
148
+ The `limit` validator has 2 possible options:
149
+ - `min`: defines the minimum allowed number of files
150
+ - `max`: defines the maximum allowed number of files
151
+
152
+ #### Examples
153
+
154
+ Use it like this:
105
155
  ```ruby
106
156
  class User < ApplicationRecord
107
- has_one_attached :avatar
157
+ has_many_attached :certificates
108
158
 
109
- validates :avatar, attached: true, content_type: :png # spoofing_protection not enabled, at your own risks!
110
- validates :avatar, attached: true, content_type: { with: :png, spoofing_protection: true } # spoofing_protection enabled
159
+ validates :certificates, limit: { min: 1, max: 10 } # restricts the number of files to between 1 and 10
111
160
  end
112
161
  ```
113
162
 
163
+ #### Error messages (I18n)
114
164
 
115
- - Dimension validation with `width`, `height` and `in`.
165
+ ```yml
166
+ en:
167
+ errors:
168
+ messages:
169
+ limit_out_of_range:
170
+ zero: "no files attached (must have between %{min} and %{max} files)"
171
+ one: "only 1 file attached (must have between %{min} and %{max} files)"
172
+ other: "total number of files must be between %{min} and %{max} files (there are %{count} files attached)"
173
+ limit_min_not_reached:
174
+ zero: "no files attached (must have at least %{min} files)"
175
+ one: "only 1 file attached (must have at least %{min} files)"
176
+ other: "%{count} files attached (must have at least %{min} files)"
177
+ limit_max_exceeded:
178
+ zero: "no files attached (maximum is %{max} files)"
179
+ one: "too many files attached (maximum is %{max} files, got %{count})"
180
+ other: "too many files attached (maximum is %{max} files, got %{count})"
181
+ ```
182
+
183
+ The `limit` validator error messages expose 3 values that you can use:
184
+ - `min` containing the minimum allowed number of files (e.g. `1`)
185
+ - `max` containing the maximum allowed number of files (e.g. `10`)
186
+ - `count` containing the current number of files (e.g. `5`)
187
+
188
+ ---
189
+
190
+ ### Content type
191
+
192
+ Validates if the attachment has an allowed content type.
116
193
 
194
+ #### Options
195
+
196
+ The `content_type` validator has 3 possible options:
197
+ - `with`: defines the exact allowed content type (string, symbol or regex)
198
+ - `in`: defines the allowed content types (array of strings or symbols)
199
+ - `spoofing_protection`: enables content type spoofing protection (boolean, defaults to `false`)
200
+
201
+ As mentioned above, this validator can define content types in several ways:
202
+ - String: `image/png` or `png`
203
+ - Symbol: `:png`
204
+ - Regex: `/\Avideo\/.*\z/`
205
+
206
+ #### Examples
207
+
208
+ Use it like this:
117
209
  ```ruby
118
210
  class User < ApplicationRecord
119
211
  has_one_attached :avatar
120
- has_many_attached :photos
121
212
 
122
- validates :avatar, dimension: { width: { in: 80..100 }, message: 'is not given between dimension' }
123
- validates :photos, dimension: { height: { in: 600..1800 } }
213
+ validates :avatar, content_type: 'image/png' # only allows PNG images
214
+ validates :avatar, content_type: :png # only allows PNG images, same as { with: :png }
215
+ validates :avatar, content_type: /\Avideo\/.*\z/ # only allows video files
216
+ validates :avatar, content_type: ['image/png', 'image/jpeg'] # only allows PNG and JPEG images
217
+ validates :avatar, content_type: { in: [:png, :jpeg], spoofing_protection: true } # only allows PNG, JPEG and their variants, with spoofing protection enabled
124
218
  end
125
219
  ```
126
220
 
127
- - Dimension validation with `min` and `max` range for width and height:
221
+ #### Best practices
222
+
223
+ When using the `content_type` validator, it is recommended to reflect the allowed content types in the html [`accept`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept) attribute in the corresponding file field in your views. This will prevent users from trying to upload files with not allowed content types (however it is only an UX improvement, a malicious user can still try to upload files with not allowed content types therefore the backend validation).
128
224
 
225
+ For example, if you want to allow PNG and JPEG images only, you can do this:
129
226
  ```ruby
130
227
  class User < ApplicationRecord
228
+ ACCEPTED_CONTENT_TYPES = ['image/png', 'image/jpeg'].freeze
229
+
131
230
  has_one_attached :avatar
132
- has_many_attached :photos
133
-
134
- validates :avatar, dimension: { min: 200..100 }
135
- # Equivalent to:
136
- # validates :avatar, dimension: { width: { min: 200 }, height: { min: 100 } }
137
- validates :photos, dimension: { min: 200..100, max: 400..200 }
138
- # Equivalent to:
139
- # validates :avatar, dimension: { width: { min: 200, max: 400 }, height: { min: 100, max: 200 } }
231
+
232
+ validates :avatar, content_type: ACCEPTED_CONTENT_TYPES
140
233
  end
141
234
  ```
142
235
 
143
- - Aspect ratio validation:
236
+ ```erb
237
+ <%= form_with model: @user do |f| %>
238
+ <%= f.file_field :avatar,
239
+ accept: ACCEPTED_CONTENT_TYPES.join(',') %>
240
+ <% end %>
241
+ ```
242
+
243
+ #### Content type shorthands
144
244
 
245
+ If you choose to use a content_type 'shorthand' (like `png`), note that it will be converted to a full content type using `Marcel::MimeType.for` under the hood. Therefore, you should check if the content_type is registered by [`Marcel::EXTENSIONS`](https://github.com/rails/marcel/blob/main/lib/marcel/tables.rb). If it's not, you can register it by adding the following code to your `config/initializers/mime_types.rb` file:
145
246
  ```ruby
146
- class User < ApplicationRecord
147
- has_one_attached :avatar
148
- has_one_attached :photo
149
- has_many_attached :photos
247
+ Marcel::MimeType.extend "application/ino", extensions: %w(ino), parents: "text/plain" # Registering arduino INO files
248
+ ```
150
249
 
151
- validates :avatar, aspect_ratio: :square
152
- validates :photo, aspect_ratio: :landscape
250
+ #### Content type spoofing protection
153
251
 
154
- # you can also pass dynamic aspect ratio, like :is_4_3, :is_16_9, etc
155
- validates :photos, aspect_ratio: :is_4_3
156
- end
252
+ By default, the gem does not prevent content type spoofing. You can enable it by setting the `spoofing_protection` option to `true` in your validator options.
253
+
254
+ <details>
255
+ <summary>
256
+ What is content type spoofing?
257
+ </summary>
258
+
259
+ File content type spoofing happens when an ill-intentioned user uploads a file which hides its true content type by faking its extension and its declared content type value. For example, a user may try to upload a `.exe` file (application/x-msdownload content type) dissimulated as a `.jpg` file (image/jpeg content type).
260
+ </details>
261
+
262
+ <details>
263
+ <summary>
264
+ How do we prevent it?
265
+ </summary>
266
+
267
+ The spoofing protection relies on both the UNIX `file` command and `Marcel` gem. Be careful, since it needs to load the whole file io to perform the analysis, it will use a lot of RAM for very large files. Therefore it could be a wise decision not to enable it in this case.
268
+
269
+ Take note that the `file` analyzer will not find the exactly same content type as the ActiveStorage blob (ActiveStorage content type detection relies on a different logic using first 4kb of content + filename + extension). To handle this issue, we consider a close parent content type to be a match. For example, for an ActiveStorage blob which content type is `video/x-ms-wmv`, the `file` analyzer will probably detect a `video/x-ms-asf` content type, this will be considered as a valid match because these 2 content types are closely related. The correlation mapping is based on `Marcel::TYPE_PARENTS` table.
270
+ </details>
271
+
272
+ <details>
273
+ <summary>
274
+ Edge cases
275
+ </summary>
276
+
277
+ The difficulty to accurately predict a mime type may generate false positives, if so there are two solutions available:
278
+ - If the ActiveStorage blob content type is closely related to the detected content type using the `file` analyzer, you can enhance `Marcel::TYPE_PARENTS` mapping using `Marcel::MimeType.extend "application/x-rar-compressed", parents: %(application/x-rar)` in the `config/initializers/mime_types.rb` file. (Please drop an issue so we can add it to the gem for everyone!)
279
+ - If the ActiveStorage blob content type is not closely related, you still can disable the content type spoofing protection in the validator, if so, please drop us an issue so we can fix it for everyone!
280
+ </details>
281
+
282
+
283
+ #### Error messages (I18n)
284
+
285
+ ```yml
286
+ en:
287
+ errors:
288
+ messages:
289
+ content_type_invalid:
290
+ one: "has an invalid content type (authorized content type is %{authorized_human_content_types})"
291
+ other: "has an invalid content type (authorized content types are %{authorized_human_content_types})"
292
+ content_type_spoofed:
293
+ one: "has a content type that is not equivalent to the one that is detected through its content (authorized content type is %{authorized_human_content_types})"
294
+ other: "has a content type that is not equivalent to the one that is detected through its content (authorized content types are %{authorized_human_content_types})"
157
295
  ```
158
296
 
159
- - Proc Usage:
297
+ The `content_type` validator error messages expose 7 values that you can use:
298
+ - `content_type` containing the content type of the sent file (e.g. `image/png`)
299
+ - `human_content_type` containing a more user-friendly version of the sent file content type (e.g. 'TXT' for 'text/plain')
300
+ - `detected_content_type` containing the detected content type of the sent file using `spoofing_protection` option (e.g. `image/png`)
301
+ - `detected_human_content_type` containing a more user-friendly version of the sent file detected content type using `spoofing_protection` option (e.g. 'TXT' for 'text/plain')
302
+ - `authorized_human_content_types` containing the list of authorized content types (e.g. 'PNG, JPEG' for `['image/png', 'image/jpeg']`)
303
+ - `count` containing the number of authorized content types (e.g. `2`)
304
+ - `filename` containing the filename
305
+
306
+ ---
307
+
308
+ ### Size
309
+
310
+ Validates each attached file size.
311
+
312
+ #### Options
313
+
314
+ The `size` validator has 5 possible options:
315
+ - `less_than`: defines the strict maximum allowed file size
316
+ - `less_than_or_equal_to`: defines the maximum allowed file size
317
+ - `greater_than`: defines the strict minimum allowed file size
318
+ - `greater_than_or_equal_to`: defines the minimum allowed file size
319
+ - `between`: defines the allowed file size range
320
+
321
+ #### Examples
160
322
 
161
- Procs can be used instead of values in all the above examples. They will be called on every validation.
323
+ Use it like this:
162
324
  ```ruby
163
325
  class User < ApplicationRecord
164
- has_many_attached :proc_files
326
+ has_one_attached :avatar
165
327
 
166
- validates :proc_files, limit: { max: -> (record) { record.admin? ? 100 : 10 } }
328
+ validates :avatar, size: { less_than: 2.megabytes } # restricts the file size to < 2MB
329
+ validates :avatar, size: { less_than_or_equal_to: 2.megabytes } # restricts the file size to <= 2MB
330
+ validates :avatar, size: { greater_than: 1.kilobyte } # restricts the file size to > 1KB
331
+ validates :avatar, size: { greater_than_or_equal_to: 1.kilobyte } # restricts the file size to >= 1KB
332
+ validates :avatar, size: { between: 1.kilobyte..2.megabytes } # restricts the file size to between 1KB and 2MB
167
333
  end
168
-
169
334
  ```
170
335
 
171
- ## Internationalization (I18n)
336
+ #### Best practices
172
337
 
173
- Active Storage Validations uses I18n for error messages. For this, add these keys in your translation file:
338
+ It is always a good practice to limit the maximum file size to a reasonable value (like 2MB for avatar images). This helps prevent server storage issues, reduces upload/download times, and ensures better performance. Large files can consume excessive bandwidth and storage space, potentially impacting both server resources and user experience.
339
+ Plus, not setting a size limit inside your Rails app might lead into your server throwing a `413 Content Too Large` error, which is not as nice as a Rails validation error.
340
+
341
+ #### Error messages (I18n)
174
342
 
175
343
  ```yml
176
344
  en:
177
345
  errors:
178
346
  messages:
179
- content_type_invalid: "has an invalid content type"
180
- file_size_not_less_than: "file size must be less than %{max_size} (current size is %{file_size})"
181
- file_size_not_less_than_or_equal_to: "file size must be less than or equal to %{max_size} (current size is %{file_size})"
182
- file_size_not_greater_than: "file size must be greater than %{min_size} (current size is %{file_size})"
183
- file_size_not_greater_than_or_equal_to: "file size must be greater than or equal to %{min_size} (current size is %{file_size})"
184
- file_size_not_between: "file size must be between %{min_size} and %{max_size} (current size is %{file_size})"
185
- total_file_size_not_less_than: "total file size must be less than %{max_size} (current size is %{total_file_size})"
186
- total_file_size_not_less_than_or_equal_to: "total file size must be less than or equal to %{max_size} (current size is %{total_file_size})"
187
- total_file_size_not_greater_than: "total file size must be greater than %{min_size} (current size is %{total_file_size})"
188
- total_file_size_not_greater_than_or_equal_to: "total file size must be greater than or equal to %{min_size} (current size is %{total_file_size})"
189
- total_file_size_not_between: "total file size must be between %{min_size} and %{max_size} (current size is %{total_file_size})"
190
- limit_out_of_range: "total number is out of range"
191
- image_metadata_missing: "is not a valid image"
192
- dimension_min_inclusion: "must be greater than or equal to %{width} x %{height} pixel"
193
- dimension_max_inclusion: "must be less than or equal to %{width} x %{height} pixel"
194
- dimension_width_inclusion: "width is not included between %{min} and %{max} pixel"
195
- dimension_height_inclusion: "height is not included between %{min} and %{max} pixel"
196
- dimension_width_greater_than_or_equal_to: "width must be greater than or equal to %{length} pixel"
197
- dimension_height_greater_than_or_equal_to: "height must be greater than or equal to %{length} pixel"
198
- dimension_width_less_than_or_equal_to: "width must be less than or equal to %{length} pixel"
199
- dimension_height_less_than_or_equal_to: "height must be less than or equal to %{length} pixel"
200
- dimension_width_equal_to: "width must be equal to %{length} pixel"
201
- dimension_height_equal_to: "height must be equal to %{length} pixel"
202
- aspect_ratio_not_square: "must be a square image"
203
- aspect_ratio_not_portrait: "must be a portrait image"
204
- aspect_ratio_not_landscape: "must be a landscape image"
205
- aspect_ratio_is_not: "must have an aspect ratio of %{aspect_ratio}"
206
- image_not_processable: "is not a valid image"
207
- aspect_ratio_invalid: "has invalid aspect ratio"
208
- ```
209
-
210
- In several cases, Active Storage Validations provides variables to help you customize messages:
347
+ file_size_not_less_than: "file size must be less than %{max} (current size is %{file_size})"
348
+ file_size_not_less_than_or_equal_to: "file size must be less than or equal to %{max} (current size is %{file_size})"
349
+ file_size_not_greater_than: "file size must be greater than %{min} (current size is %{file_size})"
350
+ file_size_not_greater_than_or_equal_to: "file size must be greater than or equal to %{min} (current size is %{file_size})"
351
+ file_size_not_between: "file size must be between %{min} and %{max} (current size is %{file_size})"
352
+ ```
211
353
 
212
- ### Aspect ratio
213
- The keys starting with `aspect_ratio_` support two variables that you can use:
214
- - `aspect_ratio` containing the expected aspect ratio, especially useful for custom aspect ratio
354
+ The `size` validator error messages expose 4 values that you can use:
355
+ - `file_size` containing the current file size (e.g. `1.5MB`)
356
+ - `min` containing the minimum allowed file size (e.g. `1KB`)
357
+ - `max` containing the maximum allowed file size (e.g. `2MB`)
215
358
  - `filename` containing the current file name
216
359
 
217
- For example :
360
+ ---
361
+
362
+ ### Total size
363
+
364
+ Validates the total file size for several files.
365
+
366
+ #### Options
367
+
368
+ The `total_size` validator has 5 possible options:
369
+ - `less_than`: defines the strict maximum allowed total file size
370
+ - `less_than_or_equal_to`: defines the maximum allowed total file size
371
+ - `greater_than`: defines the strict minimum allowed total file size
372
+ - `greater_than_or_equal_to`: defines the minimum allowed total file size
373
+ - `between`: defines the allowed total file size range
374
+
375
+ #### Examples
376
+
377
+ Use it like this:
378
+ ```ruby
379
+ class User < ApplicationRecord
380
+ has_many_attached :certificates
381
+
382
+ validates :certificates, total_size: { less_than: 10.megabytes } # restricts the total size to < 10MB
383
+ validates :certificates, total_size: { less_than_or_equal_to: 10.megabytes } # restricts the total size to <= 10MB
384
+ validates :certificates, total_size: { greater_than: 1.kilobyte } # restricts the total size to > 1KB
385
+ validates :certificates, total_size: { greater_than_or_equal_to: 1.kilobyte } # restricts the total size to >= 1KB
386
+ validates :certificates, total_size: { between: 1.kilobyte..10.megabytes } # restricts the total size to between 1KB and 10MB
387
+ end
388
+ ```
389
+
390
+ #### Error messages (I18n)
218
391
 
219
392
  ```yml
220
- aspect_ratio_is_not: "must be a %{aspect_ratio} image"
393
+ en:
394
+ errors:
395
+ messages:
396
+ total_file_size_not_less_than: "total file size must be less than %{max} (current size is %{total_file_size})"
397
+ total_file_size_not_less_than_or_equal_to: "total file size must be less than or equal to %{max} (current size is %{total_file_size})"
398
+ total_file_size_not_greater_than: "total file size must be greater than %{min} (current size is %{total_file_size})"
399
+ total_file_size_not_greater_than_or_equal_to: "total file size must be greater than or equal to %{min} (current size is %{total_file_size})"
400
+ total_file_size_not_between: "total file size must be between %{min} and %{max} (current size is %{total_file_size})"
221
401
  ```
222
402
 
223
- ### Content type
224
- The `content_type_invalid` key has three variables that you can use:
225
- - `content_type` containing the exact content type of the sent file
226
- - `human_content_type` containing a more user-friendly version of the sent file content type (e.g. 'TXT' for 'text/plain')
227
- - `authorized_types` containing the list of authorized content types
228
- - `filename` containing the current file name
403
+ The `total_size` validator error messages expose 4 values that you can use:
404
+ - `total_file_size` containing the current total file size (e.g. `1.5MB`)
405
+ - `min` containing the minimum allowed total file size (e.g. `1KB`)
406
+ - `max` containing the maximum allowed total file size (e.g. `2MB`)
407
+
408
+ ---
409
+
410
+ ### Dimension
229
411
 
230
- For example :
412
+ Validates the dimension of the attached image / video files.
413
+
414
+ #### Options
415
+
416
+ The `dimension` validator has several possible options:
417
+ - `width`: defines the exact allowed width (integer)
418
+ - `min`: defines the minimum allowed width (integer)
419
+ - `max`: defines the maximum allowed width (integer)
420
+ - `in`: defines the allowed width range (range)
421
+ - `height`: defines the exact allowed height (integer)
422
+ - `min`: defines the minimum allowed height (integer)
423
+ - `max`: defines the maximum allowed height (integer)
424
+ - `in`: defines the allowed height range (range)
425
+ - `min`: defines the minimum allowed width and height (range)
426
+ - `max`: defines the maximum allowed width and height (range)
427
+
428
+ #### Examples
429
+
430
+ Use it like this:
431
+ ```ruby
432
+ class User < ApplicationRecord
433
+ has_one_attached :avatar
434
+
435
+ validates :avatar, dimension: { width: 100 } # restricts the width to 100 pixels
436
+ validates :avatar, dimension: { width: { min: 80, max: 100 } } # restricts the width to between 80 and 100 pixels
437
+ validates :avatar, dimension: { width: { in: 80..100 } } # restricts the width to between 80 and 100 pixels
438
+ validates :avatar, dimension: { height: 100 } # restricts the height to 100 pixels
439
+ validates :avatar, dimension: { height: { min: 600, max: 1800 } } # restricts the height to between 600 and 1800 pixels
440
+ validates :avatar, dimension: { height: { in: 600..1800 } } # restricts the height to between 600 and 1800 pixels
441
+ validates :avatar, dimension: { min: 80..600, max: 100..1800 } # restricts the width to between 80 and 100 pixels, and the height to between 600 and 1800 pixels
442
+ end
443
+ ```
444
+
445
+ #### Error messages (I18n)
231
446
 
232
447
  ```yml
233
- content_type_invalid: "has an invalid content type : %{content_type}, authorized types are %{authorized_types}"
448
+ en:
449
+ errors:
450
+ messages:
451
+ dimension_min_not_included_in: "must be greater than or equal to %{width} x %{height} pixel"
452
+ dimension_max_not_included_in: "must be less than or equal to %{width} x %{height} pixel"
453
+ dimension_width_not_included_in: "width is not included between %{min} and %{max} pixel"
454
+ dimension_height_not_included_in: "height is not included between %{min} and %{max} pixel"
455
+ dimension_width_not_greater_than_or_equal_to: "width must be greater than or equal to %{length} pixel"
456
+ dimension_height_not_greater_than_or_equal_to: "height must be greater than or equal to %{length} pixel"
457
+ dimension_width_not_less_than_or_equal_to: "width must be less than or equal to %{length} pixel"
458
+ dimension_height_not_less_than_or_equal_to: "height must be less than or equal to %{length} pixel"
459
+ dimension_width_not_equal_to: "width must be equal to %{length} pixel"
460
+ dimension_height_not_equal_to: "height must be equal to %{length} pixel"
461
+ media_metadata_missing: "is not a valid media file"
234
462
  ```
235
463
 
236
- ### Dimension
237
- The keys starting with `dimension_` support six variables that you can use:
464
+ The `dimension` validator error messages expose 6 values that you can use:
238
465
  - `min` containing the minimum width or height allowed
239
466
  - `max` containing the maximum width or height allowed
240
467
  - `width` containing the minimum or maximum width allowed
241
468
  - `height` containing the minimum or maximum width allowed
242
469
  - `length` containing the exact width or height allowed
243
- - `filename` containing the current file name
470
+ - `filename` containing the current filename in error
244
471
 
245
- For example :
472
+ ---
246
473
 
247
- ```yml
248
- dimension_min_inclusion: "must be greater than or equal to %{width} x %{height} pixel."
249
- ```
474
+ ### Duration
250
475
 
251
- ### File size
252
- The keys starting with `file_size_not_` support four variables that you can use:
253
- - `file_size` containing the current file size
254
- - `min` containing the minimum file size
255
- - `max` containing the maximum file size
256
- - `filename` containing the current file name
476
+ Validates the duration of the attached audio / video files.
257
477
 
258
- For example :
478
+ #### Options
259
479
 
260
- ```yml
261
- file_size_not_between: "file size must be between %{min_size} and %{max_size} (current size is %{file_size})"
262
- ```
480
+ The `duration` validator has 5 possible options:
481
+ - `less_than`: defines the strict maximum allowed file duration
482
+ - `less_than_or_equal_to`: defines the maximum allowed file duration
483
+ - `greater_than`: defines the strict minimum allowed file duration
484
+ - `greater_than_or_equal_to`: defines the minimum allowed file duration
485
+ - `between`: defines the allowed file duration range
263
486
 
264
- ### Total file size
265
- The keys starting with `total_file_size_not_` support three variables that you can use:
266
- - `total_file_size` containing the current total file size
267
- - `min` containing the minimum file size
268
- - `max` containing the maximum file size
487
+ #### Examples
269
488
 
270
- For example :
489
+ Use it like this:
490
+ ```ruby
491
+ class User < ApplicationRecord
492
+ has_one_attached :avatar
271
493
 
272
- ```yml
273
- total_file_size_not_between: "total file size must be between %{min_size} and %{max_size} (current size is %{total_file_size})"
494
+ validates :avatar, duration: { less_than: 2.minutes } # restricts the file duration to < 2 minutes
495
+ validates :avatar, duration: { less_than_or_equal_to: 2.minutes } # restricts the file duration to <= 2 minutes
496
+ validates :avatar, duration: { greater_than: 1.second } # restricts the file duration to > 1 second
497
+ validates :avatar, duration: { greater_than_or_equal_to: 1.second } # restricts the file duration to >= 1 second
498
+ validates :avatar, duration: { between: 1.second..2.minutes } # restricts the file duration to between 1 second and 2 minutes
499
+ end
274
500
  ```
275
501
 
276
- ### Number of files
277
- The `limit_out_of_range` key supports two variables that you can use:
278
- - `min` containing the minimum number of files
279
- - `max` containing the maximum number of files
280
-
281
- For example :
502
+ #### Error messages (I18n)
282
503
 
283
504
  ```yml
284
- limit_out_of_range: "total number is out of range. range: [%{min}, %{max}]"
505
+ en:
506
+ errors:
507
+ messages:
508
+ duration_not_less_than: "duration must be less than %{max} (current duration is %{duration})"
509
+ duration_not_less_than_or_equal_to: "duration must be less than or equal to %{max} (current duration is %{duration})"
510
+ duration_not_greater_than: "duration must be greater than %{min} (current duration is %{duration})"
511
+ duration_not_greater_than_or_equal_to: "duration must be greater than or equal to %{min} (current duration is %{duration})"
512
+ duration_not_between: "duration must be between %{min} and %{max} (current duration is %{duration})"
285
513
  ```
286
514
 
287
- ### Processable image
288
- The `image_not_processable` key supports one variable that you can use:
515
+ The `duration` validator error messages expose 4 values that you can use:
516
+ - `duration` containing the current duration size (e.g. `2 minutes`)
517
+ - `min` containing the minimum allowed duration size (e.g. `1 second`)
518
+ - `max` containing the maximum allowed duration size (e.g. `2 minutes`)
289
519
  - `filename` containing the current file name
290
520
 
291
- For example :
521
+ ---
522
+
523
+ ### Aspect ratio
524
+
525
+ Validates the aspect ratio of the attached files.
526
+
527
+ #### Options
528
+
529
+ The `aspect_ratio` validator has several options:
530
+ - `with`: defines the exact allowed aspect ratio (e.g. `:is_16/9`)
531
+ - `in`: defines the allowed aspect ratios (e.g. `%i[square landscape]`)
532
+
533
+ This validator can define aspect ratios in several ways:
534
+ - Symbols:
535
+ - prebuilt aspect ratios: `:square`, `:portrait`, `:landscape`
536
+ - custom aspect ratios (it must be of type `is_xx_yy`): `:is_16_9`, `:is_4_3`, etc.
537
+
538
+ #### Examples
539
+
540
+ Use it like this:
541
+ ```ruby
542
+ class User < ApplicationRecord
543
+ has_one_attached :avatar
544
+
545
+ validates :avatar, aspect_ratio: :square # restricts the aspect ratio to 1:1
546
+ validates :avatar, aspect_ratio: :portrait # restricts the aspect ratio to x:y where y > x
547
+ validates :avatar, aspect_ratio: :landscape # restricts the aspect ratio to x:y where x > y
548
+ validates :avatar, aspect_ratio: :is_16_9 # restricts the aspect ratio to 16:9
549
+ validates :avatar, aspect_ratio: %i[square is_16_9] # restricts the aspect ratio to 1:1 and 16:9
550
+ end
551
+ ```
552
+
553
+ #### Error messages (I18n)
292
554
 
293
555
  ```yml
294
- image_not_processable: "is not a valid image (file: %{filename})"
556
+ en:
557
+ errors:
558
+ messages:
559
+ aspect_ratio_not_square: "must be square (current file is %{width}x%{height}px)"
560
+ aspect_ratio_not_portrait: "must be portrait (current file is %{width}x%{height}px)"
561
+ aspect_ratio_not_landscape: "must be landscape (current file is %{width}x%{height}px)"
562
+ aspect_ratio_not_x_y: "must be %{authorized_aspect_ratios} (current file is %{width}x%{height}px)"
563
+ aspect_ratio_invalid: "has an invalid aspect ratio (valid aspect ratios are %{authorized_aspect_ratios})"
564
+ media_metadata_missing: "is not a valid media file"
295
565
  ```
296
566
 
297
- ## Installation
567
+ The `aspect_ratio` validator error messages expose 4 values that you can use:
568
+ - `authorized_aspect_ratios` containing the authorized aspect ratios
569
+ - `width` containing the current width of the image/video
570
+ - `height` containing the current height of the image/video
571
+ - `filename` containing the current filename in error
572
+
573
+ ---
574
+
575
+ ### Processable file
576
+
577
+ Validates if the attached files can be processed by MiniMagick or Vips (image) or ffmpeg (video/audio).
578
+
579
+ #### Options
298
580
 
299
- Add this line to your application's Gemfile:
581
+ The `processable_file` validator has no options.
300
582
 
583
+ #### Examples
584
+
585
+ Use it like this:
301
586
  ```ruby
302
- gem 'active_storage_validations'
587
+ class User < ApplicationRecord
588
+ has_one_attached :avatar
303
589
 
304
- # Optional, to use :dimension validator or :aspect_ratio validator
305
- gem 'mini_magick', '>= 4.9.5'
306
- # Or
307
- gem 'ruby-vips', '>= 2.1.0'
590
+ validates :avatar, processable_file: true # ensures that the file is processable by MiniMagick or Vips (image) or ffmpeg (video/audio)
591
+ end
308
592
  ```
309
593
 
310
- And then execute:
594
+ #### Error messages (I18n)
311
595
 
312
- ```bash
313
- $ bundle
596
+ ```yml
597
+ en:
598
+ errors:
599
+ messages:
600
+ file_not_processable: "is not identified as a valid media file"
314
601
  ```
315
602
 
316
- ## Sample
603
+ The `processable_file` validator error messages expose 1 value that you can use:
604
+ - `filename` containing the current filename in error
605
+
606
+ ---
607
+
608
+ ## Upgrading from 1.x to 2.x
609
+
610
+ If you are upgrading from 1.x to 2.x, you will be pleased to note that a lot of things have been added and improved!
611
+
612
+ Added features:
613
+ - `duration` validator has been added for audio / video files
614
+ - `dimension` validator now supports videos
615
+ - `aspect_ratio` validator now supports videos
616
+ - `processable_image` validator is now `processable_file` validator and supports image/video/audio
617
+ - Major performance improvement have been added: we now only perform the expensive io analysis operation on the newly attached files. For previously attached files, we validate them using Rails `ActiveStorage::Blob#metadata` internal mecanism ([more here](https://github.com/rails/rails/blob/main/activestorage/app/models/active_storage/blob/analyzable.rb)).
618
+ - All error messages have been given an upgrade and new variables that you can use
619
+
620
+ But this major version bump also comes with some breaking changes. Below are the main breaking changes you need to be aware of:
621
+ - Error messages
622
+ - We advise you to replace all the v1 translations by the new v2 rather than changing them one by one. A majority of messages have been completely rewritten to be more consistent and easier to understand.
623
+ - If you wish to change them one by one, here is the list of changes to make:
624
+ - Some validator errors have been totally changed:
625
+ - `limit` validator keys have been totally reworked
626
+ - `dimension` validator keys have been totally reworked
627
+ - `content_type` validator keys have been totally reworked
628
+ - `processable_image` validator keys have been totally reworked
629
+ - Some keys have been changed:
630
+ - `image_metadata_missing` has been replaced by `media_metadata_missing`
631
+ - `aspect_ratio_is_not` has been replaced by `aspect_ratio_not_x_y`
632
+ - Some error messages variables names have been changed to improve readability:
633
+ - `aspect_ratio` validator:
634
+ - `aspect_ratio` has been replaced by `authorized_aspect_ratios`
635
+ - `content_type` validator:
636
+ - `authorized_types` has been replaced by `authorized_human_content_types`
637
+ - `size` validator:
638
+ - `min_size` has been replaced by `min`
639
+ - `max_size` has been replaced by `max`
640
+ - `total_size` validator:
641
+ - `min_size` has been replaced by `min`
642
+ - `max_size` has been replaced by `max`
643
+
644
+ - `content_type` validator
645
+ - The `:in` option now only accepts 'valid' content types (ie content types deemed by Marcel as valid).
646
+ - The check was mistakenly only performed on the `:with` option previously. Therefore, invalid content types were accepted in the `:in` option, which is not the expected behavior.
647
+ - This might break some cases when you had for example `content_type: ['image/png', 'image/jpg']`, because `image/jpg` is not a valid content type, it should be replaced by `image/jpeg`.
648
+ - An `ArgumentError` is now raised if `image/jpg` is used to make it easier to fix. You should now only use `image/jpeg`.
649
+
650
+ - `processable_image` validator
651
+ - The validator has been replaced by `processable_file` validator, be sure to replace `processable_image: true` to `processable_file: true`
652
+ - The associated matcher has also been updated accordingly, be sure to replace `validate_processable_image_of` to `validate_processable_file_of`
653
+
654
+ ## Internationalization (I18n)
655
+
656
+ Active Storage Validations uses I18n for error messages. Add these keys in your translation files to make them available:
317
657
 
318
- Very simple example of validation with file attached, content type check and custom error message.
658
+ ```yml
659
+ en:
660
+ errors:
661
+ messages:
662
+ content_type_invalid:
663
+ one: "has an invalid content type (authorized content type is %{authorized_human_content_types})"
664
+ other: "has an invalid content type (authorized content types are %{authorized_human_content_types})"
665
+ content_type_spoofed:
666
+ one: "has a content type that is not equivalent to the one that is detected through its content (authorized content type is %{authorized_human_content_types})"
667
+ other: "has a content type that is not equivalent to the one that is detected through its content (authorized content types are %{authorized_human_content_types})"
668
+ file_size_not_less_than: "file size must be less than %{max} (current size is %{file_size})"
669
+ file_size_not_less_than_or_equal_to: "file size must be less than or equal to %{max} (current size is %{file_size})"
670
+ file_size_not_greater_than: "file size must be greater than %{min} (current size is %{file_size})"
671
+ file_size_not_greater_than_or_equal_to: "file size must be greater than or equal to %{min} (current size is %{file_size})"
672
+ file_size_not_between: "file size must be between %{min} and %{max} (current size is %{file_size})"
673
+ total_file_size_not_less_than: "total file size must be less than %{max} (current size is %{total_file_size})"
674
+ total_file_size_not_less_than_or_equal_to: "total file size must be less than or equal to %{max} (current size is %{total_file_size})"
675
+ total_file_size_not_greater_than: "total file size must be greater than %{min} (current size is %{total_file_size})"
676
+ total_file_size_not_greater_than_or_equal_to: "total file size must be greater than or equal to %{min} (current size is %{total_file_size})"
677
+ total_file_size_not_between: "total file size must be between %{min} and %{max} (current size is %{total_file_size})"
678
+ duration_not_less_than: "duration must be less than %{max} (current duration is %{duration})"
679
+ duration_not_less_than_or_equal_to: "duration must be less than or equal to %{max} (current duration is %{duration})"
680
+ duration_not_greater_than: "duration must be greater than %{min} (current duration is %{duration})"
681
+ duration_not_greater_than_or_equal_to: "duration must be greater than or equal to %{min} (current duration is %{duration})"
682
+ duration_not_between: "duration must be between %{min} and %{max} (current duration is %{duration})"
683
+ limit_out_of_range:
684
+ zero: "no files attached (must have between %{min} and %{max} files)"
685
+ one: "only 1 file attached (must have between %{min} and %{max} files)"
686
+ other: "total number of files must be between %{min} and %{max} files (there are %{count} files attached)"
687
+ limit_min_not_reached:
688
+ zero: "no files attached (must have at least %{min} files)"
689
+ one: "only 1 file attached (must have at least %{min} files)"
690
+ other: "%{count} files attached (must have at least %{min} files)"
691
+ limit_max_exceeded:
692
+ zero: "no files attached (maximum is %{max} files)"
693
+ one: "too many files attached (maximum is %{max} files, got %{count})"
694
+ other: "too many files attached (maximum is %{max} files, got %{count})"
695
+ media_metadata_missing: "is not a valid media file"
696
+ dimension_min_not_included_in: "must be greater than or equal to %{width} x %{height} pixel"
697
+ dimension_max_not_included_in: "must be less than or equal to %{width} x %{height} pixel"
698
+ dimension_width_not_included_in: "width is not included between %{min} and %{max} pixel"
699
+ dimension_height_not_included_in: "height is not included between %{min} and %{max} pixel"
700
+ dimension_width_not_greater_than_or_equal_to: "width must be greater than or equal to %{length} pixel"
701
+ dimension_height_not_greater_than_or_equal_to: "height must be greater than or equal to %{length} pixel"
702
+ dimension_width_not_less_than_or_equal_to: "width must be less than or equal to %{length} pixel"
703
+ dimension_height_not_less_than_or_equal_to: "height must be less than or equal to %{length} pixel"
704
+ dimension_width_not_equal_to: "width must be equal to %{length} pixel"
705
+ dimension_height_not_equal_to: "height must be equal to %{length} pixel"
706
+ aspect_ratio_not_square: "must be square (current file is %{width}x%{height}px)"
707
+ aspect_ratio_not_portrait: "must be portrait (current file is %{width}x%{height}px)"
708
+ aspect_ratio_not_landscape: "must be landscape (current file is %{width}x%{height}px)"
709
+ aspect_ratio_not_x_y: "must be %{authorized_aspect_ratios} (current file is %{width}x%{height}px)"
710
+ aspect_ratio_invalid: "has an invalid aspect ratio (valid aspect ratios are %{authorized_aspect_ratios})"
711
+ file_not_processable: "is not identified as a valid media file"
712
+ ```
319
713
 
320
- [![Sample](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)
714
+ Other translation files are available [here](https://github.com/igorkasyanchuk/active_storage_validations/tree/master/config/locales).
321
715
 
322
716
  ## Test matchers
323
- Provides RSpec-compatible and Minitest-compatible matchers for testing the validators.
717
+
718
+ The gem also provides RSpec-compatible and Minitest-compatible matchers for testing the validators.
324
719
 
325
720
  ### RSpec
326
721
 
722
+ #### Setup
327
723
  In `spec_helper.rb`, you'll need to require the matchers:
328
724
 
329
725
  ```ruby
330
726
  require 'active_storage_validations/matchers'
331
727
  ```
332
728
 
333
- And _include_ the module:
729
+ And include the module:
334
730
 
335
731
  ```ruby
336
732
  RSpec.configure do |config|
@@ -338,6 +734,7 @@ RSpec.configure do |config|
338
734
  end
339
735
  ```
340
736
 
737
+ #### Matchers
341
738
  Matcher methods available:
342
739
 
343
740
  ```ruby
@@ -350,8 +747,8 @@ describe User do
350
747
  # attached
351
748
  it { is_expected.to validate_attached_of(:avatar) }
352
749
 
353
- # processable_image
354
- it { is_expected.to validate_processable_image_of(:avatar) }
750
+ # processable_file
751
+ it { is_expected.to validate_processable_file_of(:avatar) }
355
752
 
356
753
  # limit
357
754
  # #min, #max
@@ -389,6 +786,14 @@ describe User do
389
786
  it { is_expected.to validate_total_size_of(:avatar).greater_than(1.kilobyte) }
390
787
  it { is_expected.to validate_total_size_of(:avatar).greater_than_or_equal_to(1.kilobyte) }
391
788
  it { is_expected.to validate_total_size_of(:avatar).between(100..500.kilobytes) }
789
+
790
+ # duration:
791
+ # #less_than, #less_than_or_equal_to, #greater_than, #greater_than_or_equal_to, #between
792
+ it { is_expected.to validate_duration_of(:introduction).less_than(50.seconds) }
793
+ it { is_expected.to validate_duration_of(:introduction).less_than_or_equal_to(50.seconds) }
794
+ it { is_expected.to validate_duration_of(:introduction).greater_than(1.minute) }
795
+ it { is_expected.to validate_duration_of(:introduction).greater_than_or_equal_to(1.minute) }
796
+ it { is_expected.to validate_duration_of(:introduction).between(100..500.seconds) }
392
797
  end
393
798
  ```
394
799
  (Note that matcher methods are chainable)
@@ -410,6 +815,8 @@ end
410
815
  ```
411
816
 
412
817
  ### Minitest
818
+
819
+ #### Setup
413
820
  To use the matchers, make sure you have the [shoulda-context](https://github.com/thoughtbot/shoulda-context) gem up and running.
414
821
 
415
822
  You need to require the matchers:
@@ -418,7 +825,7 @@ You need to require the matchers:
418
825
  require 'active_storage_validations/matchers'
419
826
  ```
420
827
 
421
- And _extend_ the module:
828
+ And extend the module:
422
829
 
423
830
  ```ruby
424
831
  class ActiveSupport::TestCase
@@ -426,19 +833,19 @@ class ActiveSupport::TestCase
426
833
  end
427
834
  ```
428
835
 
836
+ #### Matchers
429
837
  Then you can use the matchers with the syntax specified in the RSpec section, just use `should validate_method` instead of `it { is_expected_to validate_method }` as specified in the [shoulda-context](https://github.com/thoughtbot/shoulda-context) gem.
430
838
 
431
- ## Todo
432
839
 
433
- * verify with remote storages (s3, etc)
434
- * verify how it works with direct upload
435
- * add more translations
840
+ ## Contributing
841
+
842
+ If you want to contribute to the project, you will have to fork the repository and create a new branch from the `master` branch. Then build your feature, or fix the issue, and create a pull request. Be sure to add tests for your changes.
436
843
 
437
- ## Tests & Contributing
844
+ Before submitting your pull request, run the tests to make sure everything works as expected.
438
845
 
439
- To run tests in root folder of gem:
846
+ To run the gem tests, launch the following commands in the root folder of gem repository:
440
847
 
441
- * `BUNDLE_GEMFILE=gemfiles/rails_6_1_4.gemfile bundle exec rake test` to run for Rails 6.1
848
+ * `BUNDLE_GEMFILE=gemfiles/rails_6_1_4.gemfile bundle exec rake test` to run for Rails 6.1.4
442
849
  * `BUNDLE_GEMFILE=gemfiles/rails_7_0.gemfile bundle exec rake test` to run for Rails 7.0
443
850
  * `BUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle exec rake test` to run for Rails 7.1
444
851
  * `BUNDLE_GEMFILE=gemfiles/rails_7_2.gemfile bundle exec rake test` to run for Rails 7.2
@@ -461,23 +868,22 @@ BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test
461
868
 
462
869
  Tips:
463
870
  - To focus a specific test, use the `focus` class method provided by [minitest-focus](https://github.com/minitest/minitest-focus)
464
- - To focus a specific file, use the TEST option provided by minitest, e.g. to only run size_validator_test.rb file you will execute the following command: `bundle exec rake test TEST=test/validators/size_validator_test.rb`
871
+ - To focus a specific file, use the TEST option provided by minitest, e.g. to only run `size_validator_test.rb` file you will launch the following command: `bundle exec rake test TEST=test/validators/size_validator_test.rb`
465
872
 
466
873
 
467
- ## Contributing
468
-
469
- You are welcome to contribute.
874
+ ## Additional information
470
875
 
471
- [<img src="https://opensource-heroes.com/svg/embed/igorkasyanchuk/active_storage_validations"
472
- />](https://opensource-heroes.com/r/igorkasyanchuk/active_storage_validations)
876
+ ### Contributors (BIG THANK YOU!)
473
877
 
474
- ## Contributors (BIG THANK YOU)
878
+ We have a long list of valued contributors. Check them all at:
475
879
 
476
880
  https://github.com/igorkasyanchuk/active_storage_validations/graphs/contributors
477
881
 
478
- ## License
882
+ ### License
479
883
 
480
884
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
481
885
 
886
+ <br>
887
+
482
888
  [<img src="https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true"
483
889
  />](https://www.railsjazz.com/?utm_source=github&utm_medium=bottom&utm_campaign=active_storage_validations)