active_storage_validations 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/Rakefile +2 -0
- data/lib/active_storage_validations.rb +3 -1
- data/lib/active_storage_validations/attached_validator.rb +7 -5
- data/lib/active_storage_validations/content_type_validator.rb +10 -10
- data/lib/active_storage_validations/engine.rb +2 -0
- data/lib/active_storage_validations/limit_validator.rb +19 -17
- data/lib/active_storage_validations/railtie.rb +2 -0
- data/lib/active_storage_validations/size_validator.rb +25 -27
- data/lib/active_storage_validations/version.rb +3 -1
- data/lib/tasks/active_storage_validations_tasks.rake +2 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5c26b96ccb72cbb6ca57cf20df590f334703e2e30e51e4f2ccc1ff5891a74f5
|
4
|
+
data.tar.gz: 401043685c41419a8ab9018de42f844a15d2d1219b74f0f1c37473840129bdea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0cda1fa4d225a0bf381fb08a2db575d1f3a2baef5b85bd954473455a26c454af2efc0de4b40ecc9156a9b553f38cd8653cb78f7c70a05c87291d0a0a01df94e
|
7
|
+
data.tar.gz: c98ff98594d610a57cc649f66bc7a5bd3ac135723dae52f68d807e6ccf0131e76fc5e9e577708a36badf20583ce08f61b7a0c9f3286e53f119e4e39f04714407
|
data/MIT-LICENSE
CHANGED
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_storage_validations/railtie'
|
2
4
|
require 'active_storage_validations/engine'
|
3
5
|
require 'active_storage_validations/attached_validator'
|
@@ -6,5 +8,5 @@ require 'active_storage_validations/size_validator'
|
|
6
8
|
require 'active_storage_validations/limit_validator'
|
7
9
|
|
8
10
|
ActiveSupport.on_load(:active_record) do
|
9
|
-
|
11
|
+
send :include, ActiveStorageValidations
|
10
12
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveStorageValidations
|
2
|
-
class AttachedValidator < ActiveModel::EachValidator
|
3
|
-
def validate_each(record, attribute,
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class AttachedValidator < ActiveModel::EachValidator # :nodoc:
|
5
|
+
def validate_each(record, attribute, _value)
|
6
|
+
return if record.send(attribute).attached?
|
7
|
+
|
8
|
+
record.errors.add(attribute, :blank)
|
7
9
|
end
|
8
10
|
end
|
9
11
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveStorageValidations
|
2
|
-
class ContentTypeValidator < ActiveModel::EachValidator
|
3
|
-
def validate_each(record, attribute,
|
4
|
+
class ContentTypeValidator < ActiveModel::EachValidator # :nodoc:
|
5
|
+
def validate_each(record, attribute, _value)
|
4
6
|
files = record.send(attribute)
|
5
7
|
|
6
|
-
return true
|
7
|
-
return true if types.empty?
|
8
|
+
return true if !files.attached? || types.empty?
|
8
9
|
|
9
10
|
files = Array.wrap(files)
|
10
11
|
|
@@ -12,12 +13,11 @@ module ActiveStorageValidations
|
|
12
13
|
errors_options[:message] = options[:message] if options[:message].present?
|
13
14
|
|
14
15
|
files.each do |file|
|
15
|
-
|
16
|
-
errors_options[:content_type] = content_type(file)
|
16
|
+
next if content_type_valid?(file)
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
errors_options[:content_type] = content_type(file)
|
19
|
+
record.errors.add(attribute, :content_type_invalid, errors_options)
|
20
|
+
break
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -26,7 +26,7 @@ module ActiveStorageValidations
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def types_to_human_format
|
29
|
-
types.join(
|
29
|
+
types.join(', ')
|
30
30
|
end
|
31
31
|
|
32
32
|
def content_type(file)
|
@@ -1,36 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveStorageValidations
|
2
|
-
class LimitValidator < ActiveModel::EachValidator
|
3
|
-
AVAILABLE_CHECKS = [
|
4
|
+
class LimitValidator < ActiveModel::EachValidator # :nodoc:
|
5
|
+
AVAILABLE_CHECKS = %i[max min].freeze
|
4
6
|
|
5
7
|
def check_validity!
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
return true if AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
|
9
|
+
|
10
|
+
raise ArgumentError, 'You must pass either :max or :min to the validator'
|
9
11
|
end
|
10
12
|
|
11
|
-
def validate_each(record, attribute,
|
13
|
+
def validate_each(record, attribute, _value)
|
12
14
|
files = record.send(attribute)
|
13
15
|
|
16
|
+
return true unless files.attached?
|
17
|
+
|
14
18
|
files = Array.wrap(files)
|
15
19
|
|
16
20
|
errors_options = {}
|
17
21
|
errors_options[:min] = options[:min]
|
18
22
|
errors_options[:max] = options[:max]
|
19
23
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
+
return true if files_count_valid?(files.count)
|
25
|
+
|
26
|
+
record.errors.add(attribute, options[:message].presence || :limit_out_of_range, errors_options)
|
24
27
|
end
|
25
28
|
|
26
29
|
def files_count_valid?(count)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
count >= options[:min]
|
30
|
+
if options[:max].present? && options[:min].present?
|
31
|
+
count >= options[:min] && count <= options[:max]
|
32
|
+
elsif options[:max].present?
|
33
|
+
count <= options[:max]
|
34
|
+
elsif options[:min].present?
|
35
|
+
count >= options[:min]
|
34
36
|
end
|
35
37
|
end
|
36
38
|
end
|
@@ -1,45 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveStorageValidations
|
2
|
-
class SizeValidator < ActiveModel::EachValidator
|
4
|
+
class SizeValidator < ActiveModel::EachValidator # :nodoc:
|
3
5
|
delegate :number_to_human_size, to: ActiveSupport::NumberHelper
|
4
6
|
|
5
|
-
AVAILABLE_CHECKS = [
|
7
|
+
AVAILABLE_CHECKS = %i[less_than less_than_or_equal_to greater_than greater_than_or_equal_to between].freeze
|
6
8
|
|
7
9
|
def check_validity!
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
return true if AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
|
11
|
+
|
12
|
+
raise ArgumentError, 'You must pass either :less_than, :greater_than, or :between to the validator'
|
11
13
|
end
|
12
14
|
|
13
|
-
def validate_each(record, attribute,
|
14
|
-
files = record.send(attribute)
|
15
|
+
def validate_each(record, attribute, _value)
|
15
16
|
# only attached
|
16
|
-
return true unless
|
17
|
+
return true unless record.send(attribute).attached?
|
17
18
|
|
18
|
-
files = Array.wrap(
|
19
|
+
files = Array.wrap(record.send(attribute))
|
19
20
|
|
20
21
|
files.each do |file|
|
21
|
-
if content_size_valid?(file)
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
next if content_size_valid?(file.blob.byte_size)
|
23
|
+
|
24
|
+
record.errors.add(attribute, options[:message].presence || "size #{number_to_human_size(file.blob.byte_size)} is not between required range")
|
25
|
+
break
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
|
-
def content_size_valid?(
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
when options[:greater_than_or_equal_to].present?
|
40
|
-
file_size <= options[:greater_than_or_equal_to]
|
29
|
+
def content_size_valid?(file_size)
|
30
|
+
if options[:between].present?
|
31
|
+
options[:between].include?(file_size)
|
32
|
+
elsif options[:less_than].present?
|
33
|
+
file_size < options[:less_than]
|
34
|
+
elsif options[:less_than_or_equal_to].present?
|
35
|
+
file_size <= options[:less_than_or_equal_to]
|
36
|
+
elsif options[:greater_than].present?
|
37
|
+
file_size > options[:greater_than]
|
38
|
+
elsif options[:greater_than_or_equal_to].present?
|
39
|
+
file_size >= options[:greater_than_or_equal_to]
|
41
40
|
end
|
42
41
|
end
|
43
42
|
end
|
44
|
-
|
45
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_storage_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: sqlite3
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -91,7 +105,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
105
|
- !ruby/object:Gem::Version
|
92
106
|
version: '0'
|
93
107
|
requirements: []
|
94
|
-
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.7.7
|
95
110
|
signing_key:
|
96
111
|
specification_version: 4
|
97
112
|
summary: Validations for Active Storage
|