validators.input_validator
1import re 2from typing import Any 3 4 5class InputValidator: 6 """Validator for input JSON data related to task dispatching.""" 7 8 @staticmethod 9 def validate(json_data: dict[str, Any]) -> None: 10 """ 11 Validate the structure and content of a given JSON dictionary. 12 13 This method checks for the presence and format of the 'name', 'type', 14 and 'country' fields in the input data. If any validations fail, it 15 raises a ValueError with all relevant error messages. 16 17 :param json_data: Dictionary representing the input JSON data. 18 :raises ValueError: If the input data is not valid. 19 """ 20 errors: list[str] = [] 21 22 if not isinstance(json_data, dict): 23 raise ValueError("Input data must be a JSON object.") 24 25 name = json_data.get("name") 26 if not name or not isinstance(name, str): 27 errors.append("Missing or invalid 'name' field.") 28 elif len(name) < 2 or len(name) > 64: 29 errors.append("Invalid 'name': must be between 2 and 64 characters.") 30 elif not re.fullmatch(r"[A-Za-zÀ-ÿ\s\-]+", name): 31 errors.append(f"Invalid 'name': '{name}' contains invalid characters.") 32 33 task_type = json_data.get("type") 34 if not task_type or not isinstance(task_type, str): 35 errors.append("Missing or invalid 'type' field.") 36 elif len(task_type) < 2 or len(task_type) > 32: 37 errors.append("Invalid 'type': must be between 2 and 32 characters.") 38 elif not re.fullmatch(r"[a-zA-Z_]+", task_type): 39 errors.append(f"Invalid 'type': '{task_type}' contains invalid characters.") 40 41 country = json_data.get("country") 42 if not country or not isinstance(country, str): 43 errors.append("Missing or invalid 'country' field.") 44 elif not re.fullmatch(r"[A-Z]{2}", country.upper()): 45 errors.append( 46 f"Invalid country code '{country}'. Must be ISO 3166-1 alpha-2." 47 ) 48 49 if errors: 50 raise ValueError("; ".join(errors))
class
InputValidator:
6class InputValidator: 7 """Validator for input JSON data related to task dispatching.""" 8 9 @staticmethod 10 def validate(json_data: dict[str, Any]) -> None: 11 """ 12 Validate the structure and content of a given JSON dictionary. 13 14 This method checks for the presence and format of the 'name', 'type', 15 and 'country' fields in the input data. If any validations fail, it 16 raises a ValueError with all relevant error messages. 17 18 :param json_data: Dictionary representing the input JSON data. 19 :raises ValueError: If the input data is not valid. 20 """ 21 errors: list[str] = [] 22 23 if not isinstance(json_data, dict): 24 raise ValueError("Input data must be a JSON object.") 25 26 name = json_data.get("name") 27 if not name or not isinstance(name, str): 28 errors.append("Missing or invalid 'name' field.") 29 elif len(name) < 2 or len(name) > 64: 30 errors.append("Invalid 'name': must be between 2 and 64 characters.") 31 elif not re.fullmatch(r"[A-Za-zÀ-ÿ\s\-]+", name): 32 errors.append(f"Invalid 'name': '{name}' contains invalid characters.") 33 34 task_type = json_data.get("type") 35 if not task_type or not isinstance(task_type, str): 36 errors.append("Missing or invalid 'type' field.") 37 elif len(task_type) < 2 or len(task_type) > 32: 38 errors.append("Invalid 'type': must be between 2 and 32 characters.") 39 elif not re.fullmatch(r"[a-zA-Z_]+", task_type): 40 errors.append(f"Invalid 'type': '{task_type}' contains invalid characters.") 41 42 country = json_data.get("country") 43 if not country or not isinstance(country, str): 44 errors.append("Missing or invalid 'country' field.") 45 elif not re.fullmatch(r"[A-Z]{2}", country.upper()): 46 errors.append( 47 f"Invalid country code '{country}'. Must be ISO 3166-1 alpha-2." 48 ) 49 50 if errors: 51 raise ValueError("; ".join(errors))
Validator for input JSON data related to task dispatching.
@staticmethod
def
validate(json_data: dict[str, typing.Any]) -> None:
9 @staticmethod 10 def validate(json_data: dict[str, Any]) -> None: 11 """ 12 Validate the structure and content of a given JSON dictionary. 13 14 This method checks for the presence and format of the 'name', 'type', 15 and 'country' fields in the input data. If any validations fail, it 16 raises a ValueError with all relevant error messages. 17 18 :param json_data: Dictionary representing the input JSON data. 19 :raises ValueError: If the input data is not valid. 20 """ 21 errors: list[str] = [] 22 23 if not isinstance(json_data, dict): 24 raise ValueError("Input data must be a JSON object.") 25 26 name = json_data.get("name") 27 if not name or not isinstance(name, str): 28 errors.append("Missing or invalid 'name' field.") 29 elif len(name) < 2 or len(name) > 64: 30 errors.append("Invalid 'name': must be between 2 and 64 characters.") 31 elif not re.fullmatch(r"[A-Za-zÀ-ÿ\s\-]+", name): 32 errors.append(f"Invalid 'name': '{name}' contains invalid characters.") 33 34 task_type = json_data.get("type") 35 if not task_type or not isinstance(task_type, str): 36 errors.append("Missing or invalid 'type' field.") 37 elif len(task_type) < 2 or len(task_type) > 32: 38 errors.append("Invalid 'type': must be between 2 and 32 characters.") 39 elif not re.fullmatch(r"[a-zA-Z_]+", task_type): 40 errors.append(f"Invalid 'type': '{task_type}' contains invalid characters.") 41 42 country = json_data.get("country") 43 if not country or not isinstance(country, str): 44 errors.append("Missing or invalid 'country' field.") 45 elif not re.fullmatch(r"[A-Z]{2}", country.upper()): 46 errors.append( 47 f"Invalid country code '{country}'. Must be ISO 3166-1 alpha-2." 48 ) 49 50 if errors: 51 raise ValueError("; ".join(errors))
Validate the structure and content of a given JSON dictionary.
This method checks for the presence and format of the 'name', 'type', and 'country' fields in the input data. If any validations fail, it raises a ValueError with all relevant error messages.
Parameters
- json_data: Dictionary representing the input JSON data.
Raises
- ValueError: If the input data is not valid.