Skip to content

Commit

Permalink
Merge pull request #406 from frankebersoll/identityvalidation
Browse files Browse the repository at this point in the history
Identity prefix validation including dash
  • Loading branch information
rasmus authored Nov 2, 2017
2 parents 48e9edb + e601612 commit fe56dab
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* New: Support for unicode characters in type names. This simplifies using an
[ubiquitous language](http://www.jamesshore.com/Agile-Book/ubiquitous_language.html)
in non-english domains
* Fixed: Include hyphen in prefix validation for identity values. This fixes a bug
where invalid identities could be created (e.g. `ThingyId.With("thingyINVALID-a41e...")`)

### New in 0.51.3155 (released 2017-10-25)

Expand Down
1 change: 1 addition & 0 deletions Source/EventFlow.Tests/UnitTests/Core/IdentityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public void NewDeterministic_IsValid()
}

[TestCase("da7ab6b1-c513-581f-a1a0-7cdf17109deb")]
[TestCase("thingyid-da7ab6b1-c513-581f-a1a0-7cdf17109deb")]
[TestCase("thingy-769077C6-F84D-46E3-AD2E-828A576AAAF3")]
[TestCase("thingy-pppppppp-pppp-pppp-pppp-pppppppppppp")]
[TestCase("funny-da7ab6b1-c513-581f-a1a0-7cdf17109deb")]
Expand Down
12 changes: 6 additions & 6 deletions Source/EventFlow/Core/Identity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ public abstract class Identity<T> : SingleValueObject<string>, IIdentity
where T : Identity<T>
{
// ReSharper disable StaticMemberInGenericType
private static readonly string Name;
private static readonly string NameWithDash;
private static readonly Regex ValueValidation;
// ReSharper enable StaticMemberInGenericType

static Identity()
{
var nameReplace = new Regex("Id$");
Name = nameReplace.Replace(typeof(T).Name, string.Empty).ToLowerInvariant();
NameWithDash = nameReplace.Replace(typeof(T).Name, string.Empty).ToLowerInvariant() + "-";
ValueValidation = new Regex(
@"^[\p{Ll}\p{Lm}\p{Lo}\p{Nd}]+\-(?<guid>[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12})$",
@"^[^\-]+\-(?<guid>[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12})$",
RegexOptions.Compiled);
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public static T With(string value)

public static T With(Guid guid)
{
var value = $"{Name}-{guid:D}";
var value = $"{NameWithDash}{guid:D}";
return With(value);
}

Expand All @@ -105,8 +105,8 @@ public static IEnumerable<string> Validate(string value)

if (!string.Equals(value.Trim(), value, StringComparison.OrdinalIgnoreCase))
yield return $"Identity '{value}' of type '{typeof(T).PrettyPrint()}' contains leading and/or traling spaces";
if (!value.StartsWith(Name))
yield return $"Identity '{value}' of type '{typeof(T).PrettyPrint()}' does not start with '{Name}'";
if (!value.StartsWith(NameWithDash))
yield return $"Identity '{value}' of type '{typeof(T).PrettyPrint()}' does not start with '{NameWithDash}'";
if (!ValueValidation.IsMatch(value))
yield return $"Identity '{value}' of type '{typeof(T).PrettyPrint()}' does not follow the syntax '[NAME]-[GUID]' in lower case";
}
Expand Down

0 comments on commit fe56dab

Please sign in to comment.