objective 0.2.2 → 0.3.0
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 +4 -4
- data/lib/objective.rb +0 -2
- data/lib/objective/errors/error_message_creator.rb +0 -1
- data/lib/objective/filter.rb +6 -25
- data/lib/objective/filters/any_filter.rb +0 -1
- data/lib/objective/filters/array_filter.rb +1 -7
- data/lib/objective/filters/boolean_filter.rb +0 -1
- data/lib/objective/filters/date_filter.rb +0 -1
- data/lib/objective/filters/decimal_filter.rb +0 -1
- data/lib/objective/filters/duck_filter.rb +0 -1
- data/lib/objective/filters/file_filter.rb +0 -1
- data/lib/objective/filters/float_filter.rb +0 -1
- data/lib/objective/filters/hash_filter.rb +2 -4
- data/lib/objective/filters/integer_filter.rb +0 -1
- data/lib/objective/filters/model_filter.rb +0 -1
- data/lib/objective/filters/root_filter.rb +2 -3
- data/lib/objective/filters/string_filter.rb +0 -1
- data/lib/objective/filters/time_filter.rb +0 -1
- data/lib/objective/unit.rb +0 -1
- data/test/default_test.rb +24 -10
- data/test/inheritance_test.rb +4 -4
- data/test/objective/errors/errors_test.rb +6 -25
- data/test/objective/filters/array_filter_test.rb +24 -14
- data/test/objective/filters/root_filter_test.rb +39 -54
- data/test/simple_unit.rb +1 -1
- data/test/unit_test.rb +12 -12
- metadata +2 -4
- data/lib/objective/discard.rb +0 -11
- data/lib/objective/none.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36542ad6a5572ba8c8832cf1b1df0383516bb855
|
4
|
+
data.tar.gz: 1ccb77a65a1d6feec34011ba5509862df2b3b430
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6af1516855837c90b9bd954e8724ad7d9b8e9d4b0cd470df6e99f135208c8058f40c1334fa2743bf4fa7cd97d410049e7952564ded26128c1184979163ac244c
|
7
|
+
data.tar.gz: 65f94c72d491fe8d1447b243422c9aaa3bc49a918435b02e84bdc8db0a7f4ea74a57e2ef33b124f193d1986550633a3423e2af554e21328da43ad7f726e3762a
|
data/lib/objective.rb
CHANGED
data/lib/objective/filter.rb
CHANGED
@@ -53,7 +53,6 @@ module Objective
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def feed(raw)
|
56
|
-
return feed_none if raw == Objective::NONE
|
57
56
|
return feed_nil if raw.nil?
|
58
57
|
|
59
58
|
coerced = options.strict == true ? raw : coerce(raw)
|
@@ -66,33 +65,16 @@ module Objective
|
|
66
65
|
feed_result(errors, raw, coerced)
|
67
66
|
end
|
68
67
|
|
69
|
-
def feed_none
|
70
|
-
case options.none
|
71
|
-
when Objective::ALLOW
|
72
|
-
return Objective::DISCARD
|
73
|
-
when Objective::DENY
|
74
|
-
coerced = Objective::NONE
|
75
|
-
errors = :required
|
76
|
-
when Objective::DISCARD
|
77
|
-
raise 'the none option cannot be discarded — did you mean to use allow instead?'
|
78
|
-
else
|
79
|
-
coerced = options.none
|
80
|
-
end
|
81
|
-
|
82
|
-
feed_result(errors, Objective::NONE, coerced)
|
83
|
-
end
|
84
|
-
|
85
68
|
def feed_nil
|
86
69
|
case options.nils
|
87
70
|
when Objective::ALLOW
|
88
|
-
coerced = nil
|
89
71
|
errors = nil
|
90
|
-
when Objective::DENY
|
91
72
|
coerced = nil
|
73
|
+
when Objective::DENY
|
92
74
|
errors = :nils
|
93
|
-
|
94
|
-
return Objective::DISCARD
|
75
|
+
coerced = nil
|
95
76
|
else
|
77
|
+
errors = nil
|
96
78
|
coerced = options.nils
|
97
79
|
end
|
98
80
|
|
@@ -101,11 +83,12 @@ module Objective
|
|
101
83
|
|
102
84
|
def feed_invalid(errors, raw, coerced)
|
103
85
|
case options.invalid
|
86
|
+
when Objective::ALLOW
|
87
|
+
errors = nil
|
104
88
|
when Objective::DENY
|
105
89
|
nil
|
106
|
-
when Objective::DISCARD
|
107
|
-
return Objective::DISCARD
|
108
90
|
else
|
91
|
+
errors = nil
|
109
92
|
coerced = options.invalid
|
110
93
|
end
|
111
94
|
|
@@ -118,8 +101,6 @@ module Objective
|
|
118
101
|
errors = nil
|
119
102
|
when Objective::DENY
|
120
103
|
errors = :empty
|
121
|
-
when Objective::DISCARD
|
122
|
-
return Objective::DISCARD
|
123
104
|
else
|
124
105
|
coerced = options.empty
|
125
106
|
end
|
@@ -4,7 +4,6 @@ module Objective
|
|
4
4
|
module Filters
|
5
5
|
class ArrayFilter < Objective::Filter
|
6
6
|
Options = OpenStruct.new(
|
7
|
-
none: Objective::DENY,
|
8
7
|
nils: Objective::DENY,
|
9
8
|
invalid: Objective::DENY,
|
10
9
|
strict: false,
|
@@ -13,7 +12,7 @@ module Objective
|
|
13
12
|
|
14
13
|
def feed(raw)
|
15
14
|
result = super(raw)
|
16
|
-
return result if result
|
15
|
+
return result if result.errors || result.inputs.nil?
|
17
16
|
|
18
17
|
inputs = []
|
19
18
|
errors = Objective::Errors::ErrorArray.new
|
@@ -25,11 +24,6 @@ module Objective
|
|
25
24
|
data = result.coerced
|
26
25
|
data.each_with_index do |sub_data, index|
|
27
26
|
sub_result = sub_filter.feed(sub_data)
|
28
|
-
if sub_result == Objective::DISCARD
|
29
|
-
index_shift += 1
|
30
|
-
next
|
31
|
-
end
|
32
|
-
|
33
27
|
sub_data = sub_result.inputs
|
34
28
|
sub_error = sub_result.errors
|
35
29
|
|
@@ -4,7 +4,6 @@ module Objective
|
|
4
4
|
module Filters
|
5
5
|
class HashFilter < Objective::Filter
|
6
6
|
Options = OpenStruct.new(
|
7
|
-
none: Objective::DENY,
|
8
7
|
nils: Objective::DENY,
|
9
8
|
invalid: Objective::DENY,
|
10
9
|
strict: false
|
@@ -12,16 +11,15 @@ module Objective
|
|
12
11
|
|
13
12
|
def feed(raw)
|
14
13
|
result = super(raw)
|
15
|
-
return result if result
|
14
|
+
return result if result.errors || result.inputs.nil?
|
16
15
|
|
17
16
|
errors = Objective::Errors::ErrorHash.new
|
18
17
|
inputs = HashWithIndifferentAccess.new
|
19
18
|
|
20
19
|
data = result.coerced
|
21
20
|
sub_filters_hash.each_pair do |key, key_filter|
|
22
|
-
datum = data
|
21
|
+
datum = data[key]
|
23
22
|
key_filter_result = key_filter.feed(datum)
|
24
|
-
next if key_filter_result == Objective::DISCARD
|
25
23
|
|
26
24
|
sub_data = key_filter_result.inputs
|
27
25
|
sub_error = key_filter_result.errors
|
@@ -18,14 +18,13 @@ module Objective
|
|
18
18
|
result.raw = raw
|
19
19
|
result.coerced = coerce(raw)
|
20
20
|
|
21
|
-
inputs =
|
21
|
+
inputs = HashWithIndifferentAccess.new
|
22
22
|
errors = Objective::Errors::ErrorHash.new
|
23
23
|
|
24
24
|
data = result.coerced
|
25
25
|
sub_filters_hash.each_pair do |key, key_filter|
|
26
|
-
datum = data.to_h
|
26
|
+
datum = data.to_h[key]
|
27
27
|
key_filter_result = key_filter.feed(datum)
|
28
|
-
next if key_filter_result == Objective::DISCARD
|
29
28
|
|
30
29
|
sub_data = key_filter_result.inputs
|
31
30
|
sub_error = key_filter_result.errors
|
data/lib/objective/unit.rb
CHANGED
data/test/default_test.rb
CHANGED
@@ -6,29 +6,43 @@ require 'simple_unit'
|
|
6
6
|
describe 'Objective - defaults' do
|
7
7
|
class DefaultUnit
|
8
8
|
include Objective::Unit
|
9
|
+
|
9
10
|
filter do
|
10
|
-
string :name,
|
11
|
+
string :name, nils: '-nils-', invalid: '-invalid-', empty: '-empty-'
|
11
12
|
end
|
12
13
|
|
13
14
|
def execute
|
14
|
-
inputs
|
15
|
+
inputs
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
|
-
it 'should
|
19
|
-
outcome = DefaultUnit.run
|
20
|
-
assert_equal({ name: 'Bob Jones' }, outcome.result)
|
19
|
+
it 'should use a valid value passed to it' do
|
20
|
+
outcome = DefaultUnit.run(name: 'Fred')
|
21
21
|
assert_equal true, outcome.success
|
22
|
+
assert_equal({ 'name' => 'Fred' }, outcome.result)
|
22
23
|
end
|
23
24
|
|
24
|
-
it 'should
|
25
|
-
outcome = DefaultUnit.run
|
25
|
+
it 'should use a nils option default value if no value is passed' do
|
26
|
+
outcome = DefaultUnit.run
|
27
|
+
assert_equal({ 'name' => '-nils-' }, outcome.result)
|
26
28
|
assert_equal true, outcome.success
|
27
|
-
assert_equal({ name: 'Fred' }, outcome.result)
|
28
29
|
end
|
29
30
|
|
30
|
-
it 'should
|
31
|
+
it 'should use a nils option default value if nil is passed' do
|
31
32
|
outcome = DefaultUnit.run(name: nil)
|
32
|
-
assert_equal
|
33
|
+
assert_equal({ 'name' => '-nils-' }, outcome.result)
|
34
|
+
assert_equal true, outcome.success
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should use the invalid option default if an invalid value is passed' do
|
38
|
+
outcome = DefaultUnit.run(name: /regex/)
|
39
|
+
assert_equal({ 'name' => '-invalid-' }, outcome.result)
|
40
|
+
assert_equal true, outcome.success
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should use the empty option default if an empty value is passed' do
|
44
|
+
outcome = DefaultUnit.run(name: ' ')
|
45
|
+
assert_equal({ 'name' => '-empty-' }, outcome.result)
|
46
|
+
assert_equal true, outcome.success
|
33
47
|
end
|
34
48
|
end
|
data/test/inheritance_test.rb
CHANGED
@@ -16,22 +16,22 @@ describe 'Objective - inheritance' do
|
|
16
16
|
it 'should filter with inherited unit' do
|
17
17
|
outcome = SimpleInherited.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
|
18
18
|
assert outcome.success
|
19
|
-
assert_equal
|
19
|
+
assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'age' => 10, 'amount' => 22 }, outcome.inputs)
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'should filter with original unit' do
|
23
23
|
outcome = SimpleUnit.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
|
24
24
|
assert outcome.success
|
25
|
-
assert_equal
|
25
|
+
assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'amount' => 22 }, outcome.inputs)
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'shouldnt collide' do
|
29
29
|
outcome = SimpleInherited.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
|
30
30
|
assert outcome.success
|
31
|
-
assert_equal
|
31
|
+
assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'age' => 10, 'amount' => 22 }, outcome.inputs)
|
32
32
|
|
33
33
|
outcome = SimpleUnit.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
|
34
34
|
assert outcome.success
|
35
|
-
assert_equal
|
35
|
+
assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'amount' => 22 }, outcome.inputs)
|
36
36
|
end
|
37
37
|
end
|
@@ -8,14 +8,14 @@ describe 'Objective - errors' do
|
|
8
8
|
filter do
|
9
9
|
string :str1
|
10
10
|
string :str2, in: %w[opt1 opt2 opt3]
|
11
|
-
integer :int1,
|
11
|
+
integer :int1, nils: ALLOW
|
12
12
|
|
13
|
-
hash :hash1,
|
13
|
+
hash :hash1, nils: ALLOW do
|
14
14
|
boolean :bool1
|
15
15
|
boolean :bool2
|
16
16
|
end
|
17
17
|
|
18
|
-
array :arr1,
|
18
|
+
array :arr1, nils: ALLOW do
|
19
19
|
integer
|
20
20
|
end
|
21
21
|
end
|
@@ -74,7 +74,7 @@ describe 'Objective - errors' do
|
|
74
74
|
'str1' => :empty,
|
75
75
|
'str2' => :in,
|
76
76
|
'int1' => :integer,
|
77
|
-
'hash1' => { 'bool1' => :boolean, 'bool2' => :
|
77
|
+
'hash1' => { 'bool1' => :boolean, 'bool2' => :nils },
|
78
78
|
'arr1' => [:integer, nil, :integer]
|
79
79
|
}
|
80
80
|
|
@@ -88,7 +88,7 @@ describe 'Objective - errors' do
|
|
88
88
|
'int1' => 'Int1 must be an integer',
|
89
89
|
'hash1' => {
|
90
90
|
'bool1' => 'Bool1 must be a boolean',
|
91
|
-
'bool2' => 'Bool2
|
91
|
+
'bool2' => 'Bool2 cannot be nil'
|
92
92
|
},
|
93
93
|
'arr1' => ['1st Arr1 must be an integer', nil, '3rd Arr1 must be an integer']
|
94
94
|
}
|
@@ -102,7 +102,7 @@ describe 'Objective - errors' do
|
|
102
102
|
'Str2 is not an available option',
|
103
103
|
'Int1 must be an integer',
|
104
104
|
'Bool1 must be a boolean',
|
105
|
-
'Bool2
|
105
|
+
'Bool2 cannot be nil',
|
106
106
|
'1st Arr1 must be an integer',
|
107
107
|
'3rd Arr1 must be an integer'
|
108
108
|
]
|
@@ -114,23 +114,4 @@ describe 'Objective - errors' do
|
|
114
114
|
expected.each { |e| assert @outcome.errors.message_list.include?(e) }
|
115
115
|
end
|
116
116
|
end
|
117
|
-
|
118
|
-
class WithShrinkingArray
|
119
|
-
include Objective::Unit
|
120
|
-
filter do
|
121
|
-
array :arr_one do
|
122
|
-
integer nils: DISCARD
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'returns an ErrorArray for errors in arrays' do
|
128
|
-
o = WithShrinkingArray.run(arr_one: [nil, 1, 'sally'])
|
129
|
-
|
130
|
-
assert !o.success
|
131
|
-
assert o.errors.is_a?(Objective::Errors::ErrorHash)
|
132
|
-
assert o.errors[:arr_one].is_a?(Objective::Errors::ErrorArray)
|
133
|
-
assert_nil o.errors[:arr_one][0]
|
134
|
-
assert o.errors[:arr_one][1].is_a?(Objective::Errors::ErrorAtom)
|
135
|
-
end
|
136
117
|
end
|
@@ -128,17 +128,27 @@ describe 'Objective::Filters::ArrayFilter' do
|
|
128
128
|
hash do
|
129
129
|
string :foo
|
130
130
|
integer :bar
|
131
|
-
boolean :baz,
|
131
|
+
boolean :baz, nils: Objective::ALLOW
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
135
|
-
result = f.feed(
|
136
|
-
|
135
|
+
result = f.feed(
|
136
|
+
[
|
137
|
+
{ foo: 'f', bar: 3, baz: true },
|
138
|
+
{ foo: 'f', bar: 3 },
|
139
|
+
{ foo: 'f' }
|
140
|
+
]
|
141
|
+
)
|
142
|
+
expected_result = [
|
143
|
+
{ 'foo' => 'f', 'bar' => 3, 'baz' => true },
|
144
|
+
{ 'foo' => 'f', 'bar' => 3, 'baz' => nil },
|
145
|
+
{ 'foo' => 'f', 'baz' => nil }
|
146
|
+
]
|
137
147
|
|
138
148
|
assert_equal expected_result, result.inputs
|
139
149
|
assert_nil result.errors[0]
|
140
150
|
assert_nil result.errors[1]
|
141
|
-
assert_equal ({ 'bar' => :
|
151
|
+
assert_equal ({ 'bar' => :nils }), result.errors[2].codes
|
142
152
|
end
|
143
153
|
|
144
154
|
it 'lets you pass arrays of arrays' do
|
@@ -169,33 +179,33 @@ describe 'Objective::Filters::ArrayFilter' do
|
|
169
179
|
assert_equal [err_message_string, err_message_empty], result.errors.message_list
|
170
180
|
end
|
171
181
|
|
172
|
-
it 'can
|
182
|
+
it 'can make invalid elements nil' do
|
173
183
|
f = Objective::Filters::ArrayFilter.new(:arr) do
|
174
|
-
integer invalid:
|
184
|
+
integer invalid: nil
|
175
185
|
end
|
176
186
|
|
177
187
|
result = f.feed([1, '2', 'three', '4', 5, [6]])
|
178
188
|
assert_nil result.errors
|
179
|
-
assert_equal [1, 2, 4, 5], result.inputs
|
189
|
+
assert_equal [1, 2, nil, 4, 5, nil], result.inputs
|
180
190
|
end
|
181
191
|
|
182
|
-
it 'can
|
192
|
+
it 'can allow nil elements' do
|
183
193
|
f = Objective::Filters::ArrayFilter.new(:arr) do
|
184
|
-
integer nils: Objective::
|
194
|
+
integer nils: Objective::ALLOW
|
185
195
|
end
|
186
196
|
|
187
197
|
result = f.feed([nil, 1, '2', nil, nil, '4', 5, nil])
|
188
198
|
assert_nil result.errors
|
189
|
-
assert_equal [1, 2, 4, 5], result.inputs
|
199
|
+
assert_equal [nil, 1, 2, nil, nil, 4, 5, nil], result.inputs
|
190
200
|
end
|
191
201
|
|
192
|
-
it 'can
|
202
|
+
it 'can allow empty elements' do
|
193
203
|
f = Objective::Filters::ArrayFilter.new(:arr) do
|
194
|
-
string empty: Objective::
|
204
|
+
string empty: Objective::ALLOW
|
195
205
|
end
|
196
206
|
|
197
|
-
result = f.feed(['',
|
207
|
+
result = f.feed(['', 'foo', '', '', 'bar', ''])
|
198
208
|
assert_nil result.errors
|
199
|
-
assert_equal
|
209
|
+
assert_equal ['', 'foo', '', '', 'bar', ''], result.inputs
|
200
210
|
end
|
201
211
|
end
|
@@ -3,111 +3,96 @@
|
|
3
3
|
require 'test_helper'
|
4
4
|
|
5
5
|
describe 'Objective::Filters::RootFilter' do
|
6
|
-
describe '
|
7
|
-
it '
|
8
|
-
|
6
|
+
describe 'by default' do
|
7
|
+
it 'does not allow missing keys' do
|
8
|
+
f = Objective::Filters::RootFilter.new do
|
9
9
|
filter do
|
10
10
|
string :foo
|
11
|
-
string :bar
|
11
|
+
string :bar
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
result =
|
16
|
-
assert_equal
|
17
|
-
assert_nil result.errors
|
15
|
+
result = f.feed(foo: 'oof')
|
16
|
+
assert_equal :nils, result.errors[:bar].codes
|
18
17
|
end
|
19
18
|
|
20
|
-
it '
|
21
|
-
|
19
|
+
it 'does not allow nil values' do
|
20
|
+
f = Objective::Filters::RootFilter.new do
|
22
21
|
filter do
|
23
22
|
string :foo
|
24
|
-
string :bar
|
23
|
+
string :bar
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
|
-
result =
|
29
|
-
assert_equal
|
30
|
-
assert_nil result.errors
|
27
|
+
result = f.feed(foo: 'oof', bar: nil)
|
28
|
+
assert_equal :nils, result.errors[:bar].codes
|
31
29
|
end
|
32
30
|
|
33
|
-
it '
|
34
|
-
|
31
|
+
it 'does not allow empty values' do
|
32
|
+
f = Objective::Filters::RootFilter.new do
|
35
33
|
filter do
|
36
34
|
string :foo
|
37
|
-
string :bar
|
35
|
+
string :bar
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
41
|
-
result =
|
42
|
-
assert_equal
|
43
|
-
assert_nil result.errors
|
39
|
+
result = f.feed(foo: 'oof', bar: ' ')
|
40
|
+
assert_equal :empty, result.errors[:bar].codes
|
44
41
|
end
|
45
42
|
end
|
46
43
|
|
47
|
-
describe '
|
48
|
-
it '
|
49
|
-
|
44
|
+
describe 'when nils are allowed' do
|
45
|
+
it 'can accept a nil value' do
|
46
|
+
f = Objective::Filters::RootFilter.new do
|
50
47
|
filter do
|
51
48
|
string :foo
|
52
|
-
string :bar,
|
49
|
+
string :bar, nils: Objective::ALLOW
|
53
50
|
end
|
54
51
|
end
|
55
52
|
|
56
|
-
result =
|
57
|
-
assert_equal
|
53
|
+
result = f.feed(foo: 'oof', bar: nil)
|
54
|
+
assert_equal({ 'foo' => 'oof', 'bar' => nil }, result.inputs)
|
58
55
|
assert_nil result.errors
|
59
56
|
end
|
60
57
|
|
61
|
-
it '
|
62
|
-
|
58
|
+
it 'uses nil when a value is not passed' do
|
59
|
+
f = Objective::Filters::RootFilter.new do
|
63
60
|
filter do
|
64
61
|
string :foo
|
65
|
-
string :bar,
|
62
|
+
string :bar, nils: Objective::ALLOW
|
66
63
|
end
|
67
64
|
end
|
68
65
|
|
69
|
-
result =
|
70
|
-
assert_equal
|
66
|
+
result = f.feed(foo: 'oof')
|
67
|
+
assert_equal({ 'foo' => 'oof', 'bar' => nil }, result.inputs)
|
71
68
|
assert_nil result.errors
|
72
69
|
end
|
70
|
+
end
|
73
71
|
|
74
|
-
|
75
|
-
|
72
|
+
describe 'when empty values are allowed' do
|
73
|
+
it 'can accept an empty value' do
|
74
|
+
f = Objective::Filters::RootFilter.new do
|
76
75
|
filter do
|
77
76
|
string :foo
|
78
|
-
string :bar, empty: Objective::
|
77
|
+
string :bar, empty: Objective::ALLOW
|
79
78
|
end
|
80
79
|
end
|
81
80
|
|
82
|
-
result =
|
83
|
-
assert_equal
|
81
|
+
result = f.feed(foo: 'bar', bar: '')
|
82
|
+
assert_equal({ 'foo' => 'bar', 'bar' => '' }, result.inputs)
|
84
83
|
assert_nil result.errors
|
85
84
|
end
|
86
85
|
|
87
|
-
it '
|
88
|
-
|
89
|
-
filter do
|
90
|
-
string :foo
|
91
|
-
string :bar, none: Objective::ALLOW
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
result = hf.feed(foo: 'bar', bar: '')
|
96
|
-
assert_equal({ 'bar' => :empty }, result.errors.codes)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
describe 'discarding invalid values' do
|
101
|
-
it 'should discard invalid optional values' do
|
102
|
-
hf = Objective::Filters::RootFilter.new do
|
86
|
+
it 'can default a nil value to being empty' do
|
87
|
+
f = Objective::Filters::RootFilter.new do
|
103
88
|
filter do
|
104
89
|
string :foo
|
105
|
-
|
90
|
+
string :bar, empty: Objective::ALLOW, nils: ''
|
106
91
|
end
|
107
92
|
end
|
108
93
|
|
109
|
-
result =
|
110
|
-
assert_equal
|
94
|
+
result = f.feed(foo: 'bar', bar: '')
|
95
|
+
assert_equal({ 'foo' => 'bar', 'bar' => '' }, result.inputs)
|
111
96
|
assert_nil result.errors
|
112
97
|
end
|
113
98
|
end
|
data/test/simple_unit.rb
CHANGED
data/test/unit_test.rb
CHANGED
@@ -9,7 +9,7 @@ describe 'Unit' do
|
|
9
9
|
outcome = SimpleUnit.run(name: 'John', email: 'john@gmail.com', amount: 5)
|
10
10
|
|
11
11
|
assert outcome.success
|
12
|
-
assert_equal
|
12
|
+
assert_equal({ 'name' => 'John', 'email' => 'john@gmail.com', 'amount' => 5 }, outcome.inputs)
|
13
13
|
assert_nil outcome.errors
|
14
14
|
end
|
15
15
|
|
@@ -17,7 +17,7 @@ describe 'Unit' do
|
|
17
17
|
outcome = SimpleUnit.run(name: 'John', email: 'john@gmail.com', amount: 5, buggers: true)
|
18
18
|
|
19
19
|
assert outcome.success
|
20
|
-
assert_equal
|
20
|
+
assert_equal({ 'name' => 'John', 'email' => 'john@gmail.com', 'amount' => 5 }, outcome.inputs)
|
21
21
|
assert_nil outcome.errors
|
22
22
|
end
|
23
23
|
|
@@ -77,14 +77,14 @@ describe 'Unit' do
|
|
77
77
|
outcome = SimpleUnit.run({ name: 'John', email: 'john@gmail.com' }, email: 'bob@jones.com', amount: 5)
|
78
78
|
|
79
79
|
assert outcome.success
|
80
|
-
assert_equal
|
80
|
+
assert_equal({ 'name' => 'John', 'email' => 'bob@jones.com', 'amount' => 5 }, outcome.inputs)
|
81
81
|
end
|
82
82
|
|
83
83
|
it 'should merge hashes indifferently' do
|
84
84
|
outcome = SimpleUnit.run({ name: 'John', email: 'john@gmail.com' }, 'email' => 'bob@jones.com', 'amount' => 5)
|
85
85
|
|
86
86
|
assert outcome.success
|
87
|
-
assert_equal
|
87
|
+
assert_equal({ 'name' => 'John', 'email' => 'bob@jones.com', 'amount' => 5 }, outcome.inputs)
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'shouldn\'t accept non-hashes' do
|
@@ -107,7 +107,7 @@ describe 'Unit' do
|
|
107
107
|
|
108
108
|
it 'should return the filtered inputs in the outcome' do
|
109
109
|
outcome = SimpleUnit.run(name: ' John ', email: 'john@gmail.com', amount: '5')
|
110
|
-
assert_equal(
|
110
|
+
assert_equal({ 'name' => 'John', 'email' => 'john@gmail.com', 'amount' => 5 }, outcome.inputs)
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
@@ -116,7 +116,7 @@ describe 'Unit' do
|
|
116
116
|
include Objective::Unit
|
117
117
|
filter do
|
118
118
|
string :name
|
119
|
-
string :email,
|
119
|
+
string :email, nils: ALLOW
|
120
120
|
end
|
121
121
|
|
122
122
|
def execute
|
@@ -135,12 +135,12 @@ describe 'Unit' do
|
|
135
135
|
include Objective::Unit
|
136
136
|
filter do
|
137
137
|
string :name
|
138
|
-
string :email,
|
138
|
+
string :email, nils: ALLOW
|
139
139
|
end
|
140
140
|
|
141
141
|
def execute
|
142
|
-
inputs
|
143
|
-
inputs
|
142
|
+
inputs[:name] = 'bob'
|
143
|
+
inputs[:email] = 'bob@jones.com'
|
144
144
|
{ name: inputs[:name], email: inputs[:email] }
|
145
145
|
end
|
146
146
|
end
|
@@ -156,7 +156,7 @@ describe 'Unit' do
|
|
156
156
|
include Objective::Unit
|
157
157
|
filter do
|
158
158
|
string :name
|
159
|
-
string :email,
|
159
|
+
string :email, nils: ALLOW
|
160
160
|
end
|
161
161
|
|
162
162
|
def execute
|
@@ -179,7 +179,7 @@ describe 'Unit' do
|
|
179
179
|
include Objective::Unit
|
180
180
|
filter do
|
181
181
|
string :name
|
182
|
-
string :email,
|
182
|
+
string :email, nils: ALLOW
|
183
183
|
end
|
184
184
|
|
185
185
|
def execute
|
@@ -202,7 +202,7 @@ describe 'Unit' do
|
|
202
202
|
include Objective::Unit
|
203
203
|
filter do
|
204
204
|
string :name
|
205
|
-
string :email,
|
205
|
+
string :email, nils: ALLOW
|
206
206
|
end
|
207
207
|
|
208
208
|
def execute
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: objective
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dallin Crane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -81,7 +81,6 @@ files:
|
|
81
81
|
- lib/objective.rb
|
82
82
|
- lib/objective/allow.rb
|
83
83
|
- lib/objective/deny.rb
|
84
|
-
- lib/objective/discard.rb
|
85
84
|
- lib/objective/errors/error_array.rb
|
86
85
|
- lib/objective/errors/error_atom.rb
|
87
86
|
- lib/objective/errors/error_hash.rb
|
@@ -102,7 +101,6 @@ files:
|
|
102
101
|
- lib/objective/filters/root_filter.rb
|
103
102
|
- lib/objective/filters/string_filter.rb
|
104
103
|
- lib/objective/filters/time_filter.rb
|
105
|
-
- lib/objective/none.rb
|
106
104
|
- lib/objective/outcome.rb
|
107
105
|
- lib/objective/unit.rb
|
108
106
|
- test/default_test.rb
|
data/lib/objective/discard.rb
DELETED