Skip to content

Add UUIDValidator - #153

Open
winniehell wants to merge 1 commit into
mainfrom
uuid-validator
Open

winniehell wants to merge 1 commit into
mainfrom
uuid-validator

Conversation

@winniehell

@winniehell winniehell commented Nov 9, 2026 •

Copy link
Copy Markdown

This pull request adds a new UUIDValidator that validates UUIDs. I need it because I want to validate UUIDs.

@binaryDiv binaryDiv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness sake, please also:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is missing the copyright header.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is also missing the copyright header.

'UUIDValidator',
]

from ..exceptions.uuid_exceptions import InvalidUUIDError

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 prefix

Additionally, 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',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also test that the validator returns a UUID object and not a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants