-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a module with validation exceptions
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
1 parent
99586c9
commit 1ac5437
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' |