Skip to content

Commit b621f31

Browse files
committed
Added Guid validator
1 parent 9a4aec9 commit b621f31

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

src/MADE.Data.Validation/Strings/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MADE.Data.Validation/Strings/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
<data name="EmailValidator_FeedbackMessage" xml:space="preserve">
133133
<value>The value must be a valid email address, e.g. test@example.com.</value>
134134
</data>
135+
<data name="GuidValidator_FeedbackMessage" xml:space="preserve">
136+
<value>The value must be a valid GUID.</value>
137+
</data>
135138
<data name="IpAddressValidator_FeedbackMessage" xml:space="preserve">
136139
<value>The value must be a valid IP address, e.g. 192.168.0.1.</value>
137140
</data>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// MADE Apps licenses this file to you under the MIT license.
2+
// See the LICENSE file in the project root for more information.
3+
4+
namespace MADE.Data.Validation.Validators
5+
{
6+
using System;
7+
using MADE.Data.Validation.Extensions;
8+
using MADE.Data.Validation.Strings;
9+
10+
/// <summary>
11+
/// Defines a data validator for ensuring a value is a <see cref="Guid"/>.
12+
/// </summary>
13+
public class GuidValidator : IValidator
14+
{
15+
private string feedbackMessage;
16+
17+
/// <summary>
18+
/// Gets or sets the key associated with the validator.
19+
/// </summary>
20+
public string Key { get; set; } = nameof(GuidValidator);
21+
22+
/// <summary>
23+
/// Gets or sets a value indicating whether the data provided is in an invalid state.
24+
/// </summary>
25+
public bool IsInvalid { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets a value indicating whether the data is dirty.
29+
/// </summary>
30+
public bool IsDirty { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the feedback message to display when <see cref="IValidator.IsInvalid"/> is true.
34+
/// </summary>
35+
public string FeedbackMessage
36+
{
37+
get => this.feedbackMessage.IsNullOrWhiteSpace()
38+
? Resources.GuidValidator_FeedbackMessage
39+
: this.feedbackMessage;
40+
set => this.feedbackMessage = value;
41+
}
42+
43+
/// <summary>
44+
/// Executes data validation on the provided <paramref name="value"/>.
45+
/// </summary>
46+
/// <param name="value">The value to be validated.</param>
47+
public void Validate(object value)
48+
{
49+
bool isInvalid;
50+
51+
if (value is Guid)
52+
{
53+
isInvalid = false;
54+
}
55+
else
56+
{
57+
var stringValue = value.ToString();
58+
isInvalid = !Guid.TryParse(stringValue, out _);
59+
}
60+
61+
this.IsInvalid = isInvalid;
62+
this.IsDirty = true;
63+
}
64+
}
65+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace MADE.Data.Validation.Tests.Tests
2+
{
3+
using System;
4+
using System.Diagnostics.CodeAnalysis;
5+
using MADE.Data.Validation.Validators;
6+
using NUnit.Framework;
7+
using Shouldly;
8+
9+
[ExcludeFromCodeCoverage]
10+
[TestFixture]
11+
public class GuidValidatorTests
12+
{
13+
public class WhenValidating
14+
{
15+
[Test]
16+
public void ShouldBeDirtyOnceValidated()
17+
{
18+
// Arrange
19+
string value = "Test";
20+
var validator = new GuidValidator();
21+
22+
// Act
23+
validator.Validate(value);
24+
25+
// Assert
26+
validator.IsDirty.ShouldBe(true);
27+
}
28+
29+
[Test]
30+
public void ShouldBeValidIfGuidType()
31+
{
32+
// Arrange
33+
var value = Guid.NewGuid();
34+
var validator = new GuidValidator();
35+
36+
// Act
37+
validator.Validate(value);
38+
39+
// Assert
40+
validator.IsInvalid.ShouldBe(false);
41+
}
42+
43+
[Test]
44+
public void ShouldBeValidIfStringGuid()
45+
{
46+
// Arrange
47+
var value = "f39bc65d-dcb5-47f1-a3ba-51fb5f584fd9";
48+
var validator = new GuidValidator();
49+
50+
// Act
51+
validator.Validate(value);
52+
53+
// Assert
54+
validator.IsInvalid.ShouldBe(false);
55+
}
56+
57+
[Test]
58+
public void ShouldBeInvalidIfNotGuid()
59+
{
60+
// Arrange
61+
const string value = "Test";
62+
var validator = new GuidValidator();
63+
64+
// Act
65+
validator.Validate(value);
66+
67+
// Assert
68+
validator.IsInvalid.ShouldBe(true);
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)