diff --git a/basyx/aas/model/_string_constraints.py b/basyx/aas/model/_string_constraints.py index e407a825c..e56c25123 100644 --- a/basyx/aas/model/_string_constraints.py +++ b/basyx/aas/model/_string_constraints.py @@ -26,6 +26,14 @@ _T = TypeVar("_T") +AASD130_RE = re.compile("[\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]*") + + +def _unicode_escape(value: str) -> str: + """ + Escapes unicode characters such as \uD7FF, that may be used in regular expressions, for better error messages. + """ + return value.encode("unicode_escape").decode("utf-8") # Functions to verify the constraints for a given value. @@ -36,7 +44,16 @@ def check(value: str, type_name: str, min_length: int = 0, max_length: Optional[ if max_length is not None and len(value) > max_length: raise ValueError(f"{type_name} has a maximum length of {max_length}! (length: {len(value)})") if pattern is not None and not pattern.fullmatch(value): - raise ValueError(f"{type_name} must match the pattern '{pattern.pattern}'! (value: '{value}')") + raise ValueError(f"{type_name} must match the pattern '{_unicode_escape(pattern.pattern)}'! " + f"(value: '{_unicode_escape(value)}')") + # Constraint AASd-130: an attribute with data type "string" shall consist of these characters only: + if not AASD130_RE.fullmatch(value): + # It's easier to implement this as a ValueError, because otherwise AASConstraintViolation would need to be + # imported from `base` and the ConstrainedLangStringSet would need to except AASConstraintViolation errors + # as well, while only re-raising ValueErrors. Thus, even if an AASConstraintViolation would be raised here, + # in case of a ConstrainedLangStringSet it would be re-raised as a ValueError anyway. + raise ValueError(f"Every string must match the pattern '{_unicode_escape(AASD130_RE.pattern)}'! " + f"(value: '{_unicode_escape(value)}')") def check_content_type(value: str, type_name: str = "ContentType") -> None: diff --git a/test/model/test_string_constraints.py b/test/model/test_string_constraints.py index 33f7d6cf9..3b347ea2c 100644 --- a/test/model/test_string_constraints.py +++ b/test/model/test_string_constraints.py @@ -41,6 +41,25 @@ def test_version_type(self) -> None: version = "0" _string_constraints.check_version_type(version) + def test_aasd_130(self) -> None: + name: model.NameType = "\0" + with self.assertRaises(ValueError) as cm: + _string_constraints.check_name_type(name) + self.assertEqual(r"Every string must match the pattern '[\t\n\r -\ud7ff\ue000-\ufffd\U00010000-\U0010ffff]*'! " + r"(value: '\x00')", cm.exception.args[0]) + name = "\ud800" + with self.assertRaises(ValueError) as cm: + _string_constraints.check_name_type(name) + self.assertEqual(r"Every string must match the pattern '[\t\n\r -\ud7ff\ue000-\ufffd\U00010000-\U0010ffff]*'! " + r"(value: '\ud800')", cm.exception.args[0]) + name = "\ufffe" + with self.assertRaises(ValueError) as cm: + _string_constraints.check_name_type(name) + self.assertEqual(r"Every string must match the pattern '[\t\n\r -\ud7ff\ue000-\ufffd\U00010000-\U0010ffff]*'! " + r"(value: '\ufffe')", cm.exception.args[0]) + name = "this\ris\na\tvalid täst\uffdd\U0010ab12" + _string_constraints.check_name_type(name) + class StringConstraintsDecoratorTest(unittest.TestCase): @_string_constraints.constrain_path_type("some_attr")