Skip to content

Commit 98d6b58

Browse files
committed
Added MAC address validator
1 parent b621f31 commit 98d6b58

File tree

10 files changed

+149
-6
lines changed

10 files changed

+149
-6
lines changed

src/MADE.Data.Validation/MADE.Data.Validation.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
- Base64Validator for ensuring a string is a valid base64 string.
1212
- BetweenValidator for ensuring a value is within a minimum and maximum range.
1313
- EmailValidator for ensuring a value is an email address.
14+
- GuidValidator for ensuring a value is a GUID.
1415
- IpAddressValidator for ensuring a value is a valid IP address.
1516
- LatitudeValidator for ensuring a value is a valid latitude.
1617
- LongitudeValidator for ensuring a value is a valid longitude.
18+
- MacAddressValidator for ensuring a value is a valid MAC address.
1719
- MaxLengthValidator for ensuring a value is below a maximum length.
1820
- MaxValueValidator for ensuring a value is below a maximum value.
1921
- MinLengthValidator for ensuring a value is above a minimum length.

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
@@ -138,6 +138,9 @@
138138
<data name="IpAddressValidator_FeedbackMessage" xml:space="preserve">
139139
<value>The value must be a valid IP address, e.g. 192.168.0.1.</value>
140140
</data>
141+
<data name="MacAddressValidator_FeedbackMessage" xml:space="preserve">
142+
<value>The value must be a valid MAC address, e.g. 00:11:22:33:44:55.</value>
143+
</data>
141144
<data name="MaxLengthValidator_FeedbackMessage" xml:space="preserve">
142145
<value>The length must be less than {0}.</value>
143146
</data>

src/MADE.Data.Validation/Validators/Base64Validator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override string FeedbackMessage
4141
/// <exception cref="RegexMatchTimeoutException">Thrown if a Regex time-out occurred.</exception>
4242
public override void Validate(object value)
4343
{
44-
var stringValue = value.ToString();
44+
var stringValue = value?.ToString() ?? string.Empty;
4545
if (stringValue.Length % 4 != 0)
4646
{
4747
this.IsInvalid = true;

src/MADE.Data.Validation/Validators/GuidValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void Validate(object value)
5454
}
5555
else
5656
{
57-
var stringValue = value.ToString();
57+
var stringValue = value?.ToString() ?? string.Empty;
5858
isInvalid = !Guid.TryParse(stringValue, out _);
5959
}
6060

src/MADE.Data.Validation/Validators/LatitudeValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual string FeedbackMessage
5656
/// <param name="value">The value to be validated.</param>
5757
public void Validate(object value)
5858
{
59-
bool parsed = double.TryParse(value.ToString(), out double latitude);
59+
bool parsed = double.TryParse(value?.ToString() ?? string.Empty, out double latitude);
6060
this.IsInvalid = !parsed || latitude is < Min or > Max;
6161
this.IsDirty = true;
6262
}

src/MADE.Data.Validation/Validators/LongitudeValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual string FeedbackMessage
5656
/// <param name="value">The value to be validated.</param>
5757
public void Validate(object value)
5858
{
59-
bool parsed = double.TryParse(value.ToString(), out double longitude);
59+
bool parsed = double.TryParse(value?.ToString() ?? string.Empty, out double longitude);
6060
this.IsInvalid = !parsed || longitude is < Min or > Max;
6161
this.IsDirty = true;
6262
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 System.Net.NetworkInformation;
8+
using MADE.Data.Validation.Extensions;
9+
using MADE.Data.Validation.Strings;
10+
11+
/// <summary>
12+
/// Defines a data validator for ensuring a value is a valid MAC address.
13+
/// </summary>
14+
public class MacAddressValidator : IValidator
15+
{
16+
private string feedbackMessage;
17+
18+
/// <summary>
19+
/// Gets or sets the key associated with the validator.
20+
/// </summary>
21+
public string Key { get; set; } = nameof(MacAddressValidator);
22+
23+
/// <summary>
24+
/// Gets or sets a value indicating whether the data provided is in an invalid state.
25+
/// </summary>
26+
public bool IsInvalid { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets a value indicating whether the data is dirty.
30+
/// </summary>
31+
public bool IsDirty { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets the feedback message to display when <see cref="IValidator.IsInvalid"/> is true.
35+
/// </summary>
36+
public string FeedbackMessage
37+
{
38+
get => this.feedbackMessage.IsNullOrWhiteSpace()
39+
? Resources.MacAddressValidator_FeedbackMessage
40+
: this.feedbackMessage;
41+
set => this.feedbackMessage = value;
42+
}
43+
44+
/// <summary>
45+
/// Executes data validation on the provided <paramref name="value"/>.
46+
/// </summary>
47+
/// <param name="value">The value to be validated.</param>
48+
/// <exception cref="OverflowException">The array is multidimensional and contains more than <see cref="int.MaxValue"></see> elements.</exception>
49+
public void Validate(object value)
50+
{
51+
bool isInvalid;
52+
var stringValue = value?.ToString() ?? string.Empty;
53+
54+
try
55+
{
56+
PhysicalAddress newAddress = PhysicalAddress.Parse(stringValue);
57+
isInvalid = PhysicalAddress.None.Equals(newAddress);
58+
}
59+
catch (FormatException)
60+
{
61+
isInvalid = true;
62+
}
63+
64+
this.IsInvalid = isInvalid;
65+
this.IsDirty = true;
66+
}
67+
}
68+
}

src/MADE.Data.Validation/Validators/WellFormedUrlValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public void Validate(object value)
5454
}
5555
else
5656
{
57-
var valStr = value.ToString();
58-
isInvalid = !Uri.IsWellFormedUriString(valStr, UriKind.Absolute);
57+
var stringValue = value?.ToString() ?? string.Empty;
58+
isInvalid = !Uri.IsWellFormedUriString(stringValue, UriKind.Absolute);
5959
}
6060

6161
this.IsInvalid = isInvalid;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace MADE.Data.Validation.Tests.Tests
2+
{
3+
using System.Diagnostics.CodeAnalysis;
4+
using MADE.Data.Validation.Validators;
5+
using NUnit.Framework;
6+
using Shouldly;
7+
8+
[ExcludeFromCodeCoverage]
9+
[TestFixture]
10+
public class MacAddressValidatorTests
11+
{
12+
public class WhenValidating
13+
{
14+
[Test]
15+
public void ShouldBeDirtyOnceValidated()
16+
{
17+
// Arrange
18+
string value = "Test";
19+
var validator = new MacAddressValidator();
20+
21+
// Act
22+
validator.Validate(value);
23+
24+
// Assert
25+
validator.IsDirty.ShouldBe(true);
26+
}
27+
28+
[TestCase("001122334455")]
29+
[TestCase("00-11-22-33-44-55")]
30+
[TestCase("0011.2233.4455")]
31+
[TestCase("00:11:22:33:44:55")]
32+
[TestCase("F0-E1-D2-C3-B4-A5")]
33+
[TestCase("f0-e1-d2-c3-b4-a5")]
34+
public void ShouldBeValidIfValidMacAddress(string value)
35+
{
36+
// Arrange
37+
var validator = new MacAddressValidator();
38+
39+
// Act
40+
validator.Validate(value);
41+
42+
// Assert
43+
validator.IsInvalid.ShouldBe(false);
44+
}
45+
46+
[TestCase("Test")]
47+
[TestCase("00/11/22/33/44/55")]
48+
public void ShouldBeInvalidIfInvalidMacAddress(string value)
49+
{
50+
// Arrange
51+
var validator = new MacAddressValidator();
52+
53+
// Act
54+
validator.Validate(value);
55+
56+
// Assert
57+
validator.IsInvalid.ShouldBe(true);
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)