"Allowed chars" validation for textAttribute
This commit is contained in:
parent
8e807fa52c
commit
b774427e9c
@ -13,12 +13,18 @@ def create_dynamic_schema() -> Type[Schema]:
|
|||||||
|
|
||||||
for attrib in item_attributes:
|
for attrib in item_attributes:
|
||||||
if isinstance(attrib, textAttribute):
|
if isinstance(attrib, textAttribute):
|
||||||
|
# Allowed chars
|
||||||
|
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(
|
schema_fields[attrib.attrib_name] = fields.String(
|
||||||
required=attrib.required,
|
required=attrib.required,
|
||||||
validate=[
|
validate=[
|
||||||
lambda x, attrib=attrib: len(x) <= attrib.max_length if attrib.max_length else True,
|
lambda x, attrib=attrib: len(x) <= attrib.max_length if attrib.max_length else True,
|
||||||
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,
|
||||||
],
|
],
|
||||||
error_messages={
|
error_messages={
|
||||||
"required": f"{attrib.display_name} is required.",
|
"required": f"{attrib.display_name} is required.",
|
||||||
|
Loading…
Reference in New Issue
Block a user