| 48 | |
| 49 | |
| 50 | class ValidationError(Representation, ValueError): |
| 51 | __slots__ = 'raw_errors', 'model', '_error_cache' |
| 52 | |
| 53 | def __init__(self, errors: Sequence[ErrorList], model: 'ModelOrDc') -> None: |
| 54 | self.raw_errors = errors |
| 55 | self.model = model |
| 56 | self._error_cache: Optional[List['ErrorDict']] = None |
| 57 | |
| 58 | def errors(self) -> List['ErrorDict']: |
| 59 | if self._error_cache is None: |
| 60 | try: |
| 61 | config = self.model.__config__ # type: ignore |
| 62 | except AttributeError: |
| 63 | config = self.model.__pydantic_model__.__config__ # type: ignore |
| 64 | self._error_cache = list(flatten_errors(self.raw_errors, config)) |
| 65 | return self._error_cache |
| 66 | |
| 67 | def json(self, *, indent: Union[None, int, str] = 2) -> str: |
| 68 | return json.dumps(self.errors(), indent=indent, default=pydantic_encoder) |
| 69 | |
| 70 | def __str__(self) -> str: |
| 71 | errors = self.errors() |
| 72 | no_errors = len(errors) |
| 73 | return ( |
| 74 | f'{no_errors} validation error{"" if no_errors == 1 else "s"} for {self.model.__name__}\n' |
| 75 | f'{display_errors(errors)}' |
| 76 | ) |
| 77 | |
| 78 | def __repr_args__(self) -> 'ReprArgs': |
| 79 | return [('model', self.model.__name__), ('errors', self.errors())] |
| 80 | |
| 81 | |
| 82 | def display_errors(errors: List['ErrorDict']) -> str: |
no outgoing calls
no test coverage detected