activemodel-validators 1.2.0 → 3.0.1
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 +5 -5
- data/Readme.md +3 -3
- data/config/locales/en.yml +0 -1
- data/lib/activemodel-validators/less_or_greater_than_validator.rb +1 -1
- data/lib/activemodel-validators/sum_of_validator.rb +23 -19
- data/lib/activemodel-validators/version.rb +1 -1
- data/spec/sum_of_validator_spec.rb +61 -0
- metadata +4 -5
- data/lib/activemodel-validators/phone_validator.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 961ba5817f1275b85ef77cd36078274940be5a204d283215f22c282873b6fc25
|
4
|
+
data.tar.gz: a4ba63270f369593bb3bf295b2a1db59ee5073b62d20d6ab4baa22a9787ca17e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecf522be4a8849c91c4f99010452884ff966e7e459fe286d3f40103159a26e2d8de1c7c7af138fcba502c49fa55665d37a0a47f20136c04e6077bd7863adf35a
|
7
|
+
data.tar.gz: c9e13fba3fce4ce0bed10092d60add8dba4fa2dcd3deb83f0e0aaeb2ecf6e2bc164ff498b87ca2f5b267c75138bc7b5f8495f81bb6daded210e31e812241a583
|
data/Readme.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
ActiveModel Validators
|
2
2
|
=======================
|
3
3
|
|
4
|
-
A collection of Rails
|
4
|
+
A collection of Rails ActiveModel Validators
|
5
5
|
|
6
6
|
Example Usage
|
7
7
|
-------------
|
@@ -35,7 +35,6 @@ class Thing < ActiveRecord::Base
|
|
35
35
|
validates :end_date, date: {required: true}
|
36
36
|
validates :graduation_year, year: true
|
37
37
|
|
38
|
-
validates :phone, phone: true
|
39
38
|
validates :address_postal_code, postal_code: true
|
40
39
|
validates :address_state, address_state: true
|
41
40
|
end
|
@@ -59,7 +58,6 @@ Documentation
|
|
59
58
|
* [DateValidator](activemodel-validators/blob/master/lib/activemodel-validators/date_validator.rb)
|
60
59
|
* [YearValidator](activemodel-validators/blob/master/lib/activemodel-validators/year_validator.rb)
|
61
60
|
|
62
|
-
* [PhoneValidator](activemodel-validators/blob/master/lib/activemodel-validators/phone_validator.rb)
|
63
61
|
* [PostalCodeValidator](activemodel-validators/blob/master/lib/activemodel-validators/postal_code_validator.rb)
|
64
62
|
* [AddressStateValidator](activemodel-validators/blob/master/lib/activemodel-validators/address_state_validator.rb)
|
65
63
|
|
@@ -117,6 +115,8 @@ Other collections not worth taking a look at:
|
|
117
115
|
Specialized validators that stand on their own:
|
118
116
|
-----------------------------------------------
|
119
117
|
|
118
|
+
* https://github.com/daddyz/phonelib (`phone: true`)
|
119
|
+
* https://github.com/perfectline/validates_url
|
120
120
|
* https://github.com/balexand/email_validator
|
121
121
|
|
122
122
|
|
data/config/locales/en.yml
CHANGED
@@ -73,7 +73,7 @@ module ActiveModel
|
|
73
73
|
return if value.blank? or other_value.blank?
|
74
74
|
unless value.send(operator, other_value)
|
75
75
|
record.errors.add(attribute, message_key,
|
76
|
-
options.merge(
|
76
|
+
**options.merge(
|
77
77
|
value: value,
|
78
78
|
operator: operator,
|
79
79
|
operator_text: operator_text,
|
@@ -1,23 +1,27 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class SumOfValidator < ActiveModel::EachValidator
|
4
|
+
def check_validity!
|
5
|
+
unless options[:attr_names]
|
6
|
+
raise ArgumentError, "must supply :attr_names option"
|
7
|
+
end
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
10
|
+
def validate_each(record, attribute, value)
|
11
|
+
addends = options[:attr_names].map {|_| record[_] }
|
12
|
+
return if value.blank?
|
13
|
+
sum = addends.reject(&:blank?).sum
|
14
|
+
unless value == sum
|
15
|
+
record.errors.add(attribute, options[:message] || :sum_of,
|
16
|
+
options.merge(
|
17
|
+
attr_names: options[:attr_names].to_sentence,
|
18
|
+
addends: addends.join(' + '),
|
19
|
+
sum: sum,
|
20
|
+
value: value,
|
21
|
+
)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
ActiveModel::Validations::SumOfValidator
|
4
|
+
class ActiveModel::Validations::SumOfValidator
|
5
|
+
|
6
|
+
class TestModel < Struct.new(:group_1_count, :group_2_count, :total_count)
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
validates :total_count, sum_of: {attr_names: [:group_1_count, :group_2_count] }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe TestModel do
|
13
|
+
before { subject.group_1_count = 1 }
|
14
|
+
before { subject.group_2_count = 2 }
|
15
|
+
before { subject.total_count = 3 }
|
16
|
+
|
17
|
+
context 'when group_1_count + group_2_count == total_count' do
|
18
|
+
before { subject.valid? }
|
19
|
+
its('errors.messages') { should == {} }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when group_1_count + group_2_count != total_count' do
|
23
|
+
before { subject.group_2_count = 1 }
|
24
|
+
before { subject.valid? }
|
25
|
+
its('errors.messages') { should == {total_count: ["must be the sum of group_1_count and group_2_count (2) but was 3"]} }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when total_count is blank' do
|
29
|
+
before { subject.total_count = '' }
|
30
|
+
before { subject.valid? }
|
31
|
+
its('errors.messages') { should == {} }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when group_1_count and group_2_count are blank' do
|
35
|
+
before { subject.group_1_count = nil }
|
36
|
+
before { subject.group_2_count = '' }
|
37
|
+
before { subject.valid? }
|
38
|
+
its('errors.messages') { should == {total_count: ["must be the sum of group_1_count and group_2_count (0) but was 3"]} }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when group_2_count is blank' do
|
42
|
+
before { subject.group_2_count = '' }
|
43
|
+
before { subject.valid? }
|
44
|
+
its('errors.messages') { should == {total_count: ["must be the sum of group_1_count and group_2_count (1) but was 3"]} }
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when group_1_count is a string and group_2_count is a fixnum' do
|
48
|
+
before { subject.group_1_count = '1' }
|
49
|
+
before { subject.group_2_count = 2 }
|
50
|
+
it { expect { subject.valid? }.to raise_exception 'no implicit conversion of Fixnum into String' }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when group_1_count is a fixnum and group_2_count is a string' do
|
54
|
+
before { subject.group_1_count = 1 }
|
55
|
+
before { subject.group_2_count = '2' }
|
56
|
+
it { expect { subject.valid? }.to raise_exception "String can't be coerced into Fixnum" }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel-validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Rick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -80,7 +80,6 @@ files:
|
|
80
80
|
- lib/activemodel-validators/less_or_greater_than_validator.rb
|
81
81
|
- lib/activemodel-validators/less_than_validator.rb
|
82
82
|
- lib/activemodel-validators/multiple_of_validator.rb
|
83
|
-
- lib/activemodel-validators/phone_validator.rb
|
84
83
|
- lib/activemodel-validators/postal_code_validator.rb
|
85
84
|
- lib/activemodel-validators/rails.rb
|
86
85
|
- lib/activemodel-validators/restrict_to_validator.rb
|
@@ -91,6 +90,7 @@ files:
|
|
91
90
|
- spec/date_validator_spec.rb
|
92
91
|
- spec/less_or_greater_than_validator_spec.rb
|
93
92
|
- spec/spec_helper.rb
|
93
|
+
- spec/sum_of_validator_spec.rb
|
94
94
|
- spec/support/active_record.rb
|
95
95
|
- spec/support/models/response.rb
|
96
96
|
- spec/support/models/user.rb
|
@@ -113,8 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
- !ruby/object:Gem::Version
|
114
114
|
version: '0'
|
115
115
|
requirements: []
|
116
|
-
|
117
|
-
rubygems_version: 2.2.2
|
116
|
+
rubygems_version: 3.2.22
|
118
117
|
signing_key:
|
119
118
|
specification_version: 4
|
120
119
|
summary: Some reusable ActiveModel validations
|