active_storage_validations 2.0.2 → 2.0.3
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/README.md +9 -61
- data/config/locales/da.yml +1 -0
- data/config/locales/de.yml +1 -0
- data/config/locales/en-GB.yml +54 -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/it.yml +2 -1
- data/config/locales/ja.yml +1 -0
- data/config/locales/nl.yml +1 -0
- data/config/locales/pl.yml +1 -0
- data/config/locales/pt-BR.yml +1 -0
- data/config/locales/ru.yml +1 -0
- data/config/locales/sv.yml +1 -0
- data/config/locales/tr.yml +1 -0
- data/config/locales/uk.yml +1 -0
- data/config/locales/vi.yml +1 -0
- data/config/locales/zh-CN.yml +1 -0
- data/lib/active_storage_validations/analyzer/audio_analyzer.rb +2 -2
- data/lib/active_storage_validations/analyzer/content_type_analyzer.rb +5 -5
- data/lib/active_storage_validations/analyzer/image_analyzer/image_magick.rb +0 -1
- data/lib/active_storage_validations/analyzer/image_analyzer/vips.rb +0 -1
- data/lib/active_storage_validations/analyzer/video_analyzer.rb +4 -4
- data/lib/active_storage_validations/analyzer.rb +24 -23
- data/lib/active_storage_validations/aspect_ratio_validator.rb +19 -16
- data/lib/active_storage_validations/attached_validator.rb +3 -3
- data/lib/active_storage_validations/base_comparison_validator.rb +6 -6
- data/lib/active_storage_validations/content_type_validator.rb +34 -21
- data/lib/active_storage_validations/dimension_validator.rb +158 -82
- data/lib/active_storage_validations/duration_validator.rb +28 -14
- data/lib/active_storage_validations/extensors/asv_blob_metadatable.rb +15 -8
- data/lib/active_storage_validations/extensors/asv_marcelable.rb +1 -1
- data/lib/active_storage_validations/limit_validator.rb +27 -19
- data/lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb +12 -12
- data/lib/active_storage_validations/matchers/attached_validator_matcher.rb +6 -6
- data/lib/active_storage_validations/matchers/base_comparison_validator_matcher.rb +7 -7
- data/lib/active_storage_validations/matchers/content_type_validator_matcher.rb +15 -13
- data/lib/active_storage_validations/matchers/dimension_validator_matcher.rb +8 -8
- data/lib/active_storage_validations/matchers/duration_validator_matcher.rb +2 -2
- data/lib/active_storage_validations/matchers/limit_validator_matcher.rb +8 -8
- data/lib/active_storage_validations/matchers/processable_file_validator_matcher.rb +7 -7
- data/lib/active_storage_validations/matchers/shared/asv_active_storageable.rb +1 -1
- data/lib/active_storage_validations/matchers/shared/asv_allow_blankable.rb +1 -1
- data/lib/active_storage_validations/matchers/shared/asv_attachable.rb +9 -9
- data/lib/active_storage_validations/matchers/shared/asv_contextable.rb +11 -3
- data/lib/active_storage_validations/matchers/shared/asv_messageable.rb +1 -1
- data/lib/active_storage_validations/matchers/shared/asv_rspecable.rb +2 -2
- data/lib/active_storage_validations/matchers/shared/asv_validatable.rb +2 -2
- data/lib/active_storage_validations/matchers/size_validator_matcher.rb +2 -2
- data/lib/active_storage_validations/matchers/total_size_validator_matcher.rb +3 -3
- data/lib/active_storage_validations/matchers.rb +10 -10
- data/lib/active_storage_validations/processable_file_validator.rb +37 -37
- data/lib/active_storage_validations/railtie.rb +2 -2
- data/lib/active_storage_validations/shared/asv_attachable.rb +60 -28
- data/lib/active_storage_validations/shared/asv_errorable.rb +1 -1
- data/lib/active_storage_validations/shared/asv_symbolizable.rb +1 -1
- data/lib/active_storage_validations/size_validator.rb +1 -1
- data/lib/active_storage_validations/total_size_validator.rb +2 -2
- data/lib/active_storage_validations/version.rb +1 -1
- data/lib/active_storage_validations.rb +22 -22
- metadata +3 -2
@@ -3,13 +3,13 @@
|
|
3
3
|
# Big thank you to the paperclip validation matchers:
|
4
4
|
# https://github.com/thoughtbot/paperclip/blob/v6.1.0/lib/paperclip/matchers/validate_attachment_size_matcher.rb
|
5
5
|
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
6
|
+
require_relative "shared/asv_active_storageable"
|
7
|
+
require_relative "shared/asv_allow_blankable"
|
8
|
+
require_relative "shared/asv_attachable"
|
9
|
+
require_relative "shared/asv_contextable"
|
10
|
+
require_relative "shared/asv_messageable"
|
11
|
+
require_relative "shared/asv_rspecable"
|
12
|
+
require_relative "shared/asv_validatable"
|
13
13
|
|
14
14
|
module ActiveStorageValidations
|
15
15
|
module Matchers
|
@@ -3,13 +3,13 @@
|
|
3
3
|
# Big thank you to the paperclip validation matchers:
|
4
4
|
# https://github.com/thoughtbot/paperclip/blob/v6.1.0/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
|
5
5
|
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
6
|
+
require_relative "shared/asv_active_storageable"
|
7
|
+
require_relative "shared/asv_allow_blankable"
|
8
|
+
require_relative "shared/asv_attachable"
|
9
|
+
require_relative "shared/asv_contextable"
|
10
|
+
require_relative "shared/asv_messageable"
|
11
|
+
require_relative "shared/asv_rspecable"
|
12
|
+
require_relative "shared/asv_validatable"
|
13
13
|
|
14
14
|
module ActiveStorageValidations
|
15
15
|
module Matchers
|
@@ -40,18 +40,20 @@ module ActiveStorageValidations
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def failure_message
|
43
|
-
message = ["is expected to validate the content types of :#{@attribute_name}"]
|
43
|
+
message = [ "is expected to validate the content types of :#{@attribute_name}" ]
|
44
44
|
build_failure_message(message)
|
45
45
|
message.join("\n")
|
46
46
|
end
|
47
47
|
|
48
48
|
def allowing(*content_types)
|
49
|
-
|
49
|
+
types = content_types.flatten
|
50
|
+
@allowed_content_types = types.map { |content_type| normalize_content_type(content_type) }.flatten
|
50
51
|
self
|
51
52
|
end
|
52
53
|
|
53
54
|
def rejecting(*content_types)
|
54
|
-
|
55
|
+
types = content_types.flatten
|
56
|
+
@rejected_content_types = types.map { |content_type| normalize_content_type(content_type) }.flatten
|
55
57
|
self
|
56
58
|
end
|
57
59
|
|
@@ -122,14 +124,14 @@ module ActiveStorageValidations
|
|
122
124
|
end
|
123
125
|
|
124
126
|
def attach_invalid_content_type_file
|
125
|
-
@subject.public_send(@attribute_name).attach(attachment_for(
|
127
|
+
@subject.public_send(@attribute_name).attach(attachment_for("fake/fake"))
|
126
128
|
end
|
127
129
|
|
128
130
|
def attachment_for(content_type)
|
129
|
-
suffix = Marcel::TYPE_EXTS[content_type.to_s]&.first ||
|
131
|
+
suffix = Marcel::TYPE_EXTS[content_type.to_s]&.first || "fake"
|
130
132
|
|
131
133
|
{
|
132
|
-
io: Tempfile.new(
|
134
|
+
io: Tempfile.new("."),
|
133
135
|
filename: "test.#{suffix}",
|
134
136
|
content_type: content_type
|
135
137
|
}
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
3
|
+
require_relative "shared/asv_active_storageable"
|
4
|
+
require_relative "shared/asv_allow_blankable"
|
5
|
+
require_relative "shared/asv_attachable"
|
6
|
+
require_relative "shared/asv_contextable"
|
7
|
+
require_relative "shared/asv_messageable"
|
8
|
+
require_relative "shared/asv_rspecable"
|
9
|
+
require_relative "shared/asv_validatable"
|
10
10
|
|
11
11
|
module ActiveStorageValidations
|
12
12
|
module Matchers
|
@@ -37,7 +37,7 @@ module ActiveStorageValidations
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def failure_message
|
40
|
-
message = ["is expected to validate dimensions of :#{@attribute_name}"]
|
40
|
+
message = [ "is expected to validate dimensions of :#{@attribute_name}" ]
|
41
41
|
build_failure_message(message)
|
42
42
|
message.join("\n")
|
43
43
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "base_comparison_validator_matcher"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
module Matchers
|
@@ -14,7 +14,7 @@ module ActiveStorageValidations
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def failure_message
|
17
|
-
message = ["is expected to validate file duration of :#{@attribute_name}"]
|
17
|
+
message = [ "is expected to validate file duration of :#{@attribute_name}" ]
|
18
18
|
build_failure_message(message)
|
19
19
|
message.join("\n")
|
20
20
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
3
|
+
require_relative "shared/asv_active_storageable"
|
4
|
+
require_relative "shared/asv_allow_blankable"
|
5
|
+
require_relative "shared/asv_attachable"
|
6
|
+
require_relative "shared/asv_contextable"
|
7
|
+
require_relative "shared/asv_messageable"
|
8
|
+
require_relative "shared/asv_rspecable"
|
9
|
+
require_relative "shared/asv_validatable"
|
10
10
|
|
11
11
|
module ActiveStorageValidations
|
12
12
|
module Matchers
|
@@ -37,7 +37,7 @@ module ActiveStorageValidations
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def failure_message
|
40
|
-
message = ["is expected to validate limit file of :#{@attribute_name}"]
|
40
|
+
message = [ "is expected to validate limit file of :#{@attribute_name}" ]
|
41
41
|
build_failure_message(message)
|
42
42
|
message.join("\n")
|
43
43
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
3
|
+
require_relative "shared/asv_active_storageable"
|
4
|
+
require_relative "shared/asv_allow_blankable"
|
5
|
+
require_relative "shared/asv_attachable"
|
6
|
+
require_relative "shared/asv_contextable"
|
7
|
+
require_relative "shared/asv_messageable"
|
8
|
+
require_relative "shared/asv_rspecable"
|
9
|
+
require_relative "shared/asv_validatable"
|
10
10
|
|
11
11
|
module ActiveStorageValidations
|
12
12
|
module Matchers
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "active_support/concern"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
module Matchers
|
@@ -38,29 +38,29 @@ module ActiveStorageValidations
|
|
38
38
|
def dummy_file
|
39
39
|
{
|
40
40
|
io: io,
|
41
|
-
filename:
|
42
|
-
content_type:
|
41
|
+
filename: "test.png",
|
42
|
+
content_type: "image/png"
|
43
43
|
}
|
44
44
|
end
|
45
45
|
|
46
46
|
def processable_image
|
47
47
|
{
|
48
48
|
io: StringIO.new(image_data),
|
49
|
-
filename:
|
50
|
-
content_type:
|
49
|
+
filename: "processable_image.png",
|
50
|
+
content_type: "image/png"
|
51
51
|
}
|
52
52
|
end
|
53
53
|
|
54
54
|
def not_processable_image
|
55
55
|
{
|
56
|
-
io: Tempfile.new(
|
57
|
-
filename:
|
58
|
-
content_type:
|
56
|
+
io: Tempfile.new("."),
|
57
|
+
filename: "not_processable_image.txt",
|
58
|
+
content_type: "text/plain"
|
59
59
|
}
|
60
60
|
end
|
61
61
|
|
62
62
|
def io
|
63
|
-
@io ||= Tempfile.new(
|
63
|
+
@io ||= Tempfile.new("Hello world!")
|
64
64
|
end
|
65
65
|
|
66
66
|
def image_data
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "active_support/concern"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
module Matchers
|
@@ -21,8 +21,8 @@ module ActiveStorageValidations
|
|
21
21
|
def is_context_valid?
|
22
22
|
return true if !@context && attribute_validators.none? { |validator| validator.options[:on] }
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
ensure_context_present!
|
25
|
+
ensure_context_valid!
|
26
26
|
|
27
27
|
if @context.is_a?(Array)
|
28
28
|
(validator_contexts & @context.map(&:to_s)) == validator_contexts || raise_context_not_listed_error
|
@@ -31,6 +31,14 @@ module ActiveStorageValidations
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
def ensure_context_present!
|
35
|
+
raise ArgumentError, "This validator matcher needs the #on option to work since its validator has one" if !@context && attribute_validators.all? { |validator| validator.options[:on] }
|
36
|
+
end
|
37
|
+
|
38
|
+
def ensure_context_valid!
|
39
|
+
raise ArgumentError, "This validator matcher option only allows a symbol or an array" if !(@context.is_a?(Symbol) || @context.is_a?(Array))
|
40
|
+
end
|
41
|
+
|
34
42
|
def validator_contexts
|
35
43
|
attribute_validators.map do |validator|
|
36
44
|
case validator.options[:on]
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "active_support/concern"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
module Matchers
|
@@ -20,7 +20,7 @@ module ActiveStorageValidations
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def failure_message_when_negated
|
23
|
-
failure_message.sub(/is expected to validate/,
|
23
|
+
failure_message.sub(/is expected to validate/, "is expected not to validate")
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "active_support/concern"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
module Matchers
|
@@ -33,7 +33,7 @@ module ActiveStorageValidations
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def validator_class
|
36
|
-
self.class.name.gsub(/::Matchers|Matcher/,
|
36
|
+
self.class.name.gsub(/::Matchers|Matcher/, "").constantize
|
37
37
|
end
|
38
38
|
|
39
39
|
def attribute_validator
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "base_comparison_validator_matcher"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
module Matchers
|
@@ -14,7 +14,7 @@ module ActiveStorageValidations
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def failure_message
|
17
|
-
message = ["is expected to validate file size of :#{@attribute_name}"]
|
17
|
+
message = [ "is expected to validate file size of :#{@attribute_name}" ]
|
18
18
|
build_failure_message(message)
|
19
19
|
message.join("\n")
|
20
20
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "base_comparison_validator_matcher"
|
4
4
|
|
5
5
|
module ActiveStorageValidations
|
6
6
|
module Matchers
|
@@ -14,7 +14,7 @@ module ActiveStorageValidations
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def failure_message
|
17
|
-
message = ["is expected to validate total file size of :#{@attribute_name}"]
|
17
|
+
message = [ "is expected to validate total file size of :#{@attribute_name}" ]
|
18
18
|
build_failure_message(message)
|
19
19
|
message.join("\n")
|
20
20
|
end
|
@@ -23,7 +23,7 @@ module ActiveStorageValidations
|
|
23
23
|
|
24
24
|
def attach_file
|
25
25
|
# has_many_attached relation
|
26
|
-
@subject.public_send(@attribute_name).attach([dummy_blob])
|
26
|
+
@subject.public_send(@attribute_name).attach([ dummy_blob ])
|
27
27
|
@subject.public_send(@attribute_name)
|
28
28
|
end
|
29
29
|
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
3
|
+
require "active_storage_validations/matchers/aspect_ratio_validator_matcher"
|
4
|
+
require "active_storage_validations/matchers/attached_validator_matcher"
|
5
|
+
require "active_storage_validations/matchers/processable_file_validator_matcher"
|
6
|
+
require "active_storage_validations/matchers/limit_validator_matcher"
|
7
|
+
require "active_storage_validations/matchers/content_type_validator_matcher"
|
8
|
+
require "active_storage_validations/matchers/dimension_validator_matcher"
|
9
|
+
require "active_storage_validations/matchers/duration_validator_matcher"
|
10
|
+
require "active_storage_validations/matchers/size_validator_matcher"
|
11
|
+
require "active_storage_validations/matchers/total_size_validator_matcher"
|
12
12
|
|
13
13
|
module ActiveStorageValidations
|
14
14
|
module Matchers
|
@@ -22,7 +22,7 @@ module ActiveStorageValidations
|
|
22
22
|
RSpec::Mocks.allow_message(object, method) { result }
|
23
23
|
yield
|
24
24
|
else
|
25
|
-
raise
|
25
|
+
raise "Need either Minitest::Mock or RSpec::Mocks to run this validator matcher"
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -1,37 +1,37 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
|
9
|
-
module ActiveStorageValidations
|
10
|
-
class ProcessableFileValidator < ActiveModel::EachValidator # :nodoc
|
11
|
-
include ASVActiveStorageable
|
12
|
-
include ASVAnalyzable
|
13
|
-
include ASVAttachable
|
14
|
-
include ASVErrorable
|
15
|
-
include ASVSymbolizable
|
16
|
-
|
17
|
-
ERROR_TYPES = %i[
|
18
|
-
file_not_processable
|
19
|
-
].freeze
|
20
|
-
METADATA_KEYS = %i[].freeze
|
21
|
-
|
22
|
-
def validate_each(record, attribute, _value)
|
23
|
-
return if no_attachments?(record, attribute)
|
24
|
-
|
25
|
-
validate_changed_files_from_metadata(record, attribute, METADATA_KEYS)
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def is_valid?(record, attribute, attachable, metadata)
|
31
|
-
return if !metadata.empty?
|
32
|
-
|
33
|
-
errors_options = initialize_error_options(options, attachable)
|
34
|
-
add_error(record, attribute, ERROR_TYPES.first
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "shared/asv_active_storageable"
|
4
|
+
require_relative "shared/asv_analyzable"
|
5
|
+
require_relative "shared/asv_attachable"
|
6
|
+
require_relative "shared/asv_errorable"
|
7
|
+
require_relative "shared/asv_symbolizable"
|
8
|
+
|
9
|
+
module ActiveStorageValidations
|
10
|
+
class ProcessableFileValidator < ActiveModel::EachValidator # :nodoc
|
11
|
+
include ASVActiveStorageable
|
12
|
+
include ASVAnalyzable
|
13
|
+
include ASVAttachable
|
14
|
+
include ASVErrorable
|
15
|
+
include ASVSymbolizable
|
16
|
+
|
17
|
+
ERROR_TYPES = %i[
|
18
|
+
file_not_processable
|
19
|
+
].freeze
|
20
|
+
METADATA_KEYS = %i[].freeze
|
21
|
+
|
22
|
+
def validate_each(record, attribute, _value)
|
23
|
+
return if no_attachments?(record, attribute)
|
24
|
+
|
25
|
+
validate_changed_files_from_metadata(record, attribute, METADATA_KEYS)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def is_valid?(record, attribute, attachable, metadata)
|
31
|
+
return if !metadata.empty?
|
32
|
+
|
33
|
+
errors_options = initialize_error_options(options, attachable)
|
34
|
+
add_error(record, attribute, ERROR_TYPES.first, **errors_options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
module ActiveStorageValidations
|
4
4
|
class Railtie < ::Rails::Railtie
|
5
|
-
initializer
|
5
|
+
initializer "active_storage_validations.configure", after: :load_config_initializers do
|
6
6
|
ActiveSupport.on_load(:active_record) do
|
7
7
|
send :include, ActiveStorageValidations
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
initializer
|
11
|
+
initializer "active_storage_validations.extend_active_storage_blob" do
|
12
12
|
ActiveSupport.on_load(:active_storage_blob) do
|
13
13
|
include(ActiveStorageValidations::ASVBlobMetadatable)
|
14
14
|
end
|