in connectors/source.py [0:0]
def validate(self):
"""Used to validate the `value` of a Field using its `validations`.
If `value` is empty and the field is not required,
the validation is run on the `default_value` instead.
Returns a list of errors as strings.
If the list is empty, then the Field `value` is valid.
"""
value = self.value
label = self.label
validation_errors = []
for validation in self.validations:
validation_type = validation["type"]
constraint = validation["constraint"]
match validation_type:
case ValidationTypes.LESS_THAN.value:
if value < constraint:
# valid
continue
else:
validation_errors.append(
f"'{label}' value '{value}' should be less than {constraint}."
)
case ValidationTypes.GREATER_THAN.value:
if value > constraint:
# valid
continue
else:
validation_errors.append(
f"'{label}' value '{value}' should be greater than {constraint}."
)
case ValidationTypes.LIST_TYPE.value:
if not isinstance(value, list):
validation_errors.append(
f"Cannot list_type validate '{label}' because its value '{value}' is not a list."
)
continue
for item in value:
if (constraint == "str" and not isinstance(item, str)) or (
constraint == "int" and not isinstance(item, int)
):
validation_errors.append(
f"'{label}' list value '{item}' should be of type {constraint}."
)
case ValidationTypes.INCLUDED_IN.value:
if isinstance(value, list):
for item in value:
if item not in constraint:
validation_errors.append(
f"'{label}' list value '{item}' should be one of {', '.join(str(x) for x in constraint)}."
)
else:
if value not in constraint:
validation_errors.append(
f"'{label}' list value '{value}' should be one of {', '.join(str(x) for x in constraint)}."
)
case ValidationTypes.REGEX.value:
if not isinstance(value, str):
validation_errors.append(
f"Cannot regex validate '{label}' because '{value}' is not a string."
)
continue
if not re.fullmatch(constraint, value):
validation_errors.append(
f"'{label}' value '{value}' failed regex check {constraint}."
)
return validation_errors