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

UUIDs created with ULID don't specify RFC_4122 variant #5

Closed
osintalex opened this issue Jan 10, 2023 · 3 comments
Closed

UUIDs created with ULID don't specify RFC_4122 variant #5

osintalex opened this issue Jan 10, 2023 · 3 comments

Comments

@osintalex
Copy link

I'm creating UUIDs with ULID for use in an application. As part of some data validation libraries in that application, these UUIDs must be RFC_4122 compliant.

However, some of the UUIDs created with ULID fail this check:

import uuid
from ulid import ULID


ulid = ULID.from_str('01GJMN8DEMHMCJKXM3MADM3J45')
uuid_value = ulid.to_uuid()
print(uuid_value.variant == uuid.RFC_4122)  # False
for uuid_created_with_uuid in (uuid.uuid1(), uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'), uuid.uuid4(),
                               uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')):
    print(uuid_created_with_uuid.variant == uuid.RFC_4122)  # True for all of them

This bit uuid_value.variant == uuid.RFC_4122 is what the data validation library does to check that a UUID is RFC_4122 compliant. It would be great to have that supported with ULID if possible.

@mdomke
Copy link
Owner

mdomke commented Sep 20, 2023

Sadly, I think this is not possible without changing some of the basic constraints of this library. The ULID and the UUID both consist of a 128 bit-sequence, but the layout and interpretation of the underlying structure is very different. When converting a ULID to a UUID we could obviously set the bits of the UUID variant field accordingly, so that it complies with the RFC. This would however make it impossible to do the reverse operation and convert the UUID back to the same ULID.

ulid = ULID()
assert ulid == ULID.from_uuid(ulid.to_uuid())  # This would fail

This being said, I think there is no requirement from the ULID spec side that would hinder us from converting a ULID into a valid, let's say, UUID4.

@mdomke
Copy link
Owner

mdomke commented Sep 21, 2023

Release 2.1.0 includes a method ULID.to_uuid4 which allows you to create an RFC 4122 compliant UUIDv4 from a given ULID.

@mdomke mdomke closed this as completed Sep 21, 2023
@osintalex
Copy link
Author

Thank you!!

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

No branches or pull requests

2 participants