coercive 1.3.0 → 1.4.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/README.md +10 -3
- data/coercive.gemspec +1 -1
- data/lib/coercive.rb +11 -6
- data/test/coercive.rb +12 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5ece0274ec3187ab9ae2407837048143d6c1c516bd4ddcfc953a208e5bee6ed
|
4
|
+
data.tar.gz: 6b9ffb6fa9c18a0d9049193c7e352201f559b86a98cd4258d49b756ca8bf4df9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d036ba5c9a7b0526903330fe17aff3e34f47f0c0a338769fb0762fed5da8ca7d0210143b0f87533dba2010637feed320cbf454d78d629ffd5d3ebb13a8ab18fb
|
7
|
+
data.tar.gz: c5eb1b8af3fb355081d15108bf037fd4f5ec9b85be892f875500b419a343a8d4136c26ce0ff3ca0c8fca8bf3bb91f7ac4acf8439369009fbec14dfaf98e5e043
|
data/README.md
CHANGED
@@ -171,15 +171,16 @@ CoerceFoo.call("foo" => 4)
|
|
171
171
|
# => Coercive::Error: {"foo"=>"not_valid"}
|
172
172
|
```
|
173
173
|
|
174
|
-
### `integer`
|
174
|
+
### `integer(min:, max:)`
|
175
175
|
|
176
|
-
`integer` expects an integer value.
|
176
|
+
`integer` expects an integer value. It supports optional `min` and `max` options to check if the user input is within certain bounds.
|
177
177
|
|
178
178
|
```ruby
|
179
179
|
module CoerceFoo
|
180
180
|
extend Coercive
|
181
181
|
|
182
|
-
attribute :foo,
|
182
|
+
attribute :foo, integer, optional
|
183
|
+
attribute :foo_bounds, integer(min: 1, max: 10), optional
|
183
184
|
end
|
184
185
|
|
185
186
|
CoerceFoo.call("foo" => "1")
|
@@ -193,6 +194,12 @@ CoerceFoo.call("foo" => "1.5")
|
|
193
194
|
|
194
195
|
CoerceFoo.call("foo" => 1.5)
|
195
196
|
# => Coercive::Error: {"foo"=>"float_not_permitted"}
|
197
|
+
|
198
|
+
CoerceFoo.call("foo_bounds" => 0)
|
199
|
+
# => Coercive::Error: {"foo_bounds"=>"too_low"}
|
200
|
+
|
201
|
+
CoerceFoo.call("foo_bounds" => 11)
|
202
|
+
# => Coercive::Error: {"foo_bounds"=>"too_high"}
|
196
203
|
```
|
197
204
|
|
198
205
|
### `float`
|
data/coercive.gemspec
CHANGED
data/lib/coercive.rb
CHANGED
@@ -152,15 +152,20 @@ module Coercive
|
|
152
152
|
|
153
153
|
# Public DSL: Return a coerce function to coerce input to an Integer.
|
154
154
|
# Used when declaring an attribute. See documentation for attr_coerce_fns.
|
155
|
-
def integer
|
155
|
+
def integer(min: nil, max: nil)
|
156
156
|
->(input) do
|
157
157
|
fail Coercive::Error.new("float_not_permitted") if input.is_a?(Float)
|
158
158
|
|
159
|
-
begin
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
159
|
+
input = begin
|
160
|
+
Integer(input)
|
161
|
+
rescue TypeError, ArgumentError
|
162
|
+
fail Coercive::Error.new("not_numeric")
|
163
|
+
end
|
164
|
+
|
165
|
+
fail Coercive::Error.new("too_low") if min && input < min
|
166
|
+
fail Coercive::Error.new("too_high") if max && input > max
|
167
|
+
|
168
|
+
input
|
164
169
|
end
|
165
170
|
end
|
166
171
|
|
data/test/coercive.rb
CHANGED
@@ -105,8 +105,9 @@ describe "Coercive" do
|
|
105
105
|
@coercion = Module.new do
|
106
106
|
extend Coercive
|
107
107
|
|
108
|
-
attribute :foo, integer,
|
108
|
+
attribute :foo, integer, optional
|
109
109
|
attribute :bar, integer, optional
|
110
|
+
attribute :baz, integer(min: 1, max: 10), optional
|
110
111
|
end
|
111
112
|
end
|
112
113
|
|
@@ -131,6 +132,16 @@ describe "Coercive" do
|
|
131
132
|
assert_coercion_error(expected_errors) { @coercion.call("foo" => value) }
|
132
133
|
end
|
133
134
|
end
|
135
|
+
|
136
|
+
it "errors if the input is out of bounds" do
|
137
|
+
expected_errors = { "baz" => "too_low" }
|
138
|
+
|
139
|
+
assert_coercion_error(expected_errors) { @coercion.call("baz" => 0) }
|
140
|
+
|
141
|
+
expected_errors = { "baz" => "too_high" }
|
142
|
+
|
143
|
+
assert_coercion_error(expected_errors) { @coercion.call("baz" => 11) }
|
144
|
+
end
|
134
145
|
end
|
135
146
|
|
136
147
|
describe "float" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coercive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe McIlvain
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-09-
|
12
|
+
date: 2020-09-03 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Coercive is a library to validate and coerce user input
|
15
15
|
email:
|