isValidUUID static method

bool isValidUUID({
  1. String fromString = '',
  2. Uint8List? fromByteList,
  3. ValidationMode validationMode = ValidationMode.strictRFC4122,
})

Validates the provided uuid to make sure it has all the necessary components and formatting and returns a bool You can choose to validate from a string or from a byte list based on which parameter is passed.

Implementation

static bool isValidUUID(
    {String fromString = '', Uint8List? fromByteList, ValidationMode validationMode = ValidationMode.strictRFC4122}) {
  if (fromByteList != null) {
    fromString = unparse(fromByteList);
  }
  // UUID of all 0s is ok.
  if (fromString == namespaceNil) {
    return true;
  }

  // If its not 36 characters in length, don't bother (including dashes).
  if (fromString.length != 36) {
    return false;
  }

  // Make sure if it passes the above, that it's a valid UUID or GUID.
  switch (validationMode) {
    case ValidationMode.strictRFC4122:
      {
        const pattern = r'^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$';
        final regex = RegExp(pattern, caseSensitive: false, multiLine: true);
        final match = regex.hasMatch(fromString);
        return match;
      }
    case ValidationMode.nonStrict:
      {
        const pattern = r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$';
        final regex = RegExp(pattern, caseSensitive: false, multiLine: true);
        final match = regex.hasMatch(fromString);
        return match;
      }
    default:
      {
        throw Exception('`$validationMode` is an invalid ValidationMode.');
      }
  }
}