-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from atproto_client.models import string_formats | ||
from pydantic import TypeAdapter, ValidationError | ||
|
||
some_good_handle = 'test.bsky.social' | ||
some_bad_handle = 'invalid@ @handle' | ||
|
||
strict_validation_context = {'strict_string_format': True} | ||
HandleTypeAdapter = TypeAdapter(string_formats.Handle) | ||
|
||
assert string_formats._OPT_IN_KEY == 'strict_string_format' # noqa: S101 | ||
|
||
# values will not be validated if not opting in | ||
sneaky_bad_handle = HandleTypeAdapter.validate_python(some_bad_handle) | ||
|
||
assert sneaky_bad_handle == some_bad_handle # noqa: S101 | ||
|
||
print(f'{sneaky_bad_handle=}\n\n') | ||
|
||
# values will be validated if opting in | ||
validated_good_handle = HandleTypeAdapter.validate_python(some_good_handle, context=strict_validation_context) | ||
|
||
assert validated_good_handle == some_good_handle # noqa: S101 | ||
|
||
print(f'{validated_good_handle=}\n\n') | ||
|
||
try: | ||
print('Trying to validate a bad handle with strict validation...') | ||
HandleTypeAdapter.validate_python(some_bad_handle, context=strict_validation_context) | ||
except ValidationError as e: | ||
print(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters