Merged validation of input type=number
This commit is contained in:
parent
b774427e9c
commit
7714d64d6b
@ -17,7 +17,7 @@ def create_dynamic_schema() -> Type[Schema]:
|
|||||||
def validate_allowed_chars(value: str, attrib=attrib) -> None:
|
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):
|
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}")
|
raise ValidationError(f"Invalid characters in {attrib.display_name}. Allowed characters are: {attrib.allowed_chars}")
|
||||||
|
|
||||||
schema_fields[attrib.attrib_name] = fields.String(
|
schema_fields[attrib.attrib_name] = fields.String(
|
||||||
required=attrib.required,
|
required=attrib.required,
|
||||||
validate=[
|
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: 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,
|
lambda x, attrib=attrib: re.match(attrib.regex, x) if attrib.regex else True,
|
||||||
validate_allowed_chars,
|
validate_allowed_chars,
|
||||||
|
#validate compareto,
|
||||||
],
|
],
|
||||||
error_messages={
|
error_messages={
|
||||||
"required": f"{attrib.display_name} is required.",
|
"required": f"{attrib.display_name} is required.",
|
||||||
@ -32,21 +33,11 @@ def create_dynamic_schema() -> Type[Schema]:
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
elif isinstance(attrib, intAttribute):
|
elif isinstance(attrib, (intAttribute, floatAttribute)):
|
||||||
schema_fields[attrib.attrib_name] = fields.Integer(
|
# Determine the field type based on the attribute class
|
||||||
required=attrib.required,
|
field_type = fields.Integer if isinstance(attrib, intAttribute) else fields.Float
|
||||||
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, floatAttribute):
|
schema_fields[attrib.attrib_name] = field_type(
|
||||||
schema_fields[attrib.attrib_name] = fields.Float(
|
|
||||||
required=attrib.required,
|
required=attrib.required,
|
||||||
validate=[
|
validate=[
|
||||||
lambda x, attrib=attrib: x >= attrib.min_val if attrib.min_val is not None else True,
|
lambda x, attrib=attrib: x >= attrib.min_val if attrib.min_val is not None else True,
|
||||||
|
Loading…
Reference in New Issue
Block a user