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
Maxim Avanov
committed
Jul 3, 2019
1 parent
521b719
commit d7fcc22
Showing
11 changed files
with
142 additions
and
73 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ __pycache__/ | |
.idea/ | ||
.cache/ | ||
.pytest_cache/ | ||
.DS_Store | ||
|
||
build/ | ||
dist/ | ||
|
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
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
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,5 @@ | ||
from .parser import type_constructor | ||
from .codegen import typeit | ||
from . import flags | ||
from .schema.errors import Invalid | ||
from .utils import iter_invalid | ||
from .parser import type_constructor | ||
from .schema.errors import Error | ||
|
||
__all__ = ['type_constructor', 'typeit', 'flags', 'Invalid', 'iter_invalid'] | ||
__all__ = ('type_constructor', 'flags', 'Error') |
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 |
---|---|---|
@@ -1,5 +1,71 @@ | ||
from typing import NamedTuple, Optional, Any, Mapping, Iterator, Union, TypeVar, Callable | ||
|
||
import colander | ||
|
||
# Alias for typeit.Invalid, so users don't have to import two | ||
# packages | ||
from typeit import interface as iface | ||
|
||
Invalid = colander.Invalid | ||
|
||
|
||
class InvalidData(NamedTuple): | ||
path: str | ||
reason: str | ||
sample: Optional[Any] | ||
|
||
|
||
class Error(ValueError): | ||
def __init__(self, validation_error, sample_data): | ||
super().__init__() | ||
self.validation_error = validation_error | ||
self.sample_data = sample_data | ||
|
||
def __iter__(self) -> Iterator[InvalidData]: | ||
return iter_invalid(self.validation_error, self.sample_data) | ||
|
||
|
||
def iter_invalid(error: Invalid, | ||
data: Mapping[str, Any]) -> Iterator[InvalidData]: | ||
""" A helper function to iterate over data samples that | ||
caused an exception at object construction. Use it like this: | ||
>>> try: | ||
>>> obj = mk_obj(data) | ||
>>> except Invalid as e: | ||
>>> # iterate over a sequence of InvalidData | ||
>>> for e in iter_invalid(e, data): | ||
>>> ... | ||
""" | ||
for e_path, msg in error.asdict().items(): | ||
e_parts = [] | ||
for x in e_path.split('.'): | ||
# x is either a list index or a dict key | ||
try: | ||
x = int(x) | ||
except ValueError: | ||
pass | ||
e_parts.append(x) | ||
# traverse data for a value that caused an error | ||
traversed_value: Union[None, iface.ITraversable] = data | ||
for i in e_parts: | ||
try: | ||
traversed_value = traversed_value[i] | ||
except KeyError: | ||
# handles the case when key is missing from payload | ||
traversed_value = None | ||
break | ||
yield InvalidData(path=e_path, reason=msg, sample=traversed_value) | ||
|
||
|
||
T = TypeVar('T') | ||
S = TypeVar('S') | ||
|
||
|
||
def errors_aware_constructor(construct: Callable[[T], S], data: T) -> S: | ||
try: | ||
return construct(data) | ||
except Invalid as e: | ||
raise Error(validation_error=e, | ||
sample_data=data) |
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