isValidOrThrow static method

void isValidOrThrow({
  1. String fromString = '',
  2. Uint8List? fromByteList,
  3. ValidationMode validationMode = ValidationMode.strictRFC4122,
  4. bool noDashes = false,
})

Validates the provided uuid to make sure it has all the necessary components and formatting and throws a FormatException if it is invalid. You can choose to validate from a string or from a byte list based on which parameter is passed. Optionally you can set validationMode to ValidationMode.nonStrict to allow for non RFC4122 compliant UUIDs. If you are using a Microsoft GUID, you should set validationMode to ValidationMode.nonStrict.

Implementation

static void isValidOrThrow(
    {String fromString = '',
    Uint8List? fromByteList,
    ValidationMode validationMode = ValidationMode.strictRFC4122,
    bool noDashes = false}) {
  final isValid = isValidUUID(
      fromString: fromString,
      fromByteList: fromByteList,
      validationMode: validationMode,
      noDashes: noDashes);

  if (!isValid) {
    // let's check if it is a non RFC4122 uuid and help the developer
    if (validationMode != ValidationMode.nonStrict) {
      final isValidNonStrict = isValidUUID(
          fromString: fromString,
          fromByteList: fromByteList,
          validationMode: ValidationMode.nonStrict,
          noDashes: noDashes);

      if (isValidNonStrict) {
        throw FormatException(
            'The provided UUID is not RFC4122 compliant. It seems you might be using a Microsoft GUID. Try setting `validationMode = ValidationMode.nonStrict`',
            fromString);
      }
    }

    throw FormatException('The provided UUID is invalid.', fromString);
  }
}