Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2013-05-17 10:32:19 +0200
committerArthur de Jong <arthur@arthurdejong.org>2013-06-08 14:45:38 +0200
commit33ce4e99a38f997943a755a41b1061114634e11d (patch)
tree2082f5eb2a4409e5ab3b4f2fe7d456ce5ffc8f1a /stdnum
parent66d6259f692676291a2466bd02427fec6d2549a4 (diff)
Implement validate() for Belgian numbers
Diffstat (limited to 'stdnum')
-rw-r--r--stdnum/be/vat.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/stdnum/be/vat.py b/stdnum/be/vat.py
index 28b3822..5bffa3f 100644
--- a/stdnum/be/vat.py
+++ b/stdnum/be/vat.py
@@ -1,6 +1,6 @@
# vat.py - functions for handling Belgian VAT numbers
#
-# Copyright (C) 2012 Arthur de Jong
+# Copyright (C) 2012, 2013 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -23,12 +23,15 @@
'0403019261'
>>> compact('(0)403019261')
'0403019261'
->>> is_valid('BE 428759497')
-True
->>> is_valid('BE431150351')
-False
+>>> validate('BE 428759497')
+'0428759497'
+>>> validate('BE431150351')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
"""
+from stdnum.exceptions import *
from stdnum.util import clean
@@ -50,11 +53,23 @@ def checksum(number):
return (int(number[:-2]) + int(number[-2:])) % 97
+def validate(number):
+ """Checks to see if the number provided is a valid VAT number. This checks
+ the length, formatting and check digit."""
+ number = compact(number)
+ if not number.isdigit():
+ raise InvalidFormat()
+ if len(number) != 10:
+ raise InvalidLength()
+ if checksum(number) != 0:
+ raise InvalidChecksum()
+ return number
+
+
def is_valid(number):
"""Checks to see if the number provided is a valid VAT number. This checks
the length, formatting and check digit."""
try:
- number = compact(number)
- except:
+ return bool(validate(number))
+ except ValidationError:
return False
- return len(number) == 10 and number.isdigit() and checksum(number) == 0