From 35953d20ef061c97047b22fed712c5d40e6ff025 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Fri, 5 May 2017 18:17:50 +0100 Subject: [PATCH] moving validators --- README.rst | 2 +- pydantic/fields.py | 81 ++-------------------------------------------- pydantic/main.py | 3 +- pydantic/types.py | 2 +- 4 files changed, 6 insertions(+), 82 deletions(-) diff --git a/README.rst b/README.rst index 6270adb..a399f39 100644 --- a/README.rst +++ b/README.rst @@ -9,7 +9,7 @@ Data validation and settings management using python 3.6 type hinting. TODO: -* datetime types +* datetime types, enums * other types: regex, email field * exotic typing: Union, List, Dict * more info: alt names and descriptions diff --git a/pydantic/fields.py b/pydantic/fields.py index f371328..ffd8916 100644 --- a/pydantic/fields.py +++ b/pydantic/fields.py @@ -1,87 +1,10 @@ import inspect from collections import OrderedDict from enum import IntEnum -from pathlib import Path -from typing import Any, Callable, List, Optional, Type +from typing import Any, Callable, List, Type from .exceptions import ConfigError - -NoneType = type(None) - - -def not_none_validator(v): - if v is None: - raise TypeError('None is not an allow value') - return v - - -def str_validator(v) -> str: - if isinstance(v, (str, NoneType)): - return v - elif isinstance(v, bytes): - return v.decode() - return str(v) - - -def bytes_validator(v) -> bytes: - if isinstance(v, (bytes, NoneType)): - return v - return str_validator(v).encode() - - -BOOL_STRINGS = { - '1', - 'TRUE', - 'ON', -} - - -def bool_validator(v) -> bool: - if isinstance(v, bool): - return v - if isinstance(v, bytes): - v = v.decode() - if isinstance(v, str): - return v.upper() in BOOL_STRINGS - return bool(v) - - -def number_size_validator(v, m): - if m.config.min_number_size <= v <= m.config.max_number_size: - return v - raise ValueError(f'size not in range {m.config.min_number_size} to {m.config.max_number_size}') - - -def anystr_length_validator(v, m): - if v is None or m.config.min_anystr_length <= len(v) <= m.config.max_anystr_length: - return v - raise ValueError(f'length not in range {m.config.max_anystr_length} to {m.config.max_anystr_length}') - - -def dict_validator(v) -> dict: - if isinstance(v, dict): - return v - return dict(v) - - -VALIDATORS_LOOKUP = { - int: [int, number_size_validator], - float: [float, number_size_validator], - bool: [bool_validator], - - Path: [Path], - - # TODO could do this better by detecting option Unions, removing not_none_validator and dealing with it directly - Optional[str]: [str_validator, anystr_length_validator], - str: [not_none_validator, str_validator, anystr_length_validator], - - Optional[bytes]: [bytes_validator, anystr_length_validator], - bytes: [not_none_validator, bytes_validator, anystr_length_validator], - - dict: [not_none_validator, dict_validator] - - # TODO list, List, Dict, Union, datetime, date, time, custom types -} +from .validators import VALIDATORS_LOOKUP class ValidatorSignature(IntEnum): diff --git a/pydantic/main.py b/pydantic/main.py index 9a07f2c..cbf0b18 100644 --- a/pydantic/main.py +++ b/pydantic/main.py @@ -2,7 +2,8 @@ from collections import OrderedDict from types import FunctionType from .exceptions import ValidationError -from .fields import Field, dict_validator +from .fields import Field +from .validators import dict_validator class BaseConfig: diff --git a/pydantic/types.py b/pydantic/types.py index 345da53..ddba5d3 100644 --- a/pydantic/types.py +++ b/pydantic/types.py @@ -1,7 +1,7 @@ from typing import Optional, Type -from .fields import str_validator from .utils import import_string, make_dsn +from .validators import str_validator __all__ = [ 'NoneStr',