isValidUUID method Null safety

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

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}) {
  if (fromByteList != null) {
    fromString = unparse(fromByteList);
  }
  // UUID of all 0s is ok.
  if (fromString == NAMESPACE_NIL) {
    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 its valid.
  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;
}