nested_validator 1.0.2 → 1.0.3
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 +8 -8
- data/Gemfile.lock +2 -1
- data/README.md +1 -1
- data/lib/nested_validator/validate_nested_matcher.rb +94 -97
- data/lib/nested_validator/version.rb +1 -1
- data/lib/nested_validator.rb +1 -1
- data/nested_validator.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTMyMTRjODIyZWYxOTZmZDIyNTc4MTlmNWYyNmQyZjY1MTQ2MTc1Mw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZGVhZGFmOTRjZmM0ZmVhYWNkNGM3NjVjNDAyNWRiMGI1ZmEyMDBmOA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NWY0Y2FkZmE0OGU4MTYyYjBlYzM4N2EwNWU1ZDEzMDQzYjUzZGE0NzMxN2Nj
|
10
|
+
YzliMmJkOWQ3MTY2YjlmNjYzM2I1OWQ4ZTBhMzM2NTRlYzMyNTE5M2Q4NDc1
|
11
|
+
ZjdjNTI3N2YwOWJmMDg2MjBiMWZmZTBlNWY1NWQ5NGY0ZTc0OWU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzE4N2RhYTkxNWQ3MjFiYzg4ZGU1YzM1YmM4MDBiZmQ0ZjA0NWZkMjVjYjNj
|
14
|
+
MTFlZTA5MGQyMGZmY2VhZWM2OGRkZTYzM2MzNzNhODg3ZGRiNDcyY2UwODVj
|
15
|
+
Y2E2N2RmNDk5NjIyNzZjYmViZjU2NWM3ZTY5MDE2MGM1NGM4ZTM=
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -153,7 +153,7 @@ You can use the ```validates_nested``` method:
|
|
153
153
|
class ParentMultiple < ParentBase
|
154
154
|
attr_accessor :child2
|
155
155
|
|
156
|
-
|
156
|
+
validate_nested :child, :child2, only: :attribute1
|
157
157
|
|
158
158
|
def initialize
|
159
159
|
self.child = Child.new
|
@@ -11,129 +11,126 @@
|
|
11
11
|
# it { should validate_nested(:child).only(:attribute1, :attribute2) }
|
12
12
|
# it { should validate_nested(:child).except(:attribute1) }
|
13
13
|
# end
|
14
|
-
|
14
|
+
RSpec::Matchers.define :validate_nested do |child_name|
|
15
15
|
|
16
|
-
|
16
|
+
attr_accessor :child_name, :prefix, :only_keys, :except_keys # inputs
|
17
|
+
attr_accessor :parent, :actual_keys
|
17
18
|
|
18
|
-
|
19
|
-
attr_accessor :parent, :actual_keys
|
19
|
+
TEST_KEY ||= :__test_key__
|
20
20
|
|
21
|
-
|
21
|
+
match do |parent|
|
22
|
+
self.prefix ||= ''
|
23
|
+
self.only_keys ||= []
|
24
|
+
self.except_keys ||= []
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
self.only_keys ||= []
|
26
|
-
self.except_keys ||= []
|
26
|
+
self.child_name = child_name
|
27
|
+
self.parent = parent
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
return false unless parent.respond_to? child_name
|
30
|
+
self.actual_keys = (error_keys_when_child_validity_is(false) - error_keys_when_child_validity_is(true))
|
31
|
+
return false if invalid_child_keys.present?
|
30
32
|
|
31
|
-
return false unless parent.respond_to? child_name
|
32
|
-
self.actual_keys = (error_keys_when_child_validity_is(false) - error_keys_when_child_validity_is(true))
|
33
|
-
return false if invalid_child_keys.present?
|
34
33
|
|
34
|
+
actual_keys == expected_keys
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
chain(:with_prefix) { |prefix| self.prefix = prefix }
|
40
|
-
chain(:only) { |*only| self.only_keys = only }
|
41
|
-
chain(:except) { |*except| self.except_keys = except }
|
37
|
+
chain(:with_prefix) { |prefix| self.prefix = prefix }
|
38
|
+
chain(:only) { |*only| self.only_keys = only }
|
39
|
+
chain(:except) { |*except| self.except_keys = except }
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
def child
|
42
|
+
parent.send child_name
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
def error_keys_when_child_validity_is(valid)
|
46
|
+
child_error_keys = combine TEST_KEY, only_keys, except_keys
|
47
|
+
child_errors = child_error_keys.inject({}){|result, key| result[key] = ['error message'];result }
|
50
48
|
|
51
|
-
|
52
|
-
|
49
|
+
allow(child).to receive(:valid?) { valid }
|
50
|
+
allow(child).to receive(:errors) { valid ? [] : child_errors }
|
53
51
|
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
parent.valid?
|
53
|
+
parent.errors.keys
|
54
|
+
end
|
57
55
|
|
58
|
-
|
59
|
-
|
60
|
-
|
56
|
+
def expected_keys
|
57
|
+
expected_child_keys.map{|key| :"#{expected_prefix} #{key}"}
|
58
|
+
end
|
61
59
|
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
def expected_prefix
|
61
|
+
prefix.present? ? prefix : child_name
|
62
|
+
end
|
65
63
|
|
66
|
-
|
67
|
-
|
68
|
-
|
64
|
+
def actual_prefix
|
65
|
+
:"#{actual_keys.first.to_s.split.first}"
|
66
|
+
end
|
69
67
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
68
|
+
def expected_child_keys
|
69
|
+
expected_keys = only_keys.present? ? only_keys : [TEST_KEY]
|
70
|
+
unique_except_keys = except_keys - only_keys
|
71
|
+
combine expected_keys - unique_except_keys
|
72
|
+
end
|
75
73
|
|
76
|
-
|
77
|
-
|
78
|
-
|
74
|
+
def actual_child_keys
|
75
|
+
actual_keys.map{|key| key.to_s.sub(/^.*\s+/, '').to_sym }
|
76
|
+
end
|
79
77
|
|
80
|
-
|
81
|
-
|
82
|
-
|
78
|
+
def invalid_child_keys
|
79
|
+
(only_keys + except_keys).reject{|key| child.respond_to? key}
|
80
|
+
end
|
83
81
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
82
|
+
description do
|
83
|
+
message = "validate nested #{show child_name}"
|
84
|
+
message << " with only: #{show only_keys}" if only_keys.present?
|
85
|
+
message << " except: #{show except_keys}" if except_keys.present?
|
86
|
+
message << " with prefix #{show prefix}" if prefix.present?
|
87
|
+
message
|
88
|
+
end
|
91
89
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
else
|
104
|
-
"parent has a prefix of #{show actual_prefix}. Are you missing '.with_prefix(#{show actual_prefix})'?"
|
105
|
-
end
|
90
|
+
failure_message do
|
91
|
+
case
|
92
|
+
when common_failure_message
|
93
|
+
common_failure_message
|
94
|
+
when (missing_child_keys = expected_child_keys - actual_child_keys - invalid_child_keys - [TEST_KEY]).present?
|
95
|
+
"#{parent} doesn't nest validations for: #{show missing_child_keys}"
|
96
|
+
when actual_keys.empty?
|
97
|
+
"parent doesn't nest validations for #{show child_name}"
|
98
|
+
when actual_prefix != expected_prefix
|
99
|
+
if prefix.present?
|
100
|
+
"parent uses a prefix of #{show actual_prefix} rather than #{show expected_prefix}"
|
106
101
|
else
|
107
|
-
"parent
|
108
|
-
|
102
|
+
"parent has a prefix of #{show actual_prefix}. Are you missing '.with_prefix(#{show actual_prefix})'?"
|
103
|
+
end
|
104
|
+
else
|
105
|
+
"parent does nest validations for: #{show except_keys & actual_child_keys}"
|
109
106
|
end
|
107
|
+
end
|
110
108
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
end
|
109
|
+
failure_message_when_negated do
|
110
|
+
case
|
111
|
+
when common_failure_message
|
112
|
+
common_failure_message
|
113
|
+
when (extras = only_keys & actual_child_keys).present?
|
114
|
+
"#{parent} does nest #{show child_name} validations for: #{show extras}"
|
115
|
+
when except_keys.present?
|
116
|
+
"#{parent} doesn't nest #{show child_name} validations for: #{show except_keys - actual_child_keys}"
|
117
|
+
when prefix.present?
|
118
|
+
"#{parent} does nest validations for: #{show child_name} with a prefix of #{show prefix}"
|
119
|
+
else
|
120
|
+
"#{parent} does nest validations for: #{show child_name}"
|
124
121
|
end
|
122
|
+
end
|
125
123
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
124
|
+
def common_failure_message
|
125
|
+
return "#{parent} doesn't respond to #{show child_name}" unless parent.respond_to?(child_name)
|
126
|
+
"#{child_name} doesn't respond to #{show invalid_child_keys}" if invalid_child_keys.present?
|
127
|
+
end
|
130
128
|
|
131
|
-
|
132
|
-
|
133
|
-
|
129
|
+
def show(value)
|
130
|
+
Array.wrap(value).map{|key| key.is_a?(Symbol) ? ":#{key}" : key.to_s}.join(', ')
|
131
|
+
end
|
134
132
|
|
135
|
-
|
136
|
-
|
137
|
-
end
|
133
|
+
def combine(*keys)
|
134
|
+
keys.flatten.compact
|
138
135
|
end
|
139
136
|
end
|
data/lib/nested_validator.rb
CHANGED
data/nested_validator.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Declan Whelan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|