def create_int_validator(min_: int = None, max_: int = None, nullable: bool = False) -> Callable[[int], str]: validators = [ lambda obj: if_(not isinstance(obj, int), TYPE_ERROR_MSG % INT_TYPE) ] validators = apply_min_max_validator(validators, int, min_, max_, create_limit_validator, INT_TYPE) return create_validator(validators, nullable, int)
def create_date_validator(min_: date = None, max_: date = None, nullable: bool = False) -> Callable[[date], str]: validators = [ lambda obj: if_(not isinstance(obj, date), TYPE_ERROR_MSG % DATE_TYPE) ] validators = append_validator_if_set(validators, isinstance(min_, date), create_limit_validator, lt, min_, MIN_ERROR_MSG, DATE_TYPE) validators = append_validator_if_set(validators, isinstance(max_, date), create_limit_validator, gt, max_, MAX_ERROR_MSG, DATE_TYPE) return create_validator(validators, nullable, to_date)
def create_str_validator(min_len: int = None, max_len: int = None, nullable: bool = False) -> Callable[[str], str]: validators = [ lambda obj: if_(not isinstance(obj, str), TYPE_ERROR_MSG % STR_TYPE) ] validators = append_validator_if_set(validators, isinstance(min_len, int), create_len_limit_validator, lt, min_len, MIN_LEN_ERROR_MSG, STR_TYPE) validators = append_validator_if_set(validators, isinstance(max_len, int), create_len_limit_validator, gt, max_len, MAX_LEN_ERROR_MSG, STR_TYPE) return create_validator(validators, nullable)
def create_dict_validator( nullable: bool = False, element_validators: Dict[str, Callable] = None) -> Callable[[dict], str]: validators = [ lambda obj: if_(not isinstance(obj, dict), TYPE_ERROR_MSG % DICT_TYPE) ] validate_dict = create_validator(validators, nullable) def super_func(obj: dict) -> str: message = validate_dict(obj) if message != VALID: return message return filter_msgs(obj, validate_as_dict, element_validators) return super_func
def create_float_validator(min_: float = None, max_: float = None, nullable: bool = False) -> Callable[[float], str]: # validate_min_max(min_, max_, float) validators = [ lambda obj: if_(not isinstance(obj, float), TYPE_ERROR_MSG % FLOAT_TYPE ) ] validators = append_validator_if_set(validators, isinstance(min_, float), create_limit_validator, lt, min_, MIN_ERROR_MSG, FLOAT_TYPE) validators = append_validator_if_set(validators, isinstance(max_, float), create_limit_validator, gt, max_, MAX_ERROR_MSG, FLOAT_TYPE) validators = apply_min_max_validator(validators, float, min_, max_, create_limit_validator, INT_TYPE) return create_validator(validators, nullable, float)
def create_list_validator( min_len: int = None, max_len: int = None, nullable: bool = False, validate_element: Callable = False) -> Callable[[list], str]: validators = [ lambda obj: if_(not isinstance(obj, list), TYPE_ERROR_MSG % LIST_TYPE) ] validators = append_validator_if_set(validators, isinstance(min_len, int), create_len_limit_validator, lt, min_len, MIN_LEN_ERROR_MSG, LIST_TYPE) validators = append_validator_if_set(validators, isinstance(max_len, int), create_len_limit_validator, gt, max_len, MAX_LEN_ERROR_MSG, LIST_TYPE) validate_list = create_validator(validators, nullable) def super_func(obj: list) -> str: message = validate_list(obj) if message != VALID: return message return filter_msgs(enumerate(obj), validate_as_list, validate_element) return super_func
def create_bool_validator(nullable: bool = False) -> Callable[[bool], str]: return create_validator([ lambda obj: if_(not isinstance(obj, bool), TYPE_ERROR_MSG % BOOL_TYPE) ], nullable)