From e92a90e8fce243e495b3b0d75c69c1713d9ace71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Thu, 5 Oct 2023 19:07:44 +0200 Subject: [PATCH 1/4] model._string_constraints: add AASd-130 to `check()` Fix #118 --- basyx/aas/model/_string_constraints.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/basyx/aas/model/_string_constraints.py b/basyx/aas/model/_string_constraints.py index e407a825c..4973388e8 100644 --- a/basyx/aas/model/_string_constraints.py +++ b/basyx/aas/model/_string_constraints.py @@ -26,6 +26,7 @@ _T = TypeVar("_T") +AASD130_RE = re.compile("[\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]*") # Functions to verify the constraints for a given value. @@ -37,6 +38,13 @@ def check(value: str, type_name: str, min_length: int = 0, max_length: Optional[ 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}')") + # Constraint AASd-130 + 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 '{AASD130_RE.pattern}'! (value: '{value}')") def check_content_type(value: str, type_name: str = "ContentType") -> None: From d56c612ad6b18f66c8d24697a11a893fabacf8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Thu, 5 Oct 2023 18:56:56 +0200 Subject: [PATCH 2/4] model._string_constraints: escape unicode characters in errors Escape unicode characters in regular expression patterns and string values for clean error messages. --- basyx/aas/model/_string_constraints.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/basyx/aas/model/_string_constraints.py b/basyx/aas/model/_string_constraints.py index 4973388e8..5282cd5b6 100644 --- a/basyx/aas/model/_string_constraints.py +++ b/basyx/aas/model/_string_constraints.py @@ -29,6 +29,13 @@ 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. def check(value: str, type_name: str, min_length: int = 0, max_length: Optional[int] = None, pattern: Optional[re.Pattern] = None) -> None: @@ -37,14 +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 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 '{AASD130_RE.pattern}'! (value: '{value}')") + 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: From 76611238adbc7ab7a5550c3fefe6839b24b0fe14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Thu, 5 Oct 2023 19:00:29 +0200 Subject: [PATCH 3/4] test: check constraint AASd-130 implementation --- test/model/test_string_constraints.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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") From ebc518cd9059c0b08f05176c7a0f6abcf4e01b50 Mon Sep 17 00:00:00 2001 From: s-heppner Date: Fri, 6 Oct 2023 08:49:29 +0200 Subject: [PATCH 4/4] model._string_constraints: Add documentation what AASd-130 is --- basyx/aas/model/_string_constraints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basyx/aas/model/_string_constraints.py b/basyx/aas/model/_string_constraints.py index 5282cd5b6..e56c25123 100644 --- a/basyx/aas/model/_string_constraints.py +++ b/basyx/aas/model/_string_constraints.py @@ -46,7 +46,7 @@ def check(value: str, type_name: str, min_length: int = 0, max_length: Optional[ if pattern is not None and not pattern.fullmatch(value): raise ValueError(f"{type_name} must match the pattern '{_unicode_escape(pattern.pattern)}'! " f"(value: '{_unicode_escape(value)}')") - # Constraint AASd-130 + # 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