Skip to content

Commit

Permalink
Make string regex compatible with AsyncAPI renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
febus982 committed Oct 6, 2024
1 parent 1487a66 commit 7fc0934
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
This is an implementation of the [CloudEvents spec](https://github.com/cloudevents/spec/tree/main) using
[Pydantic V2](https://docs.pydantic.dev/latest/) for high performance during validation and serialization.

It is meant to support natively [FastAPI](https://fastapi.tiangolo.com/)
and [FastStream](https://faststream.airt.ai/latest/) (WIP)

Currently supported bindings:

| Binding | Format | Single | Batch |
Expand Down
24 changes: 16 additions & 8 deletions cloudevents_pydantic/events/fields/types/_canonic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ def generic_uri_validator(value: str) -> ParseResult:
(malformed surrogate characters) but pydantic is unhappy to handle them anyway
so we can avoid the scenario in the regex and make this faster.
"""
str_constraint = (
r"^"
r"[^"
r"\u0000-\u001F\u007F-\u009F"
r"\uFDD0-\uFDEF\uFFFE\uFFFF"
class_control = r"\u0000-\u001F\u007F-\u009F"
class_nonchar_utf16_range = r"\uFDD0-\uFDEF\uFFFE\uFFFF"
class_nonchar_utf32_range = (
r"\u{1FFFE}\u{1FFFF}"
r"\u{2FFFE}\u{2FFFF}"
r"\u{3FFFE}\u{3FFFF}"
Expand All @@ -104,11 +102,21 @@ def generic_uri_validator(value: str) -> ParseResult:
r"\u{EFFFE}\u{EFFFF}"
r"\u{FFFFE}\u{FFFFF}"
r"\u{10FFFE}\u{10FFFF}"
r"]+"
r""
)
str_constraint = (
r"^"
r"[^"
+ class_control
+ class_nonchar_utf16_range
+ class_nonchar_utf32_range
+ r"]+"
r"$"
)

str_constraint_asyncapi_compat = (
r"^" r"[^" + class_control + class_nonchar_utf16_range + r"]+" r"$"
)


# TODO: Add types docstrings
Boolean = Annotated[bool, PlainSerializer(bool_serializer)]
Expand All @@ -121,7 +129,7 @@ def generic_uri_validator(value: str) -> ParseResult:
A whole number in the range -2,147,483,648 to +2,147,483,647 inclusive
"""

String = Annotated[str, StringConstraints(pattern=str_constraint)]
String = Annotated[str, StringConstraints(pattern=str_constraint_asyncapi_compat)]
"""
Sequence of allowable Unicode characters
"""
Expand Down
66 changes: 34 additions & 32 deletions tests/events/test_field_types_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,38 +144,40 @@ class StrModel(BaseModel):
*list(map(chr, range(ord("\ufdd0"), ord("\ufdef") + 1))),
"\ufffe",
"\uffff",
"\U0001fffe",
"\U0001ffff",
"\U0002fffe",
"\U0002ffff",
"\U0003fffe",
"\U0003ffff",
"\U0004fffe",
"\U0004ffff",
"\U0005fffe",
"\U0005ffff",
"\U0006fffe",
"\U0006ffff",
"\U0007fffe",
"\U0007ffff",
"\U0008fffe",
"\U0008ffff",
"\U0009fffe",
"\U0009ffff",
"\U000afffe",
"\U000affff",
"\U000bfffe",
"\U000bffff",
"\U000cfffe",
"\U000cffff",
"\U000dfffe",
"\U000dffff",
"\U000efffe",
"\U000effff",
"\U000ffffe",
"\U000fffff",
"\U0010fffe",
"\U0010ffff",
# These are to be enabled when AsyncAPI regex issue is fixed
# https://github.com/asyncapi/asyncapi-react/issues/1071
# "\U0001fffe",
# "\U0001ffff",
# "\U0002fffe",
# "\U0002ffff",
# "\U0003fffe",
# "\U0003ffff",
# "\U0004fffe",
# "\U0004ffff",
# "\U0005fffe",
# "\U0005ffff",
# "\U0006fffe",
# "\U0006ffff",
# "\U0007fffe",
# "\U0007ffff",
# "\U0008fffe",
# "\U0008ffff",
# "\U0009fffe",
# "\U0009ffff",
# "\U000afffe",
# "\U000affff",
# "\U000bfffe",
# "\U000bffff",
# "\U000cfffe",
# "\U000cffff",
# "\U000dfffe",
# "\U000dffff",
# "\U000efffe",
# "\U000effff",
# "\U000ffffe",
# "\U000fffff",
# "\U0010fffe",
# "\U0010ffff",
],
)
def test_string_fails_on_unicode_noncharacters(unicode_noncharacter):
Expand Down

0 comments on commit 7fc0934

Please sign in to comment.