-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
937 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,8 @@ install: | |
- pip install -e . | ||
script: | ||
- pytest -q | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
on_failure: always |
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,165 @@ | ||
class MissingMapping(Exception): | ||
"""Error raised when no mapper has been defined for a class | ||
while dumping an instance of that class. | ||
""" | ||
def __init__(self, type): | ||
""" | ||
Args: | ||
type (type): a type | ||
""" | ||
message = "Missing mapping for object of type %s" % type | ||
super(MissingMapping, self).__init__(message) | ||
self.type = type | ||
|
||
|
||
class InvalidDocument(Exception): | ||
"""Error raised when validating a document. | ||
It's composed of all the errors detected. | ||
""" | ||
def __init__(self, errors): | ||
super(InvalidDocument, self).__init__() | ||
self.errors = errors | ||
|
||
def __len__(self): | ||
"""Returns errors count | ||
Returns: | ||
int | ||
""" | ||
return len(self.errors) | ||
|
||
def __getitem__(self, index): | ||
"""Returns error at index | ||
Args: | ||
index (int): index | ||
Returns: | ||
Exception | ||
""" | ||
return self.errors[index] | ||
|
||
|
||
class ValidationError(Exception): | ||
"""Base class for validation errors""" | ||
|
||
def __init__(self, message, path): | ||
""" | ||
Args: | ||
message (str): error message | ||
path (list): path of the invalid data | ||
""" | ||
super(ValidationError, self).__init__(message) | ||
self.path = path | ||
|
||
|
||
class InvalidType(ValidationError): | ||
"""Error raised by `Type` validator""" | ||
|
||
def __init__(self, invalid, expected, path): | ||
""" | ||
Args: | ||
invalid (type): invalid type received | ||
expected (type): type expected | ||
path (list): path of the invalid data | ||
""" | ||
message = "Invalid type, got %s instead of %s" % (invalid, expected) | ||
super(InvalidType, self).__init__(message, path) | ||
self.invalid = invalid | ||
self.expected = expected | ||
|
||
|
||
class NotEqual(ValidationError): | ||
"""Error raised by `Equal` validator""" | ||
|
||
def __init__(self, invalid, expected, path): | ||
""" | ||
Args: | ||
invalid (object): invalid value | ||
expected (object): expected value | ||
path (list): path of the invalid data | ||
""" | ||
message = "Invalid value, got %s instead of %s" % (invalid, expected) | ||
super(NotEqual, self).__init__(message, path) | ||
self.invalid = invalid | ||
self.expected = expected | ||
|
||
|
||
class InvalidMatch(ValidationError): | ||
"""Error raised by `Match` validator""" | ||
|
||
def __init__(self, invalid, regexp, path): | ||
""" | ||
Args: | ||
invalid (str): invalid value | ||
regexp (regexp): a regexp | ||
path (list): path of the invalid data | ||
""" | ||
message = "Value \"%s\" does not match pattern \"%s\"" % (invalid, regexp.pattern) | ||
super(InvalidMatch, self).__init__(message, path) | ||
self.invalid = invalid | ||
self.regexp = regexp | ||
|
||
|
||
class InvalidIn(ValidationError): | ||
"""Error raised by `In` validator""" | ||
|
||
def __init__(self, invalid, expected, path): | ||
""" | ||
Args: | ||
invalid (str): invalid value | ||
expected (list): list of expected values | ||
path (list): path of the invalid data | ||
""" | ||
message = "Value \"%s\" is not in %s" % (invalid, expected) | ||
super(InvalidIn, self).__init__(message, path) | ||
self.invalid = invalid | ||
self.expected = expected | ||
|
||
|
||
class InvalidLength(ValidationError): | ||
"""Error raised by `Length` validator""" | ||
|
||
def __init__(self, length, min, max, path): | ||
""" | ||
Args: | ||
length (int): received length | ||
min (int): minimum length | ||
max (int): maximum length | ||
path (list): path of the invalid data | ||
""" | ||
message = "Got %s items, should have been between %s and %s" % (length, min, max) | ||
super(InvalidLength, self).__init__(message, path) | ||
self.max = max | ||
self.min = min | ||
self.length = length | ||
|
||
|
||
class InvalidDateTimeFormat(ValidationError): | ||
"""Error raised by `DateTimeFormat` validator""" | ||
|
||
def __init__(self, value, format, path): | ||
""" | ||
Args: | ||
value (str): invalid datetime string | ||
format (str): format used to parse datetime | ||
path (list): path of the invalid data | ||
""" | ||
message = "Date value \"%s\" can't be parsed with format \"%s\"" % (value, format) | ||
super(InvalidDateTimeFormat, self).__init__(message, path) | ||
self.value = value | ||
self.format = format | ||
|
||
|
||
class MissingPolymorphicKey(ValidationError): | ||
"""Error raised if Polymorphic object do not contain a type key""" | ||
|
||
def __init__(self, key, path): | ||
""" | ||
Args: | ||
key (str): polymorphic key | ||
path (list): path of the invalid data | ||
""" | ||
message = "Polymorphic document does not contain the \"%s\" key " % key | ||
super(MissingPolymorphicKey, self).__init__(message, path) | ||
self.key = key |
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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
from .field import Field # NOQA | ||
from .datetime_field import Datetime # NOQA | ||
from .datetime_field import DateTime # NOQA | ||
from .date import Date # NOQA | ||
from .string import String # NOQA | ||
from .object import Object # NOQA | ||
from .list import List # NOQA | ||
from .polymorphic_list import PolymorphicList # NOQA | ||
from .constant import Constant # NOQA | ||
from .int import Int # NOQA | ||
from .float import Float # NOQA | ||
from .number import Number # NOQA | ||
from .bool import Bool # NOQA |
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,10 @@ | ||
from . import Field | ||
from ..validators import Type | ||
|
||
|
||
class Bool(Field): | ||
"""Field used to handle boolean values""" | ||
|
||
def __init__(self, **kwargs): | ||
kwargs.setdefault("validators", []).append(Type(bool)) | ||
super(Bool, self).__init__(**kwargs) |
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,39 @@ | ||
from datetime import datetime | ||
|
||
from . import Field | ||
from ..validators import Type | ||
|
||
|
||
class Date(Field): | ||
"""Field used to handle date values""" | ||
|
||
def __init__(self, format="%Y-%m-%d", **kwargs): | ||
""" | ||
Args: | ||
format (str): datetime format to use | ||
""" | ||
kwargs.setdefault("validators", []).append(Type(str)) | ||
super(Date, self).__init__(**kwargs) | ||
self._format = format | ||
|
||
def load(self, value): | ||
"""Loads a date python object from a JSON string | ||
Args: | ||
value (str): a value | ||
Returns: | ||
date | ||
""" | ||
return datetime.strptime(value, self._format).date() | ||
|
||
def dump(self, value): | ||
"""Dump a date to string representation | ||
Args: | ||
value (date): a date | ||
Returns: | ||
str | ||
""" | ||
return value.strftime(self._format) |
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
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,10 @@ | ||
from . import Field | ||
from ..validators import Type | ||
|
||
|
||
class Float(Field): | ||
"""Field used to handle float values""" | ||
|
||
def __init__(self, **kwargs): | ||
kwargs.setdefault("validators", []).append(Type(float)) | ||
super(Float, self).__init__(**kwargs) |
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,10 @@ | ||
from . import Field | ||
from ..validators import Type | ||
|
||
|
||
class Int(Field): | ||
"""Field used to handle int values""" | ||
|
||
def __init__(self, **kwargs): | ||
kwargs.setdefault("validators", []).append(Type(int)) | ||
super(Int, self).__init__(**kwargs) |
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
Oops, something went wrong.