active_model_validates_intersection_of 1.1.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/.travis.yml +13 -2
- data/CHANGELOG.md +12 -0
- data/README.md +6 -2
- data/active_model_validates_intersection_of.gemspec +4 -3
- data/lib/active_model_validates_intersection_of/validator.rb +29 -5
- data/lib/active_model_validates_intersection_of/version.rb +1 -1
- data/spec/lib/intersection_validator_spec.rb +120 -4
- data/spec/lib/validates_intersection_of_alias_spec.rb +21 -4
- data/spec/lib/validator_spec.rb +5 -4
- data/spec/shared.rb +0 -6
- data/spec/spec_helper.rb +6 -2
- metadata +23 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 380a36633d08fb17e45a86ad9d94c329620487717be29f4ee3cfd4573a2ba1de
|
4
|
+
data.tar.gz: 00f2ec575910e8f8c7b8ab563a07b07f8c60e11740ecf10253d45b3f22730ac1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf99fdd4024988e87e6b7fd079f17347750aa80ce75cd7d4c9f71857e373abf99958672adb2af49886beddb9221c3f947060cca21bbacddaec9f0488e3e2a7f8
|
7
|
+
data.tar.gz: be4f4ed87278de54137fa89661ede7a931160fc50996d7d7e36f90efaef2c6b31ff198929880ab36812e19893834eb67ab40eb923f1300465c09c6b661222f7c
|
data/.travis.yml
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
|
-
- 2.
|
5
|
-
|
4
|
+
- 2.3.6
|
5
|
+
- 2.4.3
|
6
|
+
- 2.5.0
|
7
|
+
- 3.0.1
|
8
|
+
before_install: gem install bundler -v 2.2.16
|
9
|
+
before_script:
|
10
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
11
|
+
- chmod +x ./cc-test-reporter
|
12
|
+
- ./cc-test-reporter before-build
|
13
|
+
script:
|
14
|
+
- bundle exec rspec
|
15
|
+
after_script:
|
16
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
## v3.0.1 (2021-08-24)
|
2
|
+
|
3
|
+
- Fixing model validation when the value is nil (eg: list = `[nil]` model.valid? should be false). (From issue: #9)
|
4
|
+
|
5
|
+
## v3.0.0 (2021-04-13)
|
6
|
+
|
7
|
+
- Ruby 3 support.
|
8
|
+
|
9
|
+
## v1.2.0 (2017-12-20)
|
10
|
+
|
11
|
+
- Support for proc and lambda
|
12
|
+
|
1
13
|
## v1.1.0 (2017-06-06)
|
2
14
|
|
3
15
|
- Implement `validates :list, intersection: { in: ANOTHER_LIST }` see the README for more information
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Active Model Validates Intersection Of
|
2
2
|
|
3
|
-
 [ [](https://codeclimate.com/github/rafaelbiriba/active_model_validates_intersection_of/maintainability) [](https://codeclimate.com/github/rafaelbiriba/active_model_validates_intersection_of/test_coverage) [](https://coveralls.io/r/rafaelbiriba/active_model_validates_intersection_of?branch=master) [](https://travis-ci.org/rafaelbiriba/active_model_validates_intersection_of)
|
4
4
|
|
5
5
|
A custom validation for Active Model that check if an array is included in another one.
|
6
6
|
|
@@ -54,7 +54,7 @@ require "active_model_validates_intersection_of"
|
|
54
54
|
|
55
55
|
### Parameters
|
56
56
|
|
57
|
-
* `:in` - Parameter is required
|
57
|
+
* `:in` - Parameter is required. (Supports: Array, proc and lambda)
|
58
58
|
* `:within` - *Optional:* A synonym(alias) for `:in`
|
59
59
|
* `:message` - *Optional:* Specifies a custom error message
|
60
60
|
|
@@ -70,6 +70,10 @@ class User < ActiveRecord::Base
|
|
70
70
|
validates_intersection_of :permission, in: DEFAULT_PERMISSION, message: "invalid permission"
|
71
71
|
validates_intersection_of :permission, within: DEFAULT_PERMISSION, message: "invalid permission"
|
72
72
|
|
73
|
+
# proc and lambda support
|
74
|
+
validates_intersection_of :permission, in: proc { DEFAULT_PERMISSION }, message: "invalid permission"
|
75
|
+
validates_intersection_of :permission, in: lambda { |l| DEFAULT_PERMISSION }, message: "invalid permission"
|
76
|
+
|
73
77
|
# Using the validator explicit:
|
74
78
|
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in: DEFAULT_PERMISSION
|
75
79
|
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], within: DEFAULT_PERMISSION
|
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(spec)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "activemodel", ">=
|
22
|
-
spec.add_development_dependency "bundler", "~> 1
|
23
|
-
spec.add_development_dependency "rake", "~>
|
21
|
+
spec.add_dependency "activemodel", ">= 5.0.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 2.1"
|
23
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
25
|
spec.add_development_dependency "coveralls"
|
26
|
+
spec.add_development_dependency "simplecov"
|
26
27
|
end
|
@@ -1,12 +1,36 @@
|
|
1
1
|
module ActiveModelValidatesIntersectionOf
|
2
2
|
class Validator < ActiveModel::EachValidator
|
3
|
+
ERROR_MESSAGE = "An object with the method #include? or a proc, lambda or symbol is required, " \
|
4
|
+
"and must be supplied as the :in (or :within) option"
|
5
|
+
|
6
|
+
def check_validity!
|
7
|
+
unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym)
|
8
|
+
raise ArgumentError, ERROR_MESSAGE
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
3
12
|
def validate_each(record, attribute, value)
|
4
|
-
|
5
|
-
|
13
|
+
raise ArgumentError, "value must be an array" unless value.is_a?(Array)
|
14
|
+
|
15
|
+
if (value - members(record)).size > 0
|
16
|
+
record.errors.add(attribute, :inclusion, **options.except(:in, :within).merge!(value: value))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
6
21
|
|
7
|
-
|
8
|
-
|
22
|
+
def members(record)
|
23
|
+
members = if delimiter.respond_to?(:call)
|
24
|
+
delimiter.call(record)
|
25
|
+
elsif delimiter.respond_to?(:to_sym)
|
26
|
+
record.send(delimiter)
|
27
|
+
else
|
28
|
+
delimiter
|
9
29
|
end
|
10
30
|
end
|
31
|
+
|
32
|
+
def delimiter
|
33
|
+
@delimiter ||= options[:in] || options[:within]
|
34
|
+
end
|
11
35
|
end
|
12
|
-
end
|
36
|
+
end
|
@@ -5,14 +5,15 @@ RSpec.describe IntersectionValidator do
|
|
5
5
|
class BadSetupValidator
|
6
6
|
include ActiveModel::Validations
|
7
7
|
attr_accessor :list
|
8
|
-
validates :list, intersection: true
|
9
8
|
end
|
10
9
|
|
11
|
-
let(:badsetup) { BadSetupValidator.new }
|
12
|
-
|
13
10
|
context "setup" do
|
14
11
|
describe "with neither :in nor :within configuration" do
|
15
|
-
|
12
|
+
it "raises argument error" do
|
13
|
+
expect {
|
14
|
+
BadSetupValidator.class_eval("validates :list, intersection: true")
|
15
|
+
}.to raise_error(ArgumentError)
|
16
|
+
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
@@ -36,6 +37,22 @@ RSpec.describe IntersectionValidator do
|
|
36
37
|
describe "with an invalid list" do
|
37
38
|
it_behaves_like "invalid object"
|
38
39
|
end
|
40
|
+
|
41
|
+
context "when the model value is an array with nil value" do
|
42
|
+
class ListTest
|
43
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
44
|
+
include ActiveModel::Validations
|
45
|
+
attr_accessor :list
|
46
|
+
validates_intersection_of :list, within: VALID_ARRAY, message: "not valid"
|
47
|
+
end
|
48
|
+
|
49
|
+
subject { ListTest.new }
|
50
|
+
|
51
|
+
it "should return as invalid" do
|
52
|
+
subject.list = [nil]
|
53
|
+
expect(subject.valid?).to eq(false)
|
54
|
+
end
|
55
|
+
end
|
39
56
|
end
|
40
57
|
|
41
58
|
context "with :within option" do
|
@@ -60,6 +77,105 @@ RSpec.describe IntersectionValidator do
|
|
60
77
|
it_behaves_like "invalid object"
|
61
78
|
end
|
62
79
|
end
|
80
|
+
|
81
|
+
context "when the model value is an array with nil value" do
|
82
|
+
class ListTest
|
83
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
84
|
+
include ActiveModel::Validations
|
85
|
+
attr_accessor :list
|
86
|
+
validates_intersection_of :list, within: VALID_ARRAY, message: "not valid"
|
87
|
+
end
|
88
|
+
|
89
|
+
subject { ListTest.new }
|
90
|
+
|
91
|
+
it "should return as invalid" do
|
92
|
+
subject.list = [nil]
|
93
|
+
expect(subject.valid?).to eq(false)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "validate with lambda" do
|
99
|
+
class SomeList
|
100
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
101
|
+
include ActiveModel::Validations
|
102
|
+
attr_accessor :list
|
103
|
+
validates_intersection_of :list, in: lambda {|a| VALID_ARRAY }, message: "not valid"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "is valid" do
|
107
|
+
list = SomeList.new
|
108
|
+
list.list = ["z"]
|
109
|
+
expect(list.valid?).to eq true
|
110
|
+
end
|
111
|
+
|
112
|
+
it "is invalid" do
|
113
|
+
list = SomeList.new
|
114
|
+
list.list = ["G"]
|
115
|
+
expect(list.valid?).to eq false
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "validate with proc" do
|
120
|
+
class ProcList
|
121
|
+
VALID_ARRAY = ["z", "q", 5 , 3]
|
122
|
+
include ActiveModel::Validations
|
123
|
+
attr_accessor :list
|
124
|
+
validates_intersection_of :list, in: proc { VALID_ARRAY }, message: "not valid"
|
125
|
+
end
|
126
|
+
|
127
|
+
it "is valid" do
|
128
|
+
list = ProcList.new
|
129
|
+
list.list = ["q"]
|
130
|
+
expect(list.valid?).to eq true
|
131
|
+
end
|
132
|
+
|
133
|
+
it "is invalid" do
|
134
|
+
list = ProcList.new
|
135
|
+
list.list = [10]
|
136
|
+
expect(list.valid?).to eq false
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "validate with symbol" do
|
141
|
+
class SymList
|
142
|
+
VALID_ARRAY = ["z", "d", 5 , 6]
|
143
|
+
include ActiveModel::Validations
|
144
|
+
attr_accessor :list
|
145
|
+
validates_intersection_of :list, in: :valid_options, message: "not valid"
|
146
|
+
|
147
|
+
def valid_options
|
148
|
+
VALID_ARRAY
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it "is valid" do
|
153
|
+
list = SymList.new
|
154
|
+
list.list = ["d"]
|
155
|
+
expect(list.valid?).to eq true
|
156
|
+
end
|
157
|
+
|
158
|
+
it "is invalid" do
|
159
|
+
list = SymList.new
|
160
|
+
list.list = [8]
|
161
|
+
expect(list.valid?).to eq false
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "validate with value as string" do
|
166
|
+
class SomeInvalidList
|
167
|
+
include ActiveModel::Validations
|
168
|
+
attr_accessor :list
|
169
|
+
validates_intersection_of :list, in: proc { ['test'] }, message: "not valid"
|
170
|
+
end
|
171
|
+
|
172
|
+
it "causes an exception" do
|
173
|
+
list = SomeInvalidList.new
|
174
|
+
list.list = "d"
|
175
|
+
expect {
|
176
|
+
list.valid?
|
177
|
+
}.to raise_error(ArgumentError, "value must be an array")
|
178
|
+
end
|
63
179
|
end
|
64
180
|
end
|
65
181
|
end
|
@@ -5,14 +5,15 @@ RSpec.describe ActiveModel::Validations::HelperMethods do
|
|
5
5
|
class BadSetup
|
6
6
|
include ActiveModel::Validations
|
7
7
|
attr_accessor :list
|
8
|
-
validates_intersection_of :list
|
9
8
|
end
|
10
9
|
|
11
|
-
let(:badsetup) { BadSetup.new }
|
12
|
-
|
13
10
|
context "setup" do
|
14
11
|
describe "with neither :in nor :within configuration" do
|
15
|
-
|
12
|
+
it "raises argument error" do
|
13
|
+
expect {
|
14
|
+
BadSetup.class_eval("validates_intersection_of :list")
|
15
|
+
}.to raise_error(ArgumentError)
|
16
|
+
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
@@ -64,5 +65,21 @@ RSpec.describe ActiveModel::Validations::HelperMethods do
|
|
64
65
|
end
|
65
66
|
end
|
66
67
|
end
|
68
|
+
|
69
|
+
context "when the model value is an array with nil value" do
|
70
|
+
class ListTest
|
71
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
72
|
+
include ActiveModel::Validations
|
73
|
+
attr_accessor :list
|
74
|
+
validates_intersection_of :list, within: VALID_ARRAY, message: "not valid"
|
75
|
+
end
|
76
|
+
|
77
|
+
subject { ListTest.new }
|
78
|
+
|
79
|
+
it "should return as invalid" do
|
80
|
+
subject.list = [nil]
|
81
|
+
expect(subject.valid?).to eq(false)
|
82
|
+
end
|
83
|
+
end
|
67
84
|
end
|
68
85
|
end
|
data/spec/lib/validator_spec.rb
CHANGED
@@ -5,14 +5,15 @@ RSpec.describe ActiveModelValidatesIntersectionOf::Validator do
|
|
5
5
|
class BadSetupValidator
|
6
6
|
include ActiveModel::Validations
|
7
7
|
attr_accessor :list
|
8
|
-
validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list]
|
9
8
|
end
|
10
9
|
|
11
|
-
let(:badsetup) { BadSetupValidator.new }
|
12
|
-
|
13
10
|
context "setup" do
|
14
11
|
describe "with neither :in nor :within configuration" do
|
15
|
-
|
12
|
+
it "raises argument error" do
|
13
|
+
expect {
|
14
|
+
BadSetupValidator.class_eval("validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:list]")
|
15
|
+
}.to raise_error(ArgumentError)
|
16
|
+
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
data/spec/shared.rb
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
RSpec.shared_examples "invalid configuration" do
|
2
|
-
it "should raise an error" do
|
3
|
-
expect { badsetup.valid? }.to raise_error(ArgumentError)
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
1
|
RSpec.shared_examples "valid object" do
|
8
2
|
it "the subject should be valid" do
|
9
3
|
subject.list = valid_partial_array
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require "coveralls"
|
5
|
+
Coveralls.wear!
|
6
|
+
|
1
7
|
require "bundler/setup"
|
2
8
|
require "active_model"
|
3
9
|
require "active_model_validates_intersection_of"
|
4
|
-
require "coveralls"
|
5
|
-
Coveralls.wear!
|
6
10
|
|
7
11
|
RSpec.configure do |config|
|
8
12
|
# Enable flags like --only-failures and --next-failure
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_validates_intersection_of
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Biriba
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -16,42 +16,42 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 5.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1
|
33
|
+
version: '2.1'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1
|
40
|
+
version: '2.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '13.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '13.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: A custom validation for your Active Model that check if an array is included
|
84
98
|
in another one
|
85
99
|
email:
|
@@ -125,8 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
139
|
- !ruby/object:Gem::Version
|
126
140
|
version: '0'
|
127
141
|
requirements: []
|
128
|
-
|
129
|
-
rubygems_version: 2.6.12
|
142
|
+
rubygems_version: 3.0.3
|
130
143
|
signing_key:
|
131
144
|
specification_version: 4
|
132
145
|
summary: A custom validation for your Active Model that check if an array is included
|