Skip to content

Commit

Permalink
Fixes and new validation setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
daegalus committed Dec 12, 2023
1 parent 1898f8d commit 56aa462
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

v4.2.2

* Fix CryptoRNG on Web generating a random number 0 always
* Add NoDashes support to the validator so that it validates UUIDs that don't have dashes but are otherwise valid.

v4.2.1

* Lower `meta` dependency to 1.9.1 so that it is compatible with Flutter Stable 3.13
Expand Down
2 changes: 1 addition & 1 deletion lib/rng.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CryptoRNG extends RNG {
final b = Uint8List(16);

for (var i = 0; i < 16; i += 4) {
var k = _secureRandom.nextInt(1 << 32);
var k = _secureRandom.nextInt(pow(2, 32).toInt());
b[i] = k;
b[i + 1] = k >> 8;
b[i + 2] = k >> 16;
Expand Down
6 changes: 4 additions & 2 deletions lib/uuid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ class Uuid {
static bool isValidUUID(
{String fromString = '',
Uint8List? fromByteList,
ValidationMode validationMode = ValidationMode.strictRFC4122}) {
ValidationMode validationMode = ValidationMode.strictRFC4122,
bool noDashes = false}) {
return UuidValidation.isValidUUID(
fromString: fromString,
fromByteList: fromByteList,
validationMode: validationMode);
validationMode: validationMode,
noDashes: noDashes);
}

/// Generates a time-based version 1 UUID
Expand Down
29 changes: 20 additions & 9 deletions lib/validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class UuidValidation {
static bool isValidUUID(
{String fromString = '',
Uint8List? fromByteList,
ValidationMode validationMode = ValidationMode.strictRFC4122}) {
ValidationMode validationMode = ValidationMode.strictRFC4122,
bool noDashes = false}) {
if (fromByteList != null) {
fromString = UuidParsing.unparse(fromByteList);
}
Expand All @@ -20,24 +21,31 @@ class UuidValidation {
}

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

// If its not 32 characters in length, don't bother (excluding excluding).
if (noDashes && fromString.length != 32) {
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}-[0-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$';
var pattern = (noDashes)
? r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-8][0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$'
: r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-8][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.toLowerCase());
return match;
}
case ValidationMode.nonStrict:
{
const pattern =
r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-8][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$';
var pattern = (noDashes)
? r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-8][0-9a-f]{3}-?[0-9a-f]{4}-?[0-9a-f]{12}$'
: r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-8][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.toLowerCase());
return match;
Expand All @@ -60,19 +68,22 @@ class UuidValidation {
static void isValidOrThrow(
{String fromString = '',
Uint8List? fromByteList,
ValidationMode validationMode = ValidationMode.strictRFC4122}) {
ValidationMode validationMode = ValidationMode.strictRFC4122,
bool noDashes = false}) {
final isValid = isValidUUID(
fromString: fromString,
fromByteList: fromByteList,
validationMode: validationMode);
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);
validationMode: ValidationMode.nonStrict,
noDashes: noDashes);

if (isValidNonStrict) {
throw FormatException(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: uuid
version: 4.2.1
version: 4.2.2
description: >
RFC4122 (v1, v4, v5, v6, v7, v8) UUID Generator and Parser for Dart
documentation: https://daegalus.github.io/dart-uuid/index.html
Expand Down
14 changes: 14 additions & 0 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,20 @@ void main() {
});
});

group('[Validation Test]', () {
test('Dashes UUID', () {
const validUUID = '87cd4eb3-cb88-449b-a1da-e468fd829310';
expect(Uuid.isValidUUID(fromString: validUUID, noDashes: true), false);
expect(Uuid.isValidUUID(fromString: validUUID), true);
});
test('No Dashes UUID', () {
const validNoDashesUUID = '87cd4eb3cb88449ba1dae468fd829310';
expect(Uuid.isValidUUID(fromString: validNoDashesUUID, noDashes: true),
true);
expect(Uuid.isValidUUID(fromString: validNoDashesUUID), false);
});
});

group('[Test Vectors]', () {
group('[UUID6]', () {
for (final testCase in {
Expand Down

0 comments on commit 56aa462

Please sign in to comment.