43 lines
1.9 KiB
Python
43 lines
1.9 KiB
Python
import re
|
|
from datetime import datetime
|
|
|
|
# Validate the input value based on the attribute's constraints
|
|
def validate_value(attrib, input_val):
|
|
# Check if the value is required
|
|
if attrib.required and not input_val:
|
|
return f"{attrib.display_name} is required."
|
|
|
|
# Validate based on input type
|
|
if attrib.html_input_type == "number":
|
|
try:
|
|
input_val = int(input_val)
|
|
except ValueError:
|
|
return f"{attrib.display_name} must be a valid number."
|
|
|
|
if attrib.min is not None and input_val < attrib.min:
|
|
return f"{attrib.display_name} must be at least {attrib.min}."
|
|
if attrib.max is not None and input_val > attrib.max:
|
|
return f"{attrib.display_name} must be at most {attrib.max}."
|
|
elif attrib.html_input_type == "date":
|
|
try:
|
|
input_val = datetime.strptime(input_val, "%Y-%m-%d") # Validate date format
|
|
if attrib.min is not None and input_val < datetime.strptime(attrib.min, "%Y-%m-%d"):
|
|
return f"{attrib.display_name} must be on or after {attrib.min}."
|
|
if attrib.max is not None and input_val > datetime.strptime(attrib.max, "%Y-%m-%d"):
|
|
return f"{attrib.display_name} must be on or before {attrib.max}."
|
|
except:
|
|
return f"{attrib.display_name} must be a valid date in YYYY-MM-DD format."
|
|
elif attrib.html_input_type == "select":
|
|
if input_val not in attrib.options:
|
|
return f"{attrib.display_name} must be one of {attrib.options}."
|
|
elif attrib.html_input_type == "text":
|
|
if attrib.regex and not re.match(attrib.regex, input_val):
|
|
return f"{attrib.display_name} is invalid. Allowed pattern: {attrib.regex}."
|
|
|
|
# Validate comparison
|
|
#if attrib.compareto:
|
|
# compare attrib value
|
|
#return ""
|
|
|
|
# If all checks pass, return "Ok"
|
|
return "Ok" |