activestorage-validator 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/rspec.yml +1 -1
- data/README.md +93 -10
- data/config/locales/de.yml +1 -0
- data/config/locales/en.yml +1 -0
- data/config/locales/es.yml +1 -0
- data/config/locales/fr.yml +1 -0
- data/config/locales/ja.yml +1 -0
- data/config/locales/pl.yml +1 -0
- data/config/locales/pt-br.yml +1 -0
- data/lib/activestorage/validator/blob.rb +23 -0
- data/lib/activestorage/validator/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6b66feb405437ad34641613c818d1fee5d8130235ab77b30ab07630ca7a09560
|
|
4
|
+
data.tar.gz: 00417b87e52603376cca1afc253e336b8ac5a090fbdbb386f43ae2062a73bdb0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 457cd27983de69c7d3e1e0f9c9226fb5b67410be17e4f64132220ce0953a4c4a9345eed292628343eb4f373bbdd83aab501f6036eb689a2627f86cff22207c49
|
|
7
|
+
data.tar.gz: a7ea46c6d09ea311b7329bc63b94355c543a568a562d8db413f447a479238aae295d42edbcf29665969b4083af97624ad16a8d984a9e41120fee2dac68135f92
|
data/.github/workflows/rspec.yml
CHANGED
data/README.md
CHANGED
|
@@ -3,43 +3,126 @@
|
|
|
3
3
|
|
|
4
4
|
# ActiveStorage Validator
|
|
5
5
|
|
|
6
|
-
ActiveStorage
|
|
6
|
+
A Rails gem that makes it easy to add validations such as `content_type` and file size to ActiveStorage attachments.
|
|
7
|
+
|
|
8
|
+
- Supports both `has_one_attached` and `has_many_attached` of ActiveStorage
|
|
9
|
+
- Flexible validations for content type and file size
|
|
7
10
|
|
|
8
11
|
## Installation
|
|
9
12
|
|
|
13
|
+
### Using Bundler
|
|
14
|
+
|
|
10
15
|
Add this line to your application's Gemfile:
|
|
11
16
|
|
|
12
17
|
```ruby
|
|
13
18
|
gem 'activestorage-validator'
|
|
14
19
|
```
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
Then execute:
|
|
17
22
|
|
|
18
|
-
$ bundle
|
|
23
|
+
$ bundle install
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
### Install with gem command
|
|
21
26
|
|
|
22
27
|
$ gem install activestorage-validator
|
|
23
28
|
|
|
24
29
|
## Usage
|
|
25
30
|
|
|
31
|
+
You can add validations to models with ActiveStorage attachments using the `blob` validator.
|
|
32
|
+
|
|
26
33
|
```ruby
|
|
27
34
|
class User < ApplicationRecord
|
|
28
35
|
has_one_attached :avatar
|
|
29
36
|
has_many_attached :photos
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
validates :
|
|
33
|
-
|
|
38
|
+
# Allow only web image files
|
|
39
|
+
validates :avatar, presence: true, blob: { content_type: :web_image }
|
|
40
|
+
|
|
41
|
+
# Allow only JPEG/PNG, up to 5MB per file
|
|
42
|
+
validates :photos, presence: true, blob: { content_type: ["image/png", "image/jpg", "image/jpeg"], size_range: 1..(5.megabytes) }
|
|
43
|
+
|
|
44
|
+
# You can also use a regular expression for content_type
|
|
45
|
+
# validates :photos, blob: { content_type: %r{^image/}, size_range: 1..(5.megabytes) }
|
|
46
|
+
|
|
47
|
+
# Require the filename to have one of the allowed extensions
|
|
48
|
+
# validates :audio, blob: { content_type: "audio/mpeg", extension: %w[mp3] }
|
|
34
49
|
end
|
|
35
50
|
```
|
|
36
51
|
|
|
37
|
-
Note
|
|
52
|
+
> **Note:** For `has_many_attached`, the size validation is applied to each file individually.
|
|
53
|
+
|
|
54
|
+
## Validation Options
|
|
55
|
+
|
|
56
|
+
| Option | Description |
|
|
57
|
+
|--------------|---------------------------------------------------------------------------------------------|
|
|
58
|
+
| content_type | Allowed MIME types. Accepts a symbol (`:web_image`, `:image`, `:audio`, `:video`, `:text`), an array of MIME types, a regular expression, or a string (single MIME type). |
|
|
59
|
+
| size_range | Allowed file size range (e.g. `1..5.megabytes`) |
|
|
60
|
+
| extension | Allowed file extensions. Accepts a String or an Array of Strings. Case-insensitive, leading dot optional. Files without an extension are rejected. |
|
|
61
|
+
|
|
62
|
+
### content_type Examples
|
|
63
|
+
|
|
64
|
+
- **Symbol**
|
|
65
|
+
- `:web_image` ... PNG, JPEG, GIF, WebP (special handling)
|
|
66
|
+
- `:image`, `:audio`, `:video`, `:text` ... Calls `blob.image?`, `blob.audio?`, etc. (ActiveStorage built-in predicate)
|
|
67
|
+
- **Array**
|
|
68
|
+
- `["image/png", "image/jpg", "image/jpeg"]` ... Only allow these MIME types
|
|
69
|
+
- **Regexp**
|
|
70
|
+
- `%r{^image/}` ... Allow any image type (MIME type starts with `image/`)
|
|
71
|
+
- **String**
|
|
72
|
+
- `"application/pdf"` ... Only allow PDF files
|
|
73
|
+
|
|
74
|
+
### extension Examples
|
|
75
|
+
|
|
76
|
+
- **String**
|
|
77
|
+
- `extension: "mp3"` ... Only allow `.mp3` files
|
|
78
|
+
- **Array**
|
|
79
|
+
- `extension: %w[mp3 m4a]` ... Allow either `.mp3` or `.m4a`
|
|
80
|
+
- **Combined with content_type (AND)**
|
|
81
|
+
- `blob: { content_type: "audio/mpeg", extension: %w[mp3] }` ... Reject both `my_podcast` (no extension) and `my_podcast.wav` (wrong extension), even when their MIME types match.
|
|
82
|
+
|
|
83
|
+
The leading dot is optional (`"mp3"` and `".mp3"` behave the same), and matching is case-insensitive (`PHOTO.JPG` matches `extension: "jpg"`).
|
|
84
|
+
|
|
85
|
+
## I18n Error Message Options
|
|
86
|
+
|
|
87
|
+
Validation error messages are I18n compatible. The following interpolation keys are available in your translation files, according to the validator's implementation:
|
|
88
|
+
|
|
89
|
+
| Key | Description |
|
|
90
|
+
|----------------|---------------------------------------------|
|
|
91
|
+
| filename | The uploaded file's name |
|
|
92
|
+
| min_size | The minimum allowed file size (humanized) |
|
|
93
|
+
| max_size | The maximum allowed file size (humanized) |
|
|
94
|
+
| extension | The list of allowed extensions, joined by `, ` |
|
|
95
|
+
|
|
96
|
+
The following error types are used:
|
|
97
|
+
|
|
98
|
+
- `content_type` (invalid content type)
|
|
99
|
+
- `min_size_error` (file is too small)
|
|
100
|
+
- `max_size_error` (file is too large)
|
|
101
|
+
- `extension` (invalid file extension)
|
|
102
|
+
|
|
103
|
+
Example (config/locales/en.yml):
|
|
104
|
+
|
|
105
|
+
```yaml
|
|
106
|
+
en:
|
|
107
|
+
errors:
|
|
108
|
+
messages:
|
|
109
|
+
content_type: "%{filename} has an invalid content type"
|
|
110
|
+
min_size_error: "%{filename} is too small (minimum is %{min_size})"
|
|
111
|
+
max_size_error: "%{filename} is too large (maximum is %{max_size})"
|
|
112
|
+
extension: "%{filename} has an invalid file extension (allowed: %{extension})"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Notes
|
|
116
|
+
|
|
117
|
+
- Some features may not work depending on your ActiveStorage or Rails version.
|
|
118
|
+
- Validation error messages are I18n compatible and support interpolation keys as described above.
|
|
38
119
|
|
|
39
120
|
## Contributing
|
|
40
121
|
|
|
41
|
-
Bug reports and pull requests are welcome on GitHub
|
|
122
|
+
Bug reports and pull requests are welcome on GitHub via [issues](https://github.com/aki77/activestorage-validator/issues) or [pull requests](https://github.com/aki77/activestorage-validator/pulls).
|
|
123
|
+
|
|
124
|
+
This project adheres to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
42
125
|
|
|
43
126
|
## License
|
|
44
127
|
|
|
45
|
-
|
|
128
|
+
This gem is released under the MIT License. See [LICENSE.txt](LICENSE.txt) for details.
|
data/config/locales/de.yml
CHANGED
data/config/locales/en.yml
CHANGED
data/config/locales/es.yml
CHANGED
data/config/locales/fr.yml
CHANGED
|
@@ -5,3 +5,4 @@ fr:
|
|
|
5
5
|
content_type: "n'est pas un format de fichier valide"
|
|
6
6
|
min_size_error: "La taille du fichier doit être supérieure à %{min_size}"
|
|
7
7
|
max_size_error: "La taille du fichier doit être inférieure à %{max_size}"
|
|
8
|
+
extension: "a une extension de fichier non valide (autorisées : %{extension})"
|
data/config/locales/ja.yml
CHANGED
data/config/locales/pl.yml
CHANGED
data/config/locales/pt-br.yml
CHANGED
|
@@ -16,6 +16,15 @@ module ActiveRecord
|
|
|
16
16
|
unless valid_content_type?(value.blob)
|
|
17
17
|
record.errors.add(attribute, :content_type, filename: value.blob.filename.to_s)
|
|
18
18
|
end
|
|
19
|
+
|
|
20
|
+
unless valid_extension?(value.blob)
|
|
21
|
+
record.errors.add(
|
|
22
|
+
attribute,
|
|
23
|
+
:extension,
|
|
24
|
+
filename: value.blob.filename.to_s,
|
|
25
|
+
extension: Array(options[:extension]).map { |e| normalize_extension(e) }.join(', ')
|
|
26
|
+
)
|
|
27
|
+
end
|
|
19
28
|
end
|
|
20
29
|
end
|
|
21
30
|
|
|
@@ -37,6 +46,20 @@ module ActiveRecord
|
|
|
37
46
|
options[:content_type] == blob.content_type
|
|
38
47
|
end
|
|
39
48
|
end
|
|
49
|
+
|
|
50
|
+
def valid_extension?(blob)
|
|
51
|
+
return true if options[:extension].nil?
|
|
52
|
+
|
|
53
|
+
allowed = Array(options[:extension]).map { |e| normalize_extension(e) }
|
|
54
|
+
actual = normalize_extension(blob.filename.extension)
|
|
55
|
+
return false if actual.empty?
|
|
56
|
+
|
|
57
|
+
allowed.include?(actual)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def normalize_extension(value)
|
|
61
|
+
value.to_s.downcase.delete_prefix('.')
|
|
62
|
+
end
|
|
40
63
|
end
|
|
41
64
|
end
|
|
42
65
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activestorage-validator
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- aki
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 2026-04-29 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: rails
|