From 0606aeeab2bedfe77dbbd35c9d6ecccf07ae6e49 Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 13 Feb 2025 01:55:37 +0530 Subject: [PATCH] Redesigned config validation function --- config.py | 8 ++-- functions/validate_config.py | 88 +++++++++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/config.py b/config.py index e9606f2..949d5aa 100644 --- a/config.py +++ b/config.py @@ -7,7 +7,8 @@ item_attributes = { required=True, unique=True, primary=True, - regex=r"^[A-Z0-9]+$" # Only uppercase letters and numbers + regex=r"^[A-Z0-9]+$", # Only uppercase letters and numbers + default_val=1000000 ), "hostname": Attribute( display_name="Host Name", @@ -26,13 +27,14 @@ item_attributes = { display_name="Status", html_input_type="select", required=True, - options=["Active", "Inactive"] # Allowed values + options=["Active", "Inactive"], # Allowed values + default_val="Active" ), "staffnum": Attribute( display_name="Staff No.", html_input_type="number", required=True, min=100000, # 6 digits - max=99999999 # 8 digits + max=99999999, # 8 digits ) } \ No newline at end of file diff --git a/functions/validate_config.py b/functions/validate_config.py index e8f8d6e..253c578 100644 --- a/functions/validate_config.py +++ b/functions/validate_config.py @@ -1,4 +1,5 @@ -from functions.validate_values import validate_value +import re +from datetime import datetime # Validate the configuration file to ensure all attributes are properly defined. def validate_config(item_attributes): @@ -10,32 +11,75 @@ def validate_config(item_attributes): return f"Missing input type for attribute '{attrib_name}'." elif attrib.html_input_type not in ["text", "number", "date", "select"]: return f"Invalid input type for attribute '{attrib_name}'." - elif attrib.html_input_type == "select" and not attrib.options: - return f"Selection missing options for attribute '{attrib_name}'." - + elif attrib.html_input_type =="number": # Validate numbers + if attrib.min: + try: + min_val = int(attrib.min) + except: + return f"Min value for {attrib_name} must be a valid number." + if attrib.max: + try: + max_val = int(attrib.max) + except: + return f"Max value for {attrib_name} must be a valid number." + if attrib.min and attrib.max and max_val < min_val: + return f"Max value must greater than min value for attribute '{attrib_name}'" + if attrib.default_val: + try: + def_val = int(attrib.default_val) + except: + return f"Default value for {attrib_name} must be a valid number." + if attrib.min and def_val < attrib.min: + return f"Default value for {attrib_name} must be a greater or equal to min value." + if attrib.max and def_val > attrib.max: + return f"Default value for {attrib_name} must be a less or equal to max value." + elif attrib.html_input_type =="date": # Validate dates + if attrib.min: + try: + min_val = datetime.strptime(attrib.min, "%Y-%m-%d") + except: + return f"Min value for {attrib_name} must be a valid date in YYYY-MM-DD format." + if attrib.max: + try: + max_val = datetime.strptime(attrib.max, "%Y-%m-%d") + except: + return f"Max value for {attrib_name} must be a valid date in YYYY-MM-DD format." + if attrib.min and attrib.max and max_val < min_val: + return f"Max value must greater than min value for attribute '{attrib_name}'" + if attrib.default_val: + try: + def_val = datetime.strptime(attrib.default_val, "%Y-%m-%d") + except: + return f"Default value for {attrib_name} must be a valid date in YYYY-MM-DD format." + if attrib.min and def_val < attrib.min: + return f"Default value for {attrib_name} must be a greater or equal to min value." + if attrib.max and def_val > attrib.max: + return f"Default value for {attrib_name} must be a less or equal to max value." + elif attrib.html_input_type =="text": # Validate text types + if attrib.min or attrib.max or attrib.auto_increment or attrib.options: + return f"Text attribute {attrib_name} can't have min, max, auto_increment, or options." + if attrib.default_val: + try: + def_val = str(attrib.default_val) + if attrib.regex and not re.match(attrib.regex, def_val): + return f"Default value for {attrib_name} is invalid. Allowed pattern: {attrib.regex}." + except: + return f"Unexpected default value for text attribute '{attrib_name}.'" + elif attrib.html_input_type == "select": # Validate selection menu types + if not attrib.options: + return f"Selection missing options for attribute '{attrib_name}'." + if attrib.default_val and attrib.default_val not in attrib.options: + return f"Default value for '{attrib_name}' must be in the options." + # Validate min and max values - if attrib.min or attrib.max: - if attrib.html_input_type not in ["number", "date"]: - return f"'{attrib.display_name}' must be of type 'number' or 'date' to have min or max values." - else: - validation_result = validate_value(attrib, attrib.min) - if validation_result != "Ok": - return f"Invalid default value for attribute '{attrib_name}': {validation_result}" - validation_result = validate_value(attrib, attrib.min) - if validation_result != "Ok": - return f"Invalid default value for attribute '{attrib_name}': {validation_result}" - - # Validate default value - if attrib.default_val: - validation_result = validate_value(attrib, attrib.default_val) - if validation_result != "Ok": - return f"Invalid default value for attribute '{attrib_name}': {validation_result}" + if (attrib.min or attrib.max) and attrib.html_input_type not in ['number', 'date']: + return f"'{attrib_name}' must be of type 'number' or 'date' to have min or max values." # Validate comparison if attrib.compareto: - if attrib.compareto[0] not in ["lt", "gt", "le", "ge", "eq"]: - return f"Invalid comparison operator for attribute '{attrib_name}'. Valid operators are: lt, gt, le, ge, eq." if attrib.compareto[1] not in item_attributes: return f"Invalid reference attribute '{attrib.compareto[1]}' for comparison in attribute '{attrib_name}'." + if attrib.compareto[0] not in ["lt", "gt", "le", "ge", "eq"]: + return f"Invalid comparison operator for attribute '{attrib_name}'. Valid operators are: lt, gt, le, ge, eq." return "Ok" \ No newline at end of file