Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model._string_constraints: implement constraint AASd-130 #135

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion basyx/aas/model/_string_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
if not AASD130_RE.fullmatch(value):
s-heppner marked this conversation as resolved.
Show resolved Hide resolved
# 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:
Expand Down
19 changes: 19 additions & 0 deletions test/model/test_string_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading