Skip to content

Commit 1172c48

Browse files
author
mniebla
committed
int range feature
1 parent e585732 commit 1172c48

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

flask_restful/types.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ def positive(value, argument='argument'):
188188
return value
189189

190190

191+
def int_range(low, high, value, argument='argument'):
192+
""" Restrict input to an integer in a range (inclusive) """
193+
value = _get_integer(value)
194+
if value < low or value > high:
195+
error = ('Invalid {arg}: {val}. {arg} must be within the range {lo} - {hi}'
196+
.format(arg=argument, val=value, lo=low, hi=high))
197+
raise ValueError(error)
198+
199+
return value
200+
201+
191202
def boolean(value):
192203
"""Parse the string "true" or "false" as a boolean (case insensitive)"""
193204
value = value.lower()

tests/test_types.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ def test_natual_negative(self):
149149
def test_natural(self):
150150
assert_equal(3, types.natural(3))
151151

152-
153152
def test_natual_string(self):
154153
assert_raises(ValueError, lambda: types.natural('foo'))
155154

@@ -163,6 +162,19 @@ def test_positive_zero(self):
163162
def test_positive_negative_input(self):
164163
assert_raises(ValueError, lambda: types.positive(-1))
165164

165+
def test_int_range_good(self):
166+
assert_equal(3, types.int_range(1, 5, 3, 'my_arg'))
167+
168+
def test_int_range_inclusive(self):
169+
assert_equal(5, types.int_range(1, 5, 5, 'my_arg'))
170+
171+
def test_int_range_low(self):
172+
assert_raises(ValueError, lambda: types.int_range(0, 5, -1, 'my_arg'))
173+
174+
def test_int_range_high(self):
175+
assert_raises(ValueError, lambda: types.int_range(0, 5, 6, 'my_arg'))
176+
177+
166178

167179
def test_isointerval():
168180
intervals = [

0 commit comments

Comments
 (0)