Fix #4874. Checking restrictions of the wrong data type will now returned a failed result instead of an exception.

This commit is contained in:
Dion Moult
2024-06-20 13:22:16 +10:00
parent fbefe8c555
commit b87a6bc15c
+33 -30
View File
@@ -1025,37 +1025,40 @@ class Restriction:
if other is None:
return False
for constraint, value in self.options.items():
if constraint == "enumeration":
if other not in [cast_to_value(v, other) for v in value]:
return False
elif constraint == "pattern":
if not isinstance(other, str):
return False
value = value if isinstance(value, list) else [value]
for pattern in value:
if re.compile(identities.translate_pattern(pattern)).fullmatch(other) is None:
try:
if constraint == "enumeration":
if other not in [cast_to_value(v, other) for v in value]:
return False
elif constraint == "length":
if len(str(other)) != int(value):
return False
elif constraint == "maxLength":
if len(str(other)) > int(value):
return False
elif constraint == "minLength":
if len(str(other)) < int(value):
return False
elif constraint == "maxExclusive":
if float(other) >= float(value):
return False
elif constraint == "maxInclusive":
if float(other) > float(value):
return False
elif constraint == "minExclusive":
if float(other) <= float(value):
return False
elif constraint == "minInclusive":
if float(other) < float(value):
return False
elif constraint == "pattern":
if not isinstance(other, str):
return False
value = value if isinstance(value, list) else [value]
for pattern in value:
if re.compile(identities.translate_pattern(pattern)).fullmatch(other) is None:
return False
elif constraint == "length":
if len(str(other)) != int(value):
return False
elif constraint == "maxLength":
if len(str(other)) > int(value):
return False
elif constraint == "minLength":
if len(str(other)) < int(value):
return False
elif constraint == "maxExclusive":
if float(other) >= float(value):
return False
elif constraint == "maxInclusive":
if float(other) > float(value):
return False
elif constraint == "minExclusive":
if float(other) <= float(value):
return False
elif constraint == "minInclusive":
if float(other) < float(value):
return False
except ValueError:
return False
return True
def __str__(self):