Minor additions to Attribute properties

This commit is contained in:
Candifloss 2025-02-27 15:59:41 +05:30
parent 0539934c99
commit d396b33686
2 changed files with 42 additions and 11 deletions

View File

@ -62,13 +62,25 @@ class textAttribute(Attribute):
error = super().validate()
if error:
return error
if self.default_val is not None and self.regex is not None:
if not re.match(self.regex, str(self.default_val)):
return f"Invalid default value for '{self.attrib_name}'. It must match the pattern: {self.regex}"
if self.allowed_chars is not None and self.default_val is not None:
for char in self.default_val:
if char not in self.allowed_chars:
return f"Invalid character '{char}' in default value for '{self.attrib_name}'. Allowed characters are: {self.allowed_chars}"
if self.min_length is not None and self.max_length is not None:
if not isinstance(self.min_length, int) or not isinstance(self.max_length, int):
return f"Min and max lengths must be integers for '{self.attrib_name}'."
if int(self.min_length) > int(self.max_length):
return f"Max length must be greater than min length for '{self.attrib_name}'."
if self.default_val is not None:
if self.regex is not None:
if not re.match(self.regex, str(self.default_val)):
return f"Default value for '{self.attrib_name}' must match the pattern: {self.regex}"
if self.allowed_chars is not None:
for char in self.default_val:
if char not in self.allowed_chars:
return f"Invalid character '{char}' in default value for '{self.attrib_name}'. Allowed characters are: {self.allowed_chars}"
if self.min_length is not None:
if len(self.default_val) < int(self.min_length):
return f"Invalid default value for '{self.attrib_name}'. The minimum length is: {self.min_length}"
if self.max_length is not None:
if len(self.default_val) > int(self.max_length):
return f"Invalid default value for '{self.attrib_name}'. The maximum length is: {self.max_length}"
return None
@ -80,11 +92,13 @@ class intAttribute(Attribute):
display_name: str,
min_val: Optional[int] = None,
max_val: Optional[int] = None,
step: int = 1,
**kwargs
):
super().__init__(attrib_name, display_name, html_input_type="number", **kwargs)
self.min_val = min_val
self.max_val = max_val
self.step = step
def validate(self) -> Optional[str]:
"""Validate integer-specific attributes."""
@ -97,9 +111,16 @@ class intAttribute(Attribute):
return f"max_val must be an integer for attribute '{self.attrib_name}'."
if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val:
return f"min_val cannot be greater than max_val for attribute '{self.attrib_name}'."
if self.default_val is not None and not isinstance(self.default_val, int):
return f"default_val must be an integer for attribute '{self.attrib_name}'."
if self.step is not None:
if not isinstance(self.step, int):
return f"Step must be an integer for attribute '{self.attrib_name}'."
if self.step <= 0:
return f"Step must be a positive integer for attribute '{self.attrib_name}'."
if self.min_val is not None and self.max_val is not None and self.step > (self.max_val - self.min_val):
return f"Step value is too large for attribute '{self.attrib_name}'."
if self.default_val is not None:
if not isinstance(self.default_val, int):
return f"default_val must be an integer for attribute '{self.attrib_name}'."
if self.min_val is not None and self.default_val < self.min_val:
return f"default_val cannot be less than min_val for attribute '{self.attrib_name}'."
if self.max_val is not None and self.default_val > self.max_val:
@ -115,11 +136,13 @@ class floatAttribute(Attribute):
display_name: str,
min_val: Optional[float] = None,
max_val: Optional[float] = None,
step: float = 0.1,
**kwargs
):
super().__init__(attrib_name, display_name, html_input_type="number", **kwargs)
self.min_val = min_val
self.max_val = max_val
self.step = step
def validate(self) -> Optional[str]:
"""Validate float-specific attributes."""
@ -132,9 +155,16 @@ class floatAttribute(Attribute):
return f"max_val must be a number for attribute '{self.attrib_name}'."
if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val:
return f"min_val cannot be greater than max_val for attribute '{self.attrib_name}'."
if self.default_val is not None and not isinstance(self.default_val, (int, float)):
return f"default_val must be a number for attribute '{self.attrib_name}'."
if self.step is not None:
if not isinstance(self.step, float):
return f"Step must be an float for attribute '{self.attrib_name}'."
if self.step <= 0:
return f"Step must be a positive float for attribute '{self.attrib_name}'."
if self.min_val is not None and self.max_val is not None and self.step > (self.max_val - self.min_val):
return f"Step value is too large for attribute '{self.attrib_name}'."
if self.default_val is not None:
if not isinstance(self.default_val, float):
return f"default_val must be a number for attribute '{self.attrib_name}'."
if self.min_val is not None and self.default_val < self.min_val:
return f"default_val cannot be less than min_val for attribute '{self.attrib_name}'."
if self.max_val is not None and self.default_val > self.max_val:

View File

@ -38,6 +38,7 @@
{% if attrib.html_input_type == "number" %}
{% if attrib.min_val is not none %} min="{{ attrib.min_val }}" {% endif %}
{% if attrib.max_val is not none %} max="{{ attrib.max_val }}" {% endif %}
{% if attrib.step is not none %} step="{{ attrib.step }}" {% endif %}
{% endif %}
{% if attrib.html_input_type == "text" %}
{% if attrib.max_length is not none %} maxlength="{{ attrib.max_length }}" {% endif %}