active_model_validates_intersection_of 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +5 -1
- 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 +88 -4
- data/spec/lib/validates_intersection_of_alias_spec.rb +5 -4
- data/spec/lib/validator_spec.rb +5 -4
- data/spec/shared.rb +0 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d9a880302c1fb042f2b7cfd0d45d13886d5c6a6
|
4
|
+
data.tar.gz: 8bebd7296a28944ff88fe31bbae47f42d5df6f49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d508965100a884ba3ba35eadeda759d7e53f2d8fe360d467ec663836a297158917b76537ea9bfb09ebb159da219dc68c801f6684ad35a7bd89388540be486258
|
7
|
+
data.tar.gz: 4899760345566f7f3c71cd2012d0c8cda4603ad8086ae29eb551f33863b70fd35dbb7f15b6a8f1b0d2cdead25317d9998c5b30ae5664a84132cde1ac6fa3daec
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -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
|
@@ -1,12 +1,36 @@
|
|
1
1
|
module ActiveModelValidatesIntersectionOf
|
2
2
|
class Validator < ActiveModel::EachValidator
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
6
11
|
|
7
|
-
|
12
|
+
def validate_each(record, attribute, value)
|
13
|
+
raise ArgumentError, "value must be an array" unless value.is_a?(Array)
|
14
|
+
|
15
|
+
if (value - members(record)).any?
|
8
16
|
record.errors.add(attribute, :inclusion, options.except(:in, :within).merge!(value: value))
|
9
17
|
end
|
10
18
|
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
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
|
29
|
+
end
|
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
|
|
@@ -61,5 +62,88 @@ RSpec.describe IntersectionValidator do
|
|
61
62
|
end
|
62
63
|
end
|
63
64
|
end
|
65
|
+
|
66
|
+
context "validate with lambda" do
|
67
|
+
class SomeList
|
68
|
+
VALID_ARRAY = ["z", "x", 5 , 6]
|
69
|
+
include ActiveModel::Validations
|
70
|
+
attr_accessor :list
|
71
|
+
validates_intersection_of :list, in: lambda {|a| VALID_ARRAY }, message: "not valid"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "is valid" do
|
75
|
+
list = SomeList.new
|
76
|
+
list.list = ["z"]
|
77
|
+
expect(list.valid?).to eq true
|
78
|
+
end
|
79
|
+
|
80
|
+
it "is invalid" do
|
81
|
+
list = SomeList.new
|
82
|
+
list.list = ["G"]
|
83
|
+
expect(list.valid?).to eq false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "validate with proc" do
|
88
|
+
class ProcList
|
89
|
+
VALID_ARRAY = ["z", "q", 5 , 3]
|
90
|
+
include ActiveModel::Validations
|
91
|
+
attr_accessor :list
|
92
|
+
validates_intersection_of :list, in: proc { VALID_ARRAY }, message: "not valid"
|
93
|
+
end
|
94
|
+
|
95
|
+
it "is valid" do
|
96
|
+
list = ProcList.new
|
97
|
+
list.list = ["q"]
|
98
|
+
expect(list.valid?).to eq true
|
99
|
+
end
|
100
|
+
|
101
|
+
it "is invalid" do
|
102
|
+
list = ProcList.new
|
103
|
+
list.list = [10]
|
104
|
+
expect(list.valid?).to eq false
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "validate with symbol" do
|
109
|
+
class SymList
|
110
|
+
VALID_ARRAY = ["z", "d", 5 , 6]
|
111
|
+
include ActiveModel::Validations
|
112
|
+
attr_accessor :list
|
113
|
+
validates_intersection_of :list, in: :valid_options, message: "not valid"
|
114
|
+
|
115
|
+
def valid_options
|
116
|
+
VALID_ARRAY
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it "is valid" do
|
121
|
+
list = SymList.new
|
122
|
+
list.list = ["d"]
|
123
|
+
expect(list.valid?).to eq true
|
124
|
+
end
|
125
|
+
|
126
|
+
it "is invalid" do
|
127
|
+
list = SymList.new
|
128
|
+
list.list = [8]
|
129
|
+
expect(list.valid?).to eq false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "validate with value as string" do
|
134
|
+
class SomeInvalidList
|
135
|
+
include ActiveModel::Validations
|
136
|
+
attr_accessor :list
|
137
|
+
validates_intersection_of :list, in: proc { ['test'] }, message: "not valid"
|
138
|
+
end
|
139
|
+
|
140
|
+
it "causes an exception" do
|
141
|
+
list = SomeInvalidList.new
|
142
|
+
list.list = "d"
|
143
|
+
expect {
|
144
|
+
list.valid?
|
145
|
+
}.to raise_error(ArgumentError, "value must be an array")
|
146
|
+
end
|
147
|
+
end
|
64
148
|
end
|
65
149
|
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
|
|
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
|
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: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Biriba
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|