mutations 0.5.4 → 0.5.9
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.
- data/Gemfile.lock +1 -1
- data/TODO +2 -0
- data/lib/mutations.rb +2 -1
- data/lib/mutations/errors.rb +1 -1
- data/lib/mutations/float_filter.rb +36 -0
- data/lib/mutations/hash_filter.rb +10 -0
- data/lib/mutations/input_filter.rb +11 -2
- data/lib/mutations/string_filter.rb +9 -8
- data/lib/mutations/version.rb +1 -1
- data/spec/float_filter_spec.rb +97 -0
- data/spec/hash_filter_spec.rb +105 -2
- metadata +5 -2
data/Gemfile.lock
CHANGED
data/TODO
ADDED
data/lib/mutations.rb
CHANGED
@@ -8,6 +8,7 @@ require 'mutations/errors'
|
|
8
8
|
require 'mutations/input_filter'
|
9
9
|
require 'mutations/string_filter'
|
10
10
|
require 'mutations/integer_filter'
|
11
|
+
require 'mutations/float_filter'
|
11
12
|
require 'mutations/boolean_filter'
|
12
13
|
require 'mutations/model_filter'
|
13
14
|
require 'mutations/array_filter'
|
@@ -20,7 +21,7 @@ module Mutations
|
|
20
21
|
def error_message_creator
|
21
22
|
@error_message_creator ||= DefaultErrorMessageCreator.new
|
22
23
|
end
|
23
|
-
|
24
|
+
|
24
25
|
def error_message_creator=(creator)
|
25
26
|
@error_message_creator = creator
|
26
27
|
end
|
data/lib/mutations/errors.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Mutations
|
2
|
+
class FloatFilter < InputFilter
|
3
|
+
@default_options = {
|
4
|
+
nils: false, # true allows an explicit nil to be valid. Overrides any other options
|
5
|
+
# TODO: add strict
|
6
|
+
min: nil, # lowest value, inclusive
|
7
|
+
max: nil # highest value, inclusive
|
8
|
+
}
|
9
|
+
|
10
|
+
def filter(data)
|
11
|
+
|
12
|
+
# Handle nil case
|
13
|
+
if data.nil?
|
14
|
+
return [nil, nil] if options[:nils]
|
15
|
+
return [nil, :nils]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Ensure it's the correct data type (Float)
|
19
|
+
if !data.is_a?(Float)
|
20
|
+
if data.is_a?(String) && data =~ /^[-+]?\d*\.?\d+/
|
21
|
+
data = data.to_f
|
22
|
+
elsif data.is_a?(Fixnum)
|
23
|
+
data = data.to_f
|
24
|
+
else
|
25
|
+
return [data, :float]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
return [data, :min] if options[:min] && data < options[:min]
|
30
|
+
return [data, :max] if options[:max] && data > options[:max]
|
31
|
+
|
32
|
+
# We win, it's valid!
|
33
|
+
[data, nil]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -108,6 +108,11 @@ module Mutations
|
|
108
108
|
end
|
109
109
|
|
110
110
|
data_element = data[key]
|
111
|
+
|
112
|
+
# First, discard optional nils/empty params
|
113
|
+
data.delete(key) if !is_required && data.has_key?(key) && filterer.discard_nils? && data_element.nil?
|
114
|
+
data.delete(key) if !is_required && data.has_key?(key) && filterer.discard_empty? && data_element == ""
|
115
|
+
|
111
116
|
default_used = false
|
112
117
|
if !data.has_key?(key) && filterer.has_default?
|
113
118
|
data_element = filterer.default
|
@@ -134,6 +139,11 @@ module Mutations
|
|
134
139
|
|
135
140
|
filtered_keys.each do |key|
|
136
141
|
data_element = data[key]
|
142
|
+
|
143
|
+
# First, discard optional nils/empty params
|
144
|
+
next if data.has_key?(key) && wildcard_filterer.discard_nils? && data_element.nil?
|
145
|
+
next if data.has_key?(key) && wildcard_filterer.discard_empty? && data_element == ""
|
146
|
+
|
137
147
|
sub_data, sub_error = wildcard_filterer.filter(data_element)
|
138
148
|
if sub_error.nil?
|
139
149
|
filtered_data[key] = sub_data
|
@@ -19,11 +19,20 @@ module Mutations
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def has_default?
|
22
|
-
|
22
|
+
options.has_key?(:default)
|
23
23
|
end
|
24
24
|
|
25
25
|
def default
|
26
|
-
|
26
|
+
options[:default]
|
27
|
+
end
|
28
|
+
|
29
|
+
# Only relevant for optional params
|
30
|
+
def discard_nils?
|
31
|
+
!options[:nils]
|
32
|
+
end
|
33
|
+
|
34
|
+
def discard_empty?
|
35
|
+
options[:discard_empty]
|
27
36
|
end
|
28
37
|
end
|
29
38
|
end
|
@@ -1,14 +1,15 @@
|
|
1
1
|
module Mutations
|
2
2
|
class StringFilter < InputFilter
|
3
3
|
@default_options = {
|
4
|
-
strip: true,
|
5
|
-
strict: false,
|
6
|
-
nils: false,
|
7
|
-
empty: false,
|
8
|
-
min_length: nil,
|
9
|
-
max_length: nil,
|
10
|
-
matches: nil,
|
11
|
-
in: nil
|
4
|
+
strip: true, # true calls data.strip if data is a string
|
5
|
+
strict: false, # If false, then symbols, numbers, and booleans are converted to a string with to_s. # TODO: TEST
|
6
|
+
nils: false, # true allows an explicit nil to be valid. Overrides any other options
|
7
|
+
empty: false, # false disallows "". true allows "" and overrides any other validations (b/c they couldn't be true if it's empty)
|
8
|
+
min_length: nil, # Can be a number like 5, meaning that 5 codepoints are required
|
9
|
+
max_length: nil, # Can be a number like 10, meaning that at most 10 codepoints are permitted
|
10
|
+
matches: nil, # Can be a regexp
|
11
|
+
in: nil, # Can be an array like %w(red blue green)
|
12
|
+
discard_empty: false # If the param is optional, discard_empty: true drops empty fields.
|
12
13
|
}
|
13
14
|
|
14
15
|
def filter(data)
|
data/lib/mutations/version.rb
CHANGED
@@ -0,0 +1,97 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe "Mutations::FloatFilter" do
|
4
|
+
|
5
|
+
it "allows floats" do
|
6
|
+
f = Mutations::FloatFilter.new
|
7
|
+
filtered, errors = f.filter(3.1415926)
|
8
|
+
assert_equal 3.1415926, filtered
|
9
|
+
assert_equal nil, errors
|
10
|
+
end
|
11
|
+
|
12
|
+
it "allows strings that start with a digit" do
|
13
|
+
f = Mutations::FloatFilter.new
|
14
|
+
filtered, errors = f.filter("3")
|
15
|
+
assert_equal 3.0, filtered
|
16
|
+
assert_equal nil, errors
|
17
|
+
end
|
18
|
+
|
19
|
+
it "allows string representation of float" do
|
20
|
+
f = Mutations::FloatFilter.new
|
21
|
+
filtered, errors = f.filter("3.14")
|
22
|
+
assert_equal 3.14, filtered
|
23
|
+
assert_equal nil, errors
|
24
|
+
end
|
25
|
+
|
26
|
+
it "allows string representation of float without a number before dot" do
|
27
|
+
f = Mutations::FloatFilter.new
|
28
|
+
filtered, errors = f.filter(".14")
|
29
|
+
assert_equal 0.14, filtered
|
30
|
+
assert_equal nil, errors
|
31
|
+
end
|
32
|
+
|
33
|
+
it "allows negative strings" do
|
34
|
+
f = Mutations::FloatFilter.new
|
35
|
+
filtered, errors = f.filter("-.14")
|
36
|
+
assert_equal -0.14, filtered
|
37
|
+
assert_equal nil, errors
|
38
|
+
end
|
39
|
+
|
40
|
+
it "allows strings with a positive sign" do
|
41
|
+
f = Mutations::FloatFilter.new
|
42
|
+
filtered, errors = f.filter("+.14")
|
43
|
+
assert_equal 0.14, filtered
|
44
|
+
assert_equal nil, errors
|
45
|
+
end
|
46
|
+
|
47
|
+
it "doesnt't allow other strings, nor does it allow random objects or symbols" do
|
48
|
+
f = Mutations::FloatFilter.new
|
49
|
+
["zero","a1", {}, [], Object.new, :d].each do |thing|
|
50
|
+
filtered, errors = f.filter(thing)
|
51
|
+
assert_equal :float, errors
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "considers nil to be invalid" do
|
56
|
+
f = Mutations::FloatFilter.new(nils: false)
|
57
|
+
filtered, errors = f.filter(nil)
|
58
|
+
assert_equal nil, filtered
|
59
|
+
assert_equal :nils, errors
|
60
|
+
end
|
61
|
+
|
62
|
+
it "considers nil to be valid" do
|
63
|
+
f = Mutations::FloatFilter.new(nils: true)
|
64
|
+
filtered, errors = f.filter(nil)
|
65
|
+
assert_equal nil, filtered
|
66
|
+
assert_equal nil, errors
|
67
|
+
end
|
68
|
+
|
69
|
+
it "considers low numbers invalid" do
|
70
|
+
f = Mutations::FloatFilter.new(min: 10)
|
71
|
+
filtered, errors = f.filter(3)
|
72
|
+
assert_equal 3, filtered
|
73
|
+
assert_equal :min, errors
|
74
|
+
end
|
75
|
+
|
76
|
+
it "considers low numbers valid" do
|
77
|
+
f = Mutations::FloatFilter.new(min: 10)
|
78
|
+
filtered, errors = f.filter(31)
|
79
|
+
assert_equal 31, filtered
|
80
|
+
assert_equal nil, errors
|
81
|
+
end
|
82
|
+
|
83
|
+
it "considers high numbers invalid" do
|
84
|
+
f = Mutations::FloatFilter.new(max: 10)
|
85
|
+
filtered, errors = f.filter(31)
|
86
|
+
assert_equal 31, filtered
|
87
|
+
assert_equal :max, errors
|
88
|
+
end
|
89
|
+
|
90
|
+
it "considers high numbers vaild" do
|
91
|
+
f = Mutations::FloatFilter.new(max: 10)
|
92
|
+
filtered, errors = f.filter(3)
|
93
|
+
assert_equal 3, filtered
|
94
|
+
assert_equal nil, errors
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
data/spec/hash_filter_spec.rb
CHANGED
@@ -32,8 +32,8 @@ describe "Mutations::HashFilter" do
|
|
32
32
|
hf = Mutations::HashFilter.new do
|
33
33
|
string :*
|
34
34
|
end
|
35
|
-
filtered, errors = hf.filter(foo:
|
36
|
-
assert_equal ({"foo" => :
|
35
|
+
filtered, errors = hf.filter(foo: [])
|
36
|
+
assert_equal ({"foo" => :string}), errors.symbolic
|
37
37
|
end
|
38
38
|
|
39
39
|
it "allows a mix of specific keys and then wildcards" do
|
@@ -46,4 +46,107 @@ describe "Mutations::HashFilter" do
|
|
46
46
|
assert_equal nil, errors
|
47
47
|
end
|
48
48
|
|
49
|
+
it "doesn't allow a mix of specific keys and then wildcards -- should raise errors appropriately" do
|
50
|
+
hf = Mutations::HashFilter.new do
|
51
|
+
string :foo
|
52
|
+
integer :*
|
53
|
+
end
|
54
|
+
filtered, errors = hf.filter(foo: "bar", baz: "poopin")
|
55
|
+
assert_equal ({"baz" => :integer}), errors.symbolic
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "optional params and nils" do
|
59
|
+
it "bar is optional -- it works if not passed" do
|
60
|
+
hf = Mutations::HashFilter.new do
|
61
|
+
required do
|
62
|
+
string :foo
|
63
|
+
end
|
64
|
+
optional do
|
65
|
+
string :bar
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
filtered, errors = hf.filter(foo: "bar")
|
70
|
+
assert_equal ({"foo" => "bar"}), filtered
|
71
|
+
assert_equal nil, errors
|
72
|
+
end
|
73
|
+
|
74
|
+
it "bar is optional -- it works if nil is passed" do
|
75
|
+
hf = Mutations::HashFilter.new do
|
76
|
+
required do
|
77
|
+
string :foo
|
78
|
+
end
|
79
|
+
optional do
|
80
|
+
string :bar
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
filtered, errors = hf.filter(foo: "bar", bar: nil)
|
85
|
+
assert_equal ({"foo" => "bar"}), filtered
|
86
|
+
assert_equal nil, errors
|
87
|
+
end
|
88
|
+
|
89
|
+
it "bar is optional -- it works if nil is passed and nils are allowed" do
|
90
|
+
hf = Mutations::HashFilter.new do
|
91
|
+
required do
|
92
|
+
string :foo
|
93
|
+
end
|
94
|
+
optional do
|
95
|
+
string :bar, nils: true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
filtered, errors = hf.filter(foo: "bar", bar: nil)
|
100
|
+
assert_equal ({"foo" => "bar", "bar" => nil}), filtered
|
101
|
+
assert_equal nil, errors
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "optional params and empty values" do
|
106
|
+
it "bar is optional -- discards empty" do
|
107
|
+
hf = Mutations::HashFilter.new do
|
108
|
+
required do
|
109
|
+
string :foo
|
110
|
+
end
|
111
|
+
optional do
|
112
|
+
string :bar, discard_empty: true
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
filtered, errors = hf.filter(foo: "bar", bar: "")
|
117
|
+
assert_equal ({"foo" => "bar"}), filtered
|
118
|
+
assert_equal nil, errors
|
119
|
+
end
|
120
|
+
|
121
|
+
it "bar is optional -- errors if discard_empty is false and value is blank" do
|
122
|
+
hf = Mutations::HashFilter.new do
|
123
|
+
required do
|
124
|
+
string :foo
|
125
|
+
end
|
126
|
+
optional do
|
127
|
+
string :bar, discard_empty: false
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
filtered, errors = hf.filter(foo: "bar", bar: "")
|
132
|
+
assert_equal ({"bar" => :empty}), errors.symbolic
|
133
|
+
end
|
134
|
+
|
135
|
+
it "bar is optional -- discards empty -- now with wildcards" do
|
136
|
+
hf = Mutations::HashFilter.new do
|
137
|
+
required do
|
138
|
+
string :foo
|
139
|
+
end
|
140
|
+
optional do
|
141
|
+
string :*, discard_empty: true
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
filtered, errors = hf.filter(foo: "bar", bar: "")
|
146
|
+
assert_equal ({"foo" => "bar"}), filtered
|
147
|
+
assert_equal nil, errors
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
|
49
152
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mutations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -73,12 +73,14 @@ files:
|
|
73
73
|
- MIT-LICENSE
|
74
74
|
- README.md
|
75
75
|
- Rakefile
|
76
|
+
- TODO
|
76
77
|
- lib/mutations.rb
|
77
78
|
- lib/mutations/array_filter.rb
|
78
79
|
- lib/mutations/boolean_filter.rb
|
79
80
|
- lib/mutations/command.rb
|
80
81
|
- lib/mutations/errors.rb
|
81
82
|
- lib/mutations/exception.rb
|
83
|
+
- lib/mutations/float_filter.rb
|
82
84
|
- lib/mutations/hash_filter.rb
|
83
85
|
- lib/mutations/input_filter.rb
|
84
86
|
- lib/mutations/integer_filter.rb
|
@@ -92,6 +94,7 @@ files:
|
|
92
94
|
- spec/command_spec.rb
|
93
95
|
- spec/default_spec.rb
|
94
96
|
- spec/errors_spec.rb
|
97
|
+
- spec/float_filter_spec.rb
|
95
98
|
- spec/hash_filter_spec.rb
|
96
99
|
- spec/inheritance_spec.rb
|
97
100
|
- spec/integer_filter_spec.rb
|