- `luhnok` validator added (credit card luhn mod10 validator).

This commit is contained in:
Chris McDonough
2012-08-30 02:03:28 -04:00
parent a2dd72cde6
commit 0b21b65030
4 changed files with 45 additions and 0 deletions

View File

@@ -12,6 +12,8 @@ Next release
- Allow ``quant`` and ``rounding`` args to ``colander.Decimal`` constructor.
- ``luhnok`` validator added (credit card luhn mod10 validator).
0.9.8 (2012-04-27)
------------------

View File

@@ -355,6 +355,28 @@ class OneOf(object):
mapping={'val':value, 'choices':choices})
raise Invalid(node, err)
def luhnok(node, value):
""" Validator which checks to make sure that the value passes a luhn
mod-10 checksum (credit cards). ``value`` must be a string, not an
integer."""
sum = 0
num_digits = len(value)
oddeven = num_digits & 1
for count in range(0, num_digits):
digit = int(value[count])
if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
if digit > 9:
digit = digit - 9
sum = sum + digit
if not (sum % 10) == 0:
raise Invalid(node,
'%r is not a valid credit card number' % value)
class SchemaType(object):
""" Base class for all schema types """
def flatten(self, node, appstruct, prefix='', listitem=False):

View File

@@ -408,6 +408,25 @@ class TestOneOf(unittest.TestCase):
e = invalid_exc(validator, None, None)
self.assertEqual(e.msg.interpolate(), '"None" is not one of 1, 2')
class Test_luhnok(unittest.TestCase):
def _callFUT(self, node, value):
from colander import luhnok
return luhnok(node, value)
def test_fail(self):
import colander
val = '10'
self.assertRaises(colander.Invalid, self._callFUT, None, val)
def test_fail2(self):
import colander
val = '99999999999999999999999'
self.assertRaises(colander.Invalid, self._callFUT, None, val)
def test_success(self):
val = '4111111111111111'
self.assertFalse(self._callFUT(None, val))
class TestSchemaType(unittest.TestCase):
def _makeOne(self, *arg, **kw):
from colander import SchemaType

View File

@@ -65,6 +65,8 @@ Validators
.. autoclass:: Email
.. autofunction:: luhnok
Types
~~~~~