be_valid 0.1.0 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/be_valid.gemspec +2 -1
- data/lib/be_valid.rb +2 -2
- data/lib/be_valid/version.rb +1 -1
- data/lib/validators/must_be_validator.rb +92 -80
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 321b08decb577f0a5e7ceded71bf6c05829cd877a28f2b377f606e2f56cf6e61
|
4
|
+
data.tar.gz: c68cb42238a0480b4c2768b159fe7dca5bb690004f43027df728bdeca71b0f23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f085944be0b2681549ef91f534d2fb3a888d95be819f5bbd2e7143ac8d03817322229d5bbd1f8b46705dfe11c447006049f10c60fd301b7b4a6fa76f64c62e1e
|
7
|
+
data.tar.gz: c9f38ad043a959f56386d785b747197e0e6936867532a315348b47227858615040e8a0c3c20c7dca654f8292c815257f868d0eea9fd829072330219169f18f0b
|
data/Gemfile.lock
CHANGED
data/be_valid.gemspec
CHANGED
@@ -7,7 +7,6 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "be_valid"
|
8
8
|
spec.version = BeValid::VERSION
|
9
9
|
spec.authors = ["John Stewart (johnsinco)"]
|
10
|
-
spec.email = ["johnstewartco@outlook.com"]
|
11
10
|
|
12
11
|
spec.summary = %q{Custom Ruby on Rails Validation library supporting 'conditional' validation}
|
13
12
|
spec.description = %q{Provides more advanced and flexible 'if then then that' style conditional validation. Validate fields based on the values of other fields in your model.}
|
@@ -24,6 +23,8 @@ Gem::Specification.new do |spec|
|
|
24
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
24
|
spec.require_paths = ["lib"]
|
26
25
|
|
26
|
+
spec.required_ruby_version = '>= 2.3'
|
27
|
+
|
27
28
|
spec.add_development_dependency "bundler", "~> 1.17"
|
28
29
|
spec.add_development_dependency "rake", "~> 10.0"
|
29
30
|
spec.add_development_dependency "minitest", "~> 5.0"
|
data/lib/be_valid.rb
CHANGED
data/lib/be_valid/version.rb
CHANGED
@@ -1,97 +1,109 @@
|
|
1
|
-
|
2
|
-
MATCHERS = {
|
3
|
-
equal_to: :==,
|
4
|
-
greater_than: :>,
|
5
|
-
greater_or_equal_to: :>=,
|
6
|
-
less_than: :<,
|
7
|
-
less_or_equal_to: :<=,
|
8
|
-
not_equal_to: :!=,
|
9
|
-
matching: :=~,
|
10
|
-
# one_of: :in?
|
11
|
-
}.freeze
|
1
|
+
require 'active_record'
|
12
2
|
|
13
|
-
|
14
|
-
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class MustBeValidator < EachValidator
|
6
|
+
MATCHERS = {
|
7
|
+
equal_to: :==,
|
8
|
+
greater_than: :>,
|
9
|
+
greater_or_equal_to: :>=,
|
10
|
+
less_than: :<,
|
11
|
+
less_or_equal_to: :<=,
|
12
|
+
not_equal_to: :!=,
|
13
|
+
matching: :=~,
|
14
|
+
# one_of: :in?
|
15
|
+
}.freeze
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
MESSAGE_PREFIX = "must be"
|
18
|
+
ERRORS_METHOD = :errors
|
18
19
|
|
19
|
-
|
20
|
+
def validate_each(record, attribute, value)
|
21
|
+
raise "must_be_validator requires at least one comparison operator for attribute." unless options.slice(MATCHERS.keys + [:blank, :present])
|
20
22
|
|
21
|
-
|
23
|
+
original_value = record.read_attribute_before_type_cast( attribute )
|
22
24
|
|
23
|
-
|
24
|
-
message << " blank" if options[:blank]
|
25
|
+
message = self.class::MESSAGE_PREFIX.dup
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
return if options[:blank] && value.blank?
|
28
|
+
message << " blank" if options[:blank]
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
return if options[:present] && value.present?
|
31
|
+
message << " present" if options[:present]
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
return if options[:one_of] && Array(options[:one_of]).flatten.include?(value)
|
34
|
+
message = ": '#{value}' is not a valid value" if options[:one_of]
|
34
35
|
|
35
|
-
|
36
|
-
|
36
|
+
return if options[:not_any_of] && !(options[:not_any_of].include?(value))
|
37
|
+
message = ": '#{value}' is not a valid value" if options[:not_any_of]
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
before_resp = DateValidator.new(options.merge(attributes: attributes)).validate_before_option(record, attribute, value, original_value)
|
41
|
-
return if before_resp == true
|
42
|
-
message = before_resp
|
43
|
-
end
|
44
|
-
if options[:after]
|
45
|
-
after_resp = DateValidator.new(options.merge(attributes: attributes)).validate_after_option(record, attribute, value, original_value)
|
46
|
-
return if after_resp == true
|
47
|
-
message = after_resp
|
48
|
-
end
|
39
|
+
return if options[:only_from] && (options[:only_from] & Array(value) == Array(value))
|
40
|
+
message = ": #{Array(value).join(",").gsub('"', '\'')} is not a valid value" if options[:only_from]
|
49
41
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
42
|
+
# handle before and after date comparisons using date validator
|
43
|
+
if options[:before]
|
44
|
+
before_resp = DateValidator.new(options.merge(attributes: attributes)).validate_before_option(record, attribute, value, original_value)
|
45
|
+
return if before_resp == true
|
46
|
+
message = before_resp
|
47
|
+
end
|
48
|
+
if options[:after]
|
49
|
+
after_resp = DateValidator.new(options.merge(attributes: attributes)).validate_after_option(record, attribute, value, original_value)
|
50
|
+
return if after_resp == true
|
51
|
+
message = after_resp
|
52
|
+
end
|
58
53
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
conditions.each do |field, values|
|
67
|
-
case values
|
68
|
-
when Regexp
|
69
|
-
return if !values.match?(record[field])
|
70
|
-
condition_errors << "#{field} = #{record[field]}"
|
71
|
-
when Array
|
72
|
-
return if !values.include?(record[field])
|
73
|
-
condition_errors << "#{field} = #{record[field]}"
|
74
|
-
when Symbol
|
75
|
-
return if !record.send(field)&.send(values)
|
76
|
-
condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
|
77
|
-
else
|
78
|
-
return if values != record[field]
|
79
|
-
condition_errors << "#{field} = #{record[field]}"
|
54
|
+
options.slice(*MATCHERS.keys).each do |key, operand|
|
55
|
+
operand_msg = operand.is_a?(Regexp) ? operand.inspect : operand.to_s
|
56
|
+
operand = record.send(operand) if operand.is_a? Symbol
|
57
|
+
return if operand.nil?
|
58
|
+
return if value&.send(MATCHERS[key], operand)
|
59
|
+
return if key == :not_equal_to && value != operand
|
60
|
+
message << " #{key.to_s.humanize(capitalize: false)} #{operand_msg}"
|
80
61
|
end
|
81
|
-
end
|
82
|
-
message << condition_errors.join(' and ')
|
83
|
-
end
|
84
62
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
63
|
+
if options[:when].present?
|
64
|
+
# check if conditions to validate satisfied
|
65
|
+
conditions = options[:when]
|
66
|
+
raise "Invalid :when option provided to must_be, must be a Hash or method" unless (conditions.is_a? Hash || conditions.is_a?(Method))
|
67
|
+
# add error message with predicate info
|
68
|
+
message << " when "
|
69
|
+
condition_errors = []
|
70
|
+
conditions.each do |field, values|
|
71
|
+
case values
|
72
|
+
when Regexp
|
73
|
+
return if !values.match?(record[field])
|
74
|
+
condition_errors << "#{field} = #{record[field]}"
|
75
|
+
when Array
|
76
|
+
return if !values.flatten.include?(record[field])
|
77
|
+
condition_errors << "#{field} = #{record[field]}"
|
78
|
+
when :blank
|
79
|
+
return unless record.send(field).blank?
|
80
|
+
condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
|
81
|
+
when :present
|
82
|
+
return unless record.send(field).present?
|
83
|
+
condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
|
84
|
+
when Symbol
|
85
|
+
return if !record.send(field)&.send(values)
|
86
|
+
condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
|
87
|
+
else
|
88
|
+
return if values != record[field]
|
89
|
+
condition_errors << "#{field} = #{record[field]}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
message << condition_errors.join(' and ')
|
93
|
+
end
|
94
|
+
|
95
|
+
if options[:one_of] && options.fetch(:show_values, true)
|
96
|
+
message << ". Valid values: #{options[:one_of].join(', ')}"
|
97
|
+
end
|
98
|
+
if options[:only_from] && options.fetch(:show_values, true)
|
99
|
+
message << ". Valid values: #{options[:only_from].join(', ')}"
|
100
|
+
end
|
101
|
+
if options[:not_any_of] && options.fetch(:show_values, true)
|
102
|
+
message << ". Invalid values: #{options[:not_any_of].join(', ')}"
|
103
|
+
end
|
94
104
|
|
95
|
-
|
105
|
+
record.send(self.class::ERRORS_METHOD).add(attribute, (options[:message] || "#{message}."), rule_name: options[:rule_name])
|
106
|
+
end
|
107
|
+
end
|
96
108
|
end
|
97
109
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: be_valid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Stewart (johnsinco)
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04-
|
11
|
+
date: 2021-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,7 +83,6 @@ dependencies:
|
|
83
83
|
description: Provides more advanced and flexible 'if then then that' style conditional
|
84
84
|
validation. Validate fields based on the values of other fields in your model.
|
85
85
|
email:
|
86
|
-
- johnstewartco@outlook.com
|
87
86
|
executables: []
|
88
87
|
extensions: []
|
89
88
|
extra_rdoc_files: []
|
@@ -114,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
113
|
requirements:
|
115
114
|
- - ">="
|
116
115
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
116
|
+
version: '2.3'
|
118
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
118
|
requirements:
|
120
119
|
- - ">="
|