From e26ed95e4385f164fac8f21202cd65955fe1f88d Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Fri, 5 May 2017 18:20:30 +0100 Subject: [PATCH] add missing file --- pydantic/validators.py | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 pydantic/validators.py diff --git a/pydantic/validators.py b/pydantic/validators.py new file mode 100644 index 0000000..3ad026b --- /dev/null +++ b/pydantic/validators.py @@ -0,0 +1,79 @@ +from pathlib import Path +from typing import Optional + +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 +}