Skip to content

Commit

Permalink
Provide a module with validation exceptions
Browse files Browse the repository at this point in the history
This introduces a new module for switching the validation scheme. Instead of
using the is_valid() function that returns a boolean a validate() function
either returns the sanitised number or raises an exception that should
indicate the kind of validation failure.

This should make it easier for applications calling this library to present
more informative messages to the user.
  • Loading branch information
arthurdejong committed Jun 8, 2013
1 parent 99586c9 commit 1ac5437
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ group=root
[nosetests]
with-doctest=true
doctest-extension=doctest
doctest-options=+IGNORE_EXCEPTION_DETAIL
with-coverage=true
cover-package=stdnum
cover-erase=true
Expand Down
66 changes: 66 additions & 0 deletions stdnum/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# exceptions.py - collection of stdnum exceptions
# coding: utf-8
#
# Copyright (C) 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
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Collection of exceptions.
The validation functions of stdnum should raise one of the below exceptions
when validation of the number fails.
"""


class ValidationError(Exception):
"""Top-level error for validating numbers.
This exception should normally not be raised, only subclasses of this
exception."""

def __str__(self):
return getattr(self, 'message', '')


class InvalidFormat(ValidationError):
"""Something is wrong with the format of the number.
This generally means characters or delimiters that are not allowed are
part of the number or required parts are missing."""

message = 'The number has an invalid format.'


class InvalidChecksum(ValidationError):
"""The number's internal checksum or check digit does not match."""

message = "The number's checksum or check digit is invalid."


class InvalidLength(InvalidFormat):
"""The length of the number is wrong."""

message = 'The number has an invalid length.'


class InvalidComponent(ValidationError):
"""One of the parts of the number has an invalid reference.
Some part of the number refers to some external entity like a country
code, a date or a predefined collection of values. The number contains
some invalid reference."""

message = 'One of the parts of the number are invalid or unknown.'

0 comments on commit 1ac5437

Please sign in to comment.