Skip to content
Sarj Code Standards

prefer-validated-uuid-comparison

Validate UUID identifiers at input boundaries before comparing them.

Why

Reparsing both operands while comparing identifiers moves input validation into business logic, repeats normalization and can raise on malformed input after the boundary has accepted it.

Fix

Parse and normalize UUIDs in the router's request schema or ingestion adapter, use the same nominal UUID-backed ID in domain and store contracts, then compare the typed values directly. Preserve canonicalization of existing string-backed data before removing constructors.

Examples

Before — flagged Both identifiers are reparsed at comparison time
app/router.py
from uuid import UUID
def same_owner(item, request):
return UUID(item.owner_id) == UUID(request.owner_id)
After — preferred Compare the validated nominal identifiers
app/router.py
from uuid import UUID
from typing import NewType
OwnerId = NewType("OwnerId", UUID)
def same_owner(current: OwnerId, requested: OwnerId) -> bool:
return current == requested