Merged validation of input type=number

This commit is contained in:
Candifloss 2025-03-15 11:27:13 +05:30
parent b774427e9c
commit 7714d64d6b

View File

@ -17,7 +17,7 @@ def create_dynamic_schema() -> Type[Schema]:
def validate_allowed_chars(value: str, attrib=attrib) -> None:
if attrib.allowed_chars and not all(char in attrib.allowed_chars for char in value):
raise ValidationError(f"Invalid characters in {attrib.display_name}. Allowed characters are: {attrib.allowed_chars}")
schema_fields[attrib.attrib_name] = fields.String(
required=attrib.required,
validate=[
@ -25,6 +25,7 @@ def create_dynamic_schema() -> Type[Schema]:
lambda x, attrib=attrib: len(x) >= attrib.min_length if attrib.min_length else True,
lambda x, attrib=attrib: re.match(attrib.regex, x) if attrib.regex else True,
validate_allowed_chars,
#validate compareto,
],
error_messages={
"required": f"{attrib.display_name} is required.",
@ -32,21 +33,11 @@ def create_dynamic_schema() -> Type[Schema]:
},
)
elif isinstance(attrib, intAttribute):
schema_fields[attrib.attrib_name] = fields.Integer(
required=attrib.required,
validate=[
lambda x, attrib=attrib: x >= attrib.min_val if attrib.min_val is not None else True,
lambda x, attrib=attrib: x <= attrib.max_val if attrib.max_val is not None else True,
],
error_messages={
"required": f"{attrib.display_name} is required.",
"validator_failed": f"Invalid value for {attrib.display_name}.",
},
)
elif isinstance(attrib, (intAttribute, floatAttribute)):
# Determine the field type based on the attribute class
field_type = fields.Integer if isinstance(attrib, intAttribute) else fields.Float
elif isinstance(attrib, floatAttribute):
schema_fields[attrib.attrib_name] = fields.Float(
schema_fields[attrib.attrib_name] = field_type(
required=attrib.required,
validate=[
lambda x, attrib=attrib: x >= attrib.min_val if attrib.min_val is not None else True,