newdwh2021/ecs/crm-datafetch/src/util/dict_checker.py
2022-08-05 23:29:19 +09:00

52 lines
2.0 KiB
Python

import re
class DictChecker:
def __init__(self, object_dict: dict) -> None:
self.__object_dict = object_dict
def is_empty(self, check_key):
"""辞書型バリュー空文字チェック"""
return self.__object_dict[check_key] != '' and self.__object_dict[check_key] is not None
def is_list_empty(self, check_key):
"""list型データ存在チェック"""
return len(self.__object_dict[check_key]) != 0
def check_key_exist(self, check_key: str) -> bool:
"""辞書型キー存在チェック"""
return check_key in self.__object_dict and self.is_empty(check_key)
def check_data_type(self, check_key: str, check_type: type) -> bool:
"""辞書型バリュー型チェック"""
return isinstance(self.__object_dict[check_key], check_type)
def check_match_pattern(self, regex_str: str, check_key: str) -> bool:
"""辞書型バリュー正規表現チェック"""
return True if re.fullmatch(regex_str, self.__object_dict[check_key]) else False
def assert_key_exist(self, check_key: str) -> None:
"""辞書型キー存在検査"""
if not self.check_key_exist(check_key):
raise Exception(f'{check_key}」キーは必須です')
return
def assert_data_type(self, check_key: str, check_type: type) -> None:
"""バリュー型検査"""
if not self.check_data_type(check_key, check_type):
raise Exception(f'{check_key}」キーの値は「{check_type}」でなければなりません')
return
def assert_match_pattern(self, check_key: str, regex_str: str):
"""正規表現検査"""
if not self.check_match_pattern(regex_str, check_key):
raise Exception(f'{check_key}」キーの値の正規表現「{regex_str}」チェックに失敗しました')
return
def assert_list_empty(self, check_key: str):
if not self.is_list_empty(check_key):
raise Exception(f'{check_key}」キーのリストの値は必須です')