newdwh2021/ecs/crm-datafetch/src/util/dict_checker.py

52 lines
2.1 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] == '' or self.__object_dict[check_key] is 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 not 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, expected_str: str):
"""正規表現検査"""
if not self.check_match_pattern(regex_str, check_key):
raise Exception(f'{check_key}」キーの値の正規表現チェックに失敗しました 「{expected_str}」形式である必要があります')
return
def assert_list_empty(self, check_key: str):
if self.is_list_empty(check_key):
raise Exception(f'{check_key}」キーのリストの値は必須です')