isUUID static method
Validates if the given string is a valid UUID.
Supports both UUID formats:
- With hyphens (36 chars):
123e4567-e89b-12d3-a456-426614174000 - Without hyphens (32 chars):
123e4567e89b12d3a456426614174000
The validation checks:
- Correct length (32 or 36 characters)
- Valid hexadecimal characters only
- Valid UUID version (1-5) in the version nibble
- Valid variant bits (8, 9, a, or b) in the variant nibble
Returns false if:
- The input is null or empty
- The length is not 32 or 36 characters
- The format doesn't match UUID specifications
Example:
UuidUtils.isUUID('123e4567-e89b-12d3-a456-426614174000'); // true (v1 UUID)
UuidUtils.isUUID('550e8400-e29b-41d4-a716-446655440000'); // true (v4 UUID)
UuidUtils.isUUID('123e4567e89b12d3a456426614174000'); // true (no hyphens)
UuidUtils.isUUID(''); // false
UuidUtils.isUUID('not-a-uuid'); // false
UuidUtils.isUUID('123e4567-e89b-62d3-a456-426614174000'); // false (invalid version 6)
Implementation
static bool isUUID(String? uuid) {
if (uuid == null || uuid.isEmpty) {
return false;
}
// Check length: must be 32 (no hyphens) or 36 (with hyphens)
if (uuid.length != _uuidLengthWithoutHyphens && uuid.length != _uuidLengthWithHyphens) {
return false;
}
// Use appropriate regex based on length
if (uuid.length == _uuidLengthWithHyphens) {
return _uuidWithHyphensRegex.hasMatch(uuid);
}
return _uuidWithoutHyphensRegex.hasMatch(uuid);
}