Skip to content

uuid() accepts nil, v1, and max UUIDs despite documenting UUID-v4 validation #600

Backlog

Description

@SonicHedghog

uuid() is documented as validating a UUID-v4, but it currently accepts UUIDs of any version, including the nil and max UUIDs.

Repro

With validators 0.35.0:

>>> import validators
>>> validators.uuid("00000000-0000-0000-0000-000000000000")  # nil UUID
True
>>> validators.uuid("2bc1c94f-0deb-13e9-92a1-4775189ec9f8")  # version 1
True
>>> validators.uuid("2bc1c94f-0deb-43e9-92a1-4775189ec9f8")  # version 4
True
>>> validators.uuid("ffffffff-ffff-ffff-ffff-ffffffffffff")  # max UUID
True

UUID objects are also accepted without checking their version:

>>> import uuid as uuidlib
>>> validators.uuid(uuidlib.uuid1())
True

Expected behavior

If uuid() is intended to be UUID-v4-specific, only version-4 values should return True; nil, v1, and other versions should return ValidationError.

Actual behavior

Every value that uuid.UUID(...) can parse returns True. For string input this happens because the UUID(value) object is truthy, so the version is never checked. For UUID objects, the function returns True immediately.

Why this matters

Callers may use this validator specifically to enforce random v4 identifiers. Accepting v1 UUIDs can admit timestamp/node-based identifiers, while nil and max UUIDs are sentinel values rather than normal generated identifiers.

Possible resolution

Either:

  1. enforce parsed_uuid.version == 4 for both string and UUID inputs, or
  2. if accepting all UUID versions is intentional, update the documentation so callers do not rely on v4-only validation.

I would be happy to implement whichever direction you prefer, with regression tests.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions