be_valid 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -1
- data/Gemfile.lock +13 -12
- data/be_valid.gemspec +4 -2
- data/lib/be_valid/version.rb +1 -1
- data/lib/be_valid.rb +18 -6
- data/lib/validators/date_validator.rb +12 -11
- data/lib/validators/must_be_validator.rb +17 -8
- metadata +20 -7
- data/lib/be_valid/be_valid.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09ad2711308c18cd301eecd01ca9caf828c81909f9be6597c96a6353513ba4b4'
|
4
|
+
data.tar.gz: 79cff60db8a311481607f39f89191e84682f6d809e02710908f6aacb535b038f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35c8e9420efe9154f461af5857a9e2c8bd81ef1cacd525b1be4f176b39d51b4e7ebcabade66152c308429b8b653dd76aeb230f11c8a5e80bde24661a7a8e20b4
|
7
|
+
data.tar.gz: a4475627627e9d0338f7117254b9dc3785de4b8500b5e960900eb90069e27347792dc8f7e2c232c00a36966fd3388ea5bd05e30acfd1effd30f423bac8b57147
|
data/.github/workflows/ruby.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,31 +1,32 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
be_valid (0.1.
|
4
|
+
be_valid (0.1.8)
|
5
|
+
activemodel (~> 6.1)
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
8
9
|
specs:
|
9
|
-
activemodel (6.1.
|
10
|
-
activesupport (= 6.1.
|
11
|
-
activerecord (6.1.
|
12
|
-
activemodel (= 6.1.
|
13
|
-
activesupport (= 6.1.
|
14
|
-
activesupport (6.1.
|
10
|
+
activemodel (6.1.4.1)
|
11
|
+
activesupport (= 6.1.4.1)
|
12
|
+
activerecord (6.1.4.1)
|
13
|
+
activemodel (= 6.1.4.1)
|
14
|
+
activesupport (= 6.1.4.1)
|
15
|
+
activesupport (6.1.4.1)
|
15
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
17
|
i18n (>= 1.6, < 2)
|
17
18
|
minitest (>= 5.1)
|
18
19
|
tzinfo (~> 2.0)
|
19
20
|
zeitwerk (~> 2.3)
|
20
|
-
concurrent-ruby (1.1.
|
21
|
+
concurrent-ruby (1.1.9)
|
21
22
|
i18n (1.8.10)
|
22
23
|
concurrent-ruby (~> 1.0)
|
23
24
|
minitest (5.14.4)
|
24
|
-
rake (
|
25
|
+
rake (13.0.6)
|
25
26
|
sqlite3 (1.4.2)
|
26
27
|
tzinfo (2.0.4)
|
27
28
|
concurrent-ruby (~> 1.0)
|
28
|
-
zeitwerk (2.
|
29
|
+
zeitwerk (2.5.1)
|
29
30
|
|
30
31
|
PLATFORMS
|
31
32
|
ruby
|
@@ -35,8 +36,8 @@ DEPENDENCIES
|
|
35
36
|
be_valid!
|
36
37
|
bundler (~> 2)
|
37
38
|
minitest (~> 5.0)
|
38
|
-
rake (~>
|
39
|
+
rake (~> 13.0)
|
39
40
|
sqlite3 (~> 1.4)
|
40
41
|
|
41
42
|
BUNDLED WITH
|
42
|
-
2.2.
|
43
|
+
2.2.22
|
data/be_valid.gemspec
CHANGED
@@ -23,10 +23,12 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
|
26
|
-
spec.required_ruby_version = '>=
|
26
|
+
spec.required_ruby_version = '>= 3.0'
|
27
|
+
|
28
|
+
spec.add_dependency "activemodel", "~> 6.1"
|
27
29
|
|
28
30
|
spec.add_development_dependency "bundler", "~> 2"
|
29
|
-
spec.add_development_dependency "rake", "~>
|
31
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
30
32
|
spec.add_development_dependency "minitest", "~> 5.0"
|
31
33
|
spec.add_development_dependency "activerecord", "~> 6"
|
32
34
|
spec.add_development_dependency "sqlite3", "~> 1.4"
|
data/lib/be_valid/version.rb
CHANGED
data/lib/be_valid.rb
CHANGED
@@ -1,9 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
begin
|
1
|
+
module BeValid
|
4
2
|
require 'validators/date_validator'
|
5
3
|
require 'validators/must_be_validator'
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
|
5
|
+
def self.config
|
6
|
+
@@config ||= Configuration.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.configure
|
10
|
+
self.config ||= Configuration.new
|
11
|
+
yield(config)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Configuration
|
15
|
+
attr_accessor :rules
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@rules = {}
|
19
|
+
end
|
20
|
+
end
|
9
21
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
1
3
|
class DateValidator < ActiveModel::EachValidator
|
2
4
|
|
3
5
|
# https://stackoverflow.com/questions/28538080/rails-validate-dates-in-model-before-type-cast
|
@@ -6,23 +8,24 @@ class DateValidator < ActiveModel::EachValidator
|
|
6
8
|
|
7
9
|
@error_hash = options[:error_level].present? ? record.send(options[:error_level]) : record.errors
|
8
10
|
|
9
|
-
original_value =
|
11
|
+
original_value = value
|
12
|
+
original_value = record.read_attribute_before_type_cast( attribute ) rescue nil # only in rails
|
10
13
|
# dont display date format error unless date could not be parsed
|
11
14
|
if value.nil?
|
12
15
|
# if blank date was given it's still not a format issue, still show message if desired
|
13
16
|
return if original_value.blank? && options[:allow_blank]
|
14
17
|
|
15
18
|
# display helpful date format validation message with original value
|
16
|
-
message = options[:time] ? ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format." : ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD."
|
17
|
-
@error_hash.add(attribute, (options[:message] || message)
|
19
|
+
message = options[:time] ? ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format." : ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD format."
|
20
|
+
@error_hash.add(attribute, (options[:message] || message))
|
18
21
|
else
|
19
22
|
# handle validation options on valid date instances
|
20
23
|
return unless value.respond_to?(:strftime)
|
21
24
|
if(options[:after] && (after_message = validate_after_option(record, attribute, value, original_value)) != true)
|
22
|
-
@error_hash.add(attribute, (options[:after_message] || "#{after_message}.")
|
25
|
+
@error_hash.add(attribute, (options[:after_message] || "#{after_message}."))
|
23
26
|
end
|
24
27
|
if(options[:before] && (before_message = validate_before_option(record, attribute, value, original_value)) != true)
|
25
|
-
@error_hash.add(attribute, (options[:before_message] || "#{before_message}.")
|
28
|
+
@error_hash.add(attribute, (options[:before_message] || "#{before_message}."))
|
26
29
|
end
|
27
30
|
end
|
28
31
|
end
|
@@ -78,13 +81,11 @@ class DateValidator < ActiveModel::EachValidator
|
|
78
81
|
record.__send__(option) if record.respond_to?(option)
|
79
82
|
end
|
80
83
|
return unless date.present?
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
value = value.to_date
|
84
|
+
if options[:time]
|
85
|
+
return [date.to_time, value.to_time]
|
86
|
+
else
|
87
|
+
return [date.to_date, value.to_date]
|
86
88
|
end
|
87
|
-
[date, value]
|
88
89
|
end
|
89
90
|
|
90
91
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'active_model'
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
module Validations
|
@@ -22,7 +22,8 @@ module ActiveModel
|
|
22
22
|
raise "Requires at least one comparison operator for attribute." unless (options.keys & REQD_OPTS).length > 0
|
23
23
|
return if options[:rule_name] && BeValid.config.rules&.fetch(options[:rule_name], {})[:disabled]
|
24
24
|
|
25
|
-
original_value =
|
25
|
+
original_value = value
|
26
|
+
original_value = record.read_attribute_before_type_cast( attribute ) rescue nil # only in rails
|
26
27
|
|
27
28
|
message = self.class::MESSAGE_PREFIX.dup
|
28
29
|
|
@@ -70,13 +71,14 @@ module ActiveModel
|
|
70
71
|
message << " when "
|
71
72
|
condition_errors = []
|
72
73
|
conditions.each do |field, values|
|
74
|
+
field_value = record.send(field)
|
73
75
|
case values
|
74
76
|
when Regexp
|
75
|
-
return if !values.match?(
|
76
|
-
condition_errors << "#{field} = #{
|
77
|
+
return if !values.match?(field_value)
|
78
|
+
condition_errors << "#{field} = #{field_value}"
|
77
79
|
when Array
|
78
|
-
return if !values.flatten.include?(
|
79
|
-
condition_errors << "#{field} = #{
|
80
|
+
return if !values.flatten.include?(field_value)
|
81
|
+
condition_errors << "#{field} = #{field_value}"
|
80
82
|
when :blank
|
81
83
|
return unless record.send(field).blank?
|
82
84
|
condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
|
@@ -87,8 +89,8 @@ module ActiveModel
|
|
87
89
|
return if !record.send(field)&.send(values)
|
88
90
|
condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
|
89
91
|
else
|
90
|
-
return if values !=
|
91
|
-
condition_errors << "#{field} = #{
|
92
|
+
return if values != field_value
|
93
|
+
condition_errors << "#{field} = #{field_value}"
|
92
94
|
end
|
93
95
|
end
|
94
96
|
message << condition_errors.join(' and ')
|
@@ -106,6 +108,13 @@ module ActiveModel
|
|
106
108
|
|
107
109
|
record.send(self.class::ERRORS_METHOD).add(attribute, (options[:message] || "#{message}."), rule_name: options[:rule_name])
|
108
110
|
end
|
111
|
+
module ClassMethods
|
112
|
+
def validates_must_be_valid(*attr_names)
|
113
|
+
validates_with MustBeValidator, _merge_attributes(attr_names)
|
114
|
+
end
|
115
|
+
|
116
|
+
alias_method :validates_must_be, :validates_must_be_valid
|
117
|
+
end
|
109
118
|
end
|
110
119
|
end
|
111
120
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
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.8
|
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-
|
11
|
+
date: 2021-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.1'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -30,14 +44,14 @@ dependencies:
|
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '13.0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '13.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: minitest
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -98,7 +112,6 @@ files:
|
|
98
112
|
- bin/console
|
99
113
|
- bin/setup
|
100
114
|
- lib/be_valid.rb
|
101
|
-
- lib/be_valid/be_valid.rb
|
102
115
|
- lib/be_valid/version.rb
|
103
116
|
- lib/validators/date_validator.rb
|
104
117
|
- lib/validators/must_be_validator.rb
|
@@ -114,14 +127,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
127
|
requirements:
|
115
128
|
- - ">="
|
116
129
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
130
|
+
version: '3.0'
|
118
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
132
|
requirements:
|
120
133
|
- - ">="
|
121
134
|
- !ruby/object:Gem::Version
|
122
135
|
version: '0'
|
123
136
|
requirements: []
|
124
|
-
rubygems_version: 3.
|
137
|
+
rubygems_version: 3.2.22
|
125
138
|
signing_key:
|
126
139
|
specification_version: 4
|
127
140
|
summary: Custom Ruby on Rails Validation library supporting 'conditional' validation
|
data/lib/be_valid/be_valid.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module BeValid
|
2
|
-
def self.config
|
3
|
-
@@config ||= Configuration.new
|
4
|
-
end
|
5
|
-
|
6
|
-
def self.configure
|
7
|
-
self.config ||= Configuration.new
|
8
|
-
yield(config)
|
9
|
-
end
|
10
|
-
|
11
|
-
class Configuration
|
12
|
-
attr_accessor :rules
|
13
|
-
|
14
|
-
def initialize
|
15
|
-
@rules = {}
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|