mutations 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/lib/mutations/hash_filter.rb +17 -17
- data/lib/mutations/input_filter.rb +1 -1
- data/lib/mutations/version.rb +1 -1
- data/spec/hash_filter_spec.rb +31 -1
- metadata +1 -1
data/CHANGELOG.md
CHANGED
@@ -101,29 +101,29 @@ module Mutations
|
|
101
101
|
|
102
102
|
data_element = data[key]
|
103
103
|
|
104
|
-
|
105
|
-
data.delete(key) if !is_required && data.has_key?(key) && filterer.discard_nils? && data_element.nil?
|
106
|
-
data.delete(key) if !is_required && data.has_key?(key) && filterer.discard_empty? && data_element == "" # BUG: this doesn't account for data_elem being " "
|
107
|
-
|
108
|
-
default_used = false
|
109
|
-
if !data.has_key?(key) && filterer.has_default?
|
110
|
-
data_element = filterer.default
|
111
|
-
default_used = true
|
112
|
-
end
|
113
|
-
|
114
|
-
if data.has_key?(key) || default_used
|
104
|
+
if data.has_key?(key)
|
115
105
|
sub_data, sub_error = filterer.filter(data_element)
|
116
106
|
|
117
107
|
if sub_error.nil?
|
118
108
|
filtered_data[key] = sub_data
|
119
109
|
elsif !is_required && filterer.discard_invalid?
|
120
110
|
data.delete(key)
|
111
|
+
elsif !is_required && sub_error == :empty && filterer.discard_empty?
|
112
|
+
data.delete(key)
|
113
|
+
elsif !is_required && sub_error == :nils && filterer.discard_nils?
|
114
|
+
data.delete(key)
|
121
115
|
else
|
122
116
|
sub_error = ErrorAtom.new(key, sub_error) if sub_error.is_a?(Symbol)
|
123
117
|
errors[key] = sub_error
|
124
118
|
end
|
125
|
-
|
126
|
-
|
119
|
+
end
|
120
|
+
|
121
|
+
if !data.has_key?(key)
|
122
|
+
if filterer.has_default?
|
123
|
+
filtered_data[key] = filterer.default
|
124
|
+
elsif is_required
|
125
|
+
errors[key] = ErrorAtom.new(key, :required)
|
126
|
+
end
|
127
127
|
end
|
128
128
|
end
|
129
129
|
end
|
@@ -134,15 +134,15 @@ module Mutations
|
|
134
134
|
filtered_keys.each do |key|
|
135
135
|
data_element = data[key]
|
136
136
|
|
137
|
-
# First, discard optional nils/empty params
|
138
|
-
next if data.has_key?(key) && wildcard_filterer.discard_nils? && data_element.nil?
|
139
|
-
next if data.has_key?(key) && wildcard_filterer.discard_empty? && data_element == ""
|
140
|
-
|
141
137
|
sub_data, sub_error = wildcard_filterer.filter(data_element)
|
142
138
|
if sub_error.nil?
|
143
139
|
filtered_data[key] = sub_data
|
144
140
|
elsif wildcard_filterer.discard_invalid?
|
145
141
|
data.delete(key)
|
142
|
+
elsif sub_error == :empty && wildcard_filterer.discard_empty?
|
143
|
+
data.delete(key)
|
144
|
+
elsif sub_error == :nils && wildcard_filterer.discard_nils?
|
145
|
+
data.delete(key)
|
146
146
|
else
|
147
147
|
sub_error = ErrorAtom.new(key, sub_error) if sub_error.is_a?(Symbol)
|
148
148
|
errors[key] = sub_error
|
data/lib/mutations/version.rb
CHANGED
data/spec/hash_filter_spec.rb
CHANGED
@@ -155,6 +155,36 @@ describe "Mutations::HashFilter" do
|
|
155
155
|
assert_equal ({"foo" => "bar"}), filtered
|
156
156
|
assert_equal nil, errors
|
157
157
|
end
|
158
|
+
|
159
|
+
it "bar is optional -- discards empty if it needs to be stripped" do
|
160
|
+
hf = Mutations::HashFilter.new do
|
161
|
+
required do
|
162
|
+
string :foo
|
163
|
+
end
|
164
|
+
optional do
|
165
|
+
string :bar, :discard_empty => true
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
filtered, errors = hf.filter(:foo => "bar", :bar => " ")
|
170
|
+
assert_equal ({"foo" => "bar"}), filtered
|
171
|
+
assert_equal nil, errors
|
172
|
+
end
|
173
|
+
|
174
|
+
it "bar is optional -- don't discard empty if it's spaces but stripping is off" do
|
175
|
+
hf = Mutations::HashFilter.new do
|
176
|
+
required do
|
177
|
+
string :foo
|
178
|
+
end
|
179
|
+
optional do
|
180
|
+
string :bar, :discard_empty => true, :strip => false
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
filtered, errors = hf.filter(:foo => "bar", :bar => " ")
|
185
|
+
assert_equal ({"foo" => "bar", "bar" => " "}), filtered
|
186
|
+
assert_equal nil, errors
|
187
|
+
end
|
158
188
|
|
159
189
|
it "bar is optional -- errors if discard_empty is false and value is blank" do
|
160
190
|
hf = Mutations::HashFilter.new do
|
@@ -180,7 +210,7 @@ describe "Mutations::HashFilter" do
|
|
180
210
|
end
|
181
211
|
end
|
182
212
|
|
183
|
-
filtered, errors = hf.filter(:foo => "bar", :bar => "")
|
213
|
+
filtered, errors = hf.filter(:foo => "bar", :bar => "", :baz => "\t")
|
184
214
|
assert_equal ({"foo" => "bar"}), filtered
|
185
215
|
assert_equal nil, errors
|
186
216
|
end
|