Skip to content

Commit

Permalink
Integer validator (threat9#235)
Browse files Browse the repository at this point in the history
* Add ability to cast Option's value to integer

* Fix formatting.
  • Loading branch information
fwkz authored and lucyoa committed Apr 20, 2017
1 parent 416a766 commit e144f58
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions routersploit/test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ def test_boolify_true_7(self):
value = "t"
self.assertEqual(validators.boolify(value), True)

def test_integer_1(self):
self.assertEqual(validators.integer('1'), 1)

def test_integer_2(self):
self.assertEqual(validators.integer('123'), 123)

def test_integer_3(self):
with self.assertRaises(OptionValidationError):
validators.integer('foobar')


if __name__ == '__main__':
unittest.main()
8 changes: 8 additions & 0 deletions routersploit/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ def boolify(value):
return False
else:
return bool(value)


def integer(number):
""" Cast Option value to the integer using int() """
try:
return int(number)
except ValueError:
raise OptionValidationError("Invalid option. can't cast '{}' to integer.".format(number))

0 comments on commit e144f58

Please sign in to comment.