赵福来
First commit
90a947b
"""
validator module-input and data validation
"""
from typing import Tuple
from src.config.settings import settings
class Validator:
"""validator class"""
@staticmethod
def validate_api_key(api_key: str) -> Tuple[bool, str]:
"""api validation"""
if not api_key or api_key.strip() == "":
return False, "API KEY should not be empty!"
if len(api_key.strip()) < 10:
return False, "API KEY value error"
return True, "API KEY validation passed"
@staticmethod
def validate_base_url(base_url: str) -> Tuple[bool, str]:
"""URL validation"""
if not base_url or base_url.strip() == "":
return False, "Base URL should not be empty!"
if not (base_url.startswith("http://") or base_url.startswith("https://")):
return False, "Base URL should start with <http://> or <https://>"
return True, "Base URL validation passed"
@staticmethod
def validate_models(attack_model: str, victim_model: str) -> Tuple[bool, str]:
"""model validation"""
if not attack_model or attack_model.strip() == "":
return False, "Please select the attack model's name"
if not victim_model or victim_model.strip() == "":
return False, "Please select the Victim model's name"
if attack_model not in settings.AVAILABLE_MODELS:
return False, f"Attack model '{attack_model}' is not available!"
if victim_model not in settings.AVAILABLE_MODELS:
return False, f"Victim model '{victim_model}' is not available!"
return True, "Model selection validation passed!"
@staticmethod
def validate_topic(topic: str) -> Tuple[bool, str]:
"""topic validation"""
if not topic or topic.strip() == "":
return False, "Topic should not be empty!"
if len(topic.strip()) < 3:
return False, "Topic length should be more than 3 strings!"
if len(topic.strip()) > 200:
return False, "Topic length should be less than 200 strings!"
return True, "Topic validation passed!"
@staticmethod
def validate_max_rounds(max_rounds: int) -> Tuple[bool, str]:
"""max rounds validation"""
if max_rounds < 1:
return False, "Max rounds should not be larger than:0!"
if max_rounds > settings.MAX_CONVERSATION_ROUNDS:
return False, f"Max rounds should be smaller than:{settings.MAX_CONVERSATION_ROUNDS}!"
return True, "Max round validation passed!"
@staticmethod
def validate_all_inputs(api_key: str,
base_url: str,
attack_model: str,
victim_model: str,
topic: str,
max_rounds: int) -> Tuple[bool, str]:
"""validate all data"""
validations = [
Validator.validate_api_key(api_key),
Validator.validate_base_url(base_url),
Validator.validate_models(attack_model, victim_model),
Validator.validate_topic(topic),
Validator.validate_max_rounds(max_rounds)
]
for is_valid, message in validations:
if not is_valid:
return False, message
return True, "All data validation passed!"