coercive 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f107658df43970b3e3cef00a20ed705a11a0f3b9e44ec50c3e391e827c161fb
4
- data.tar.gz: df087c93a23d4764f77a816427bbdc7a9c2fc0f75a9137a34a82fc0004bd50c4
3
+ metadata.gz: f5ece0274ec3187ab9ae2407837048143d6c1c516bd4ddcfc953a208e5bee6ed
4
+ data.tar.gz: 6b9ffb6fa9c18a0d9049193c7e352201f559b86a98cd4258d49b756ca8bf4df9
5
5
  SHA512:
6
- metadata.gz: e53a1643a5e2f09fc78286cbcf991d535e876d816ca01f59807ada41315e9effd5093206e624f15941eb80d121b291e3c0c5a63e0c0def2b5403aaa1d09daae7
7
- data.tar.gz: 5b6668f6bd4241bb7eb1a56a623564be8f63e5538cd9b7cec726e69bf907e11734f4b7f5bc8f4795737847a322ff04b6a3bac0da9ee2ce3c3286b2ec26ef8777
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, integer, optional
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`
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "coercive"
3
- s.version = "1.3.0"
3
+ s.version = "1.4.0"
4
4
  s.summary = "Coercive is a library to validate and coerce user input"
5
5
  s.description = s.summary
6
6
  s.authors = ["Joe McIlvain", "Lucas Tolchinsky"]
@@ -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
- Integer(input)
161
- rescue TypeError, ArgumentError
162
- fail Coercive::Error.new("not_numeric")
163
- end
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
 
@@ -105,8 +105,9 @@ describe "Coercive" do
105
105
  @coercion = Module.new do
106
106
  extend Coercive
107
107
 
108
- attribute :foo, integer, required
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.3.0
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-02 00:00:00.000000000 Z
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: