From d52cf17f156ec0c646a8bccfc069c6a149373c19 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Mon, 8 May 2017 17:33:37 +0100 Subject: [PATCH] tweaking exception definitions --- pydantic/exceptions.py | 8 ++++++++ pydantic/main.py | 11 ++--------- pydantic/validators.py | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pydantic/exceptions.py b/pydantic/exceptions.py index eafb91e..ccf3f25 100644 --- a/pydantic/exceptions.py +++ b/pydantic/exceptions.py @@ -53,3 +53,11 @@ class ValidationError(ValueError): class ConfigError(RuntimeError): pass + + +class Missing(ValueError): + pass + + +class Extra(ValueError): + pass diff --git a/pydantic/main.py b/pydantic/main.py index 3ea0738..72d6862 100644 --- a/pydantic/main.py +++ b/pydantic/main.py @@ -1,7 +1,7 @@ from collections import OrderedDict from types import FunctionType -from .exceptions import Error, ValidationError +from .exceptions import Error, Extra, Missing, ValidationError from .fields import Field from .validators import dict_validator @@ -14,6 +14,7 @@ class BaseConfig: raise_exception = True validate_all = False ignore_extra = True + # TODO allow extra def inherit_config(self_config, parent_config) -> BaseConfig: @@ -63,14 +64,6 @@ class MetaModel(type): return super().__new__(mcs, name, bases, namespace) -class Missing(ValueError): - pass - - -class Extra(ValueError): - pass - - MISSING = Missing('field required') MISSING_ERROR = Error(MISSING, None, None, None) EXTRA_ERROR = Error(Extra('extra fields not permitted'), None, None, None) diff --git a/pydantic/validators.py b/pydantic/validators.py index 64f7490..501fbe5 100644 --- a/pydantic/validators.py +++ b/pydantic/validators.py @@ -81,7 +81,7 @@ def enum_validator(v, field, **kwargs) -> Enum: return field.type_(v) -# order is important here, for example: bool is a subclass of int, datetime is a subclass of date +# order is important here, for example: bool is a subclass of int so has to come first, datetime before date same _VALIDATORS = [ (str, [not_none_validator, str_validator, anystr_length_validator]), (bytes, [not_none_validator, bytes_validator, anystr_length_validator]),