Repository navigation
Add UUIDValidator - #153
winniehell wants to merge 1 commit into
Conversation
binaryDiv
left a comment
There was a problem hiding this comment.
For completeness sake, please also:
- Add the validator to the markdown documentation in
docs. (In the long run, the validator docs should be generated from the doc strings, but we don't have that yet.) - Add a typing test to
tests/mypy/, see this example, which is basically the same with different types: https://github.com/binary-butterfly/validataclass/blob/main/tests/mypy/validators/test_date_validator.yml
There was a problem hiding this comment.
This file is missing the copyright header.
There was a problem hiding this comment.
This file is also missing the copyright header.
| 'UUIDValidator', | ||
| ] | ||
|
|
||
| from ..exceptions.uuid_exceptions import InvalidUUIDError |
There was a problem hiding this comment.
Please sort the imports. The __all__ doesn't belong between imports. Also, please use absolute imports for the exception (basically, everything that's not in the same package should use an absolute import). You can look at other validator files for examples.
| """ | ||
| code = 'invalid_uuid' | ||
|
|
||
| def __init__(self, **kwargs: Any): |
There was a problem hiding this comment.
The init method is redundant, it doesn't do anything other than passing arguments to the init method of the super class. You can remove it.
| input_str = str(input_data) | ||
|
|
||
| try: | ||
| return UUID(input_str) |
There was a problem hiding this comment.
This accepts UUIDs in a variety of different formats, not just the dash-separated groups of hexadecimal numbers. For example, these are all valid too:
UUID('{12345678-1234-5678-1234-567812345678}') # curly braces
UUID('12345678123456781234567812345678') # no dashes
UUID('urn:uuid:12345678-1234-5678-1234-567812345678') # URN prefixAdditionally, the dashes are not just optional, there can be any number of dashes in any place (e.g. ---1-234---56-78-1234-5678-12345-678-1234-567-8- is considered valid).
See uuid docs: https://docs.python.org/3/library/uuid.html#uuid.UUID
The question is, should these all be accepted? If yes, then these formats should be included in the unit tests. If not, then the code needs to be extended and there should be negative tests for these cases. If "it depends on the use case", then there should be a option in the validator for it.
In any case: UUIDs are not completely trivial, so the documentation of the validator should specify which formats it accepts.
| 'input_data', | ||
| [ | ||
| '', | ||
| 'unit test apple', |
There was a problem hiding this comment.
There should be more test cases for invalid strings: Strings that look like UUIDs in various aspects, but are not valid UUIDs.
For example:
- Characters that are not allowed in UUIDs. (Letters that aren't hex digits, special characters that aren't valid delimiters)
- Hexadecimal strings that don't have the correct length (too short, to long)
And, depending on the answer to my other comment about different formats currently all being accepted, maybe more test cases.
| def test_valid_string(input_data): | ||
| """ Test UUIDValidator with valid strings. """ | ||
| validator = UUIDValidator() | ||
| assert str(validator.validate(input_data)) == input_data |
There was a problem hiding this comment.
Please also test that the validator returns a UUID object and not a string.
This pull request adds a new UUIDValidator that validates UUIDs. I need it because I want to validate UUIDs.