Skip to content

Commit aee8e4a

Browse files
authored
Merge pull request #249 from MADE-Apps/feature/new-validators
Added collection of new data validators
2 parents 855d5fc + e07bbff commit aee8e4a

21 files changed

+1101
-19
lines changed

docs/articles/features/data-validation.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,15 @@ If the value contains only letters, the validator will report valid; otherwise,
6969

7070
Similar to the `AlphaValidator`, the `AlphaNumericValidator` extends the characters in the validation to include numbers also.
7171

72+
### Base64Validator
73+
74+
The `Base64Validator` checks whether a string value is a valid Base64 encoded string.
75+
7276
### BetweenValidator
7377

7478
The `BetweenValidator` validates an `IComparable` value is between a configurable minimum and maximum range.
7579

76-
The range can be configured by setting the `Min` and `Max` values.
80+
The range can be configured by setting the `Min` and `Max` values, as well as an `Inclusive` flag to configure whether the minimum and maximum values are included in the range (defaults to `true`).
7781

7882
The in-box `System` types which implement the `IComparable` interface can be [found in the Microsoft documentation](https://docs.microsoft.com/en-us/dotnet/api/system.icomparable?view=net-5.0).
7983

@@ -89,6 +93,12 @@ this.Pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|" + @"([-a-zA-Z0-9!#$%&'*+/
8993

9094
MADE.NET has a comprehensive set of test cases which validate the implementation with a variety of different valid and invalid email addresses.
9195

96+
### GuidValidator
97+
98+
The `GuidValidator` checks whether a string value is a valid GUID.
99+
100+
The underlying implementation uses the `Guid.TryParse` method to validate the string.
101+
92102
### IpAddressValidator
93103

94104
The `IpAddressValidator` is a simple data validator which ensures that a value is a valid IP address.
@@ -100,6 +110,22 @@ The implementation splits the IP address into each nibble and validates them bas
100110
- Is a digit
101111
- Is a numeric value between 0 and 255
102112

113+
### LatitudeValidator
114+
115+
The `LatitudeValidator` validates a value is within the valid range for a latitude value (-90 and 90).
116+
117+
### LongitudeValidator
118+
119+
The `LongitudeValidator` validates a value is within the valid range for a longitude value (-180 and 180).
120+
121+
### MacAddressValidator
122+
123+
The `MacAddressValidator` is a simple data validator which ensures that a value is a valid MAC address.
124+
125+
The implementation uses the .NET `PhysicalAddress` class to parse the provided value.
126+
127+
For more information on the `PhysicalAddress` class, see the [Microsoft documentation](https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.physicaladdress?view=net-6.0).
128+
103129
### MaxValueValidator
104130

105131
The `MaxValueValidator` validates an `IComparable` value is less than a configurable maximum value.
@@ -116,6 +142,10 @@ The minimum can be configured by setting the `Min` value.
116142

117143
The in-box `System` types which implement the `IComparable` interface can be [found in the Microsoft documentation](https://docs.microsoft.com/en-us/dotnet/api/system.icomparable?view=net-5.0).
118144

145+
### PredicateValidator
146+
147+
The `PredicateValidator` validates a value using a custom predicate to ensure that a condition is met.
148+
119149
### RegexValidator
120150

121151
The `RegexValidator` is a generic data validator which validates a value based on a configurable regular expression pattern.

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@
66
<Product>MADE.NET Data Validation</Product>
77
<Description>
88
This package includes:
9-
- AlphaNumericValidator for validating whether a string contains only alphanumeric characters.
10-
- AlphaValidator for validating whether a string contains only alpha characters.
11-
- BetweenValidator for validating whether a value is within a minimum and maximum range.
12-
- EmailValidator for validating whether a value is an email address.
13-
- IpAddressValidator for validating whether a value is a valid IP address.
14-
- MaxLengthValidator for validating whether a value is below a maximum length.
15-
- MaxValueValidator for validating whether a value is below a maximum value.
16-
- MinLengthValidator for validating whether a value is above a minimum length.
17-
- MinValueValidator for validating whether a value is above a minimum value.
18-
- RegexValidator for validating whether a value matches a regular expression.
19-
- RequiredValidator for validating whether a value has been provided.
9+
- AlphaNumericValidator for ensuring a string contains only alphanumeric characters.
10+
- AlphaValidator for ensuring a string contains only alpha characters.
11+
- Base64Validator for ensuring a string is a valid base64 string.
12+
- BetweenValidator for ensuring a value is within a minimum and maximum range.
13+
- EmailValidator for ensuring a value is an email address.
14+
- GuidValidator for ensuring a value is a GUID.
15+
- IpAddressValidator for ensuring a value is a valid IP address.
16+
- LatitudeValidator for ensuring a value is a valid latitude.
17+
- LongitudeValidator for ensuring a value is a valid longitude.
18+
- MacAddressValidator for ensuring a value is a valid MAC address.
19+
- MaxLengthValidator for ensuring a value is below a maximum length.
20+
- MaxValueValidator for ensuring a value is below a maximum value.
21+
- MinLengthValidator for ensuring a value is above a minimum length.
22+
- MinValueValidator for ensuring a value is above a minimum value.
23+
- PredicateValidator for ensuring a value meets the condition of a value predicate.
24+
- RegexValidator for ensuring a value matches a regular expression.
25+
- RequiredValidator for ensuring a value has been provided.
26+
- WellFormedUrlValidator for ensuring a value is a well-formed URL.
2027
</Description>
2128
<PackageTags>MADE Data Validation RegEx Range Email IpAddress Min Max Required Length</PackageTags>
2229
</PropertyGroup>

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

Lines changed: 47 additions & 3 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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,24 @@
123123
<data name="AlphaValidator_FeedbackMessage" xml:space="preserve">
124124
<value>The value must only contain letters.</value>
125125
</data>
126+
<data name="Base64Validator_FeedbackMessage" xml:space="preserve">
127+
<value>The value must be a valid base64 string.</value>
128+
</data>
126129
<data name="BetweenValidator_FeedbackMessage" xml:space="preserve">
127130
<value>The value must be between {0} and {1}.</value>
128131
</data>
129132
<data name="EmailValidator_FeedbackMessage" xml:space="preserve">
130133
<value>The value must be a valid email address, e.g. test@example.com.</value>
131134
</data>
135+
<data name="GuidValidator_FeedbackMessage" xml:space="preserve">
136+
<value>The value must be a valid GUID.</value>
137+
</data>
132138
<data name="IpAddressValidator_FeedbackMessage" xml:space="preserve">
133139
<value>The value must be a valid IP address, e.g. 192.168.0.1.</value>
134140
</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>
135144
<data name="MaxLengthValidator_FeedbackMessage" xml:space="preserve">
136145
<value>The length must be less than {0}.</value>
137146
</data>
@@ -144,10 +153,16 @@
144153
<data name="MinValueValidator_FeedbackMessage" xml:space="preserve">
145154
<value>The value must be greater than {0}.</value>
146155
</data>
156+
<data name="PredicateValidator_FeedbackMessage" xml:space="preserve">
157+
<value>The value is not valid.</value>
158+
</data>
147159
<data name="RegexValidator_FeedbackMessage" xml:space="preserve">
148160
<value>The value does not match the valid mask.</value>
149161
</data>
150162
<data name="RequiredValidator_FeedbackMessage" xml:space="preserve">
151163
<value>A value is required.</value>
152164
</data>
165+
<data name="UrlValidator_FeedbackMessage" xml:space="preserve">
166+
<value>The value must be a valid URL, e.g. https://example.com.</value>
167+
</data>
153168
</root>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.Text.RegularExpressions;
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 valid base64 value.
12+
/// </summary>
13+
public class Base64Validator : RegexValidator
14+
{
15+
private string feedbackMessage;
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="Base64Validator"/> class.
19+
/// </summary>
20+
public Base64Validator()
21+
{
22+
this.Key = nameof(Base64Validator);
23+
this.Pattern = @"^[a-zA-Z0-9\+/]*={0,3}$";
24+
}
25+
26+
/// <summary>
27+
/// Gets or sets the feedback message to display when <see cref="IValidator.IsInvalid"/> is true.
28+
/// </summary>
29+
public override string FeedbackMessage
30+
{
31+
get => this.feedbackMessage.IsNullOrWhiteSpace()
32+
? Resources.Base64Validator_FeedbackMessage
33+
: this.feedbackMessage;
34+
set => this.feedbackMessage = value;
35+
}
36+
37+
/// <summary>
38+
/// Executes data validation on the provided <paramref name="value"/>.
39+
/// </summary>
40+
/// <param name="value">The value to be validated.</param>
41+
/// <exception cref="RegexMatchTimeoutException">Thrown if a Regex time-out occurred.</exception>
42+
public override void Validate(object value)
43+
{
44+
var stringValue = value?.ToString() ?? string.Empty;
45+
if (stringValue.Length % 4 != 0)
46+
{
47+
this.IsInvalid = true;
48+
}
49+
else
50+
{
51+
base.Validate(value);
52+
}
53+
54+
this.IsDirty = true;
55+
}
56+
}
57+
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public BetweenValidator(IComparable min, IComparable max)
5252
/// </summary>
5353
public string FeedbackMessage
5454
{
55-
get => this.feedbackMessage.IsNullOrWhiteSpace() ? string.Format(Resources.BetweenValidator_FeedbackMessage, this.Min, this.Max) : this.feedbackMessage;
55+
get => this.feedbackMessage.IsNullOrWhiteSpace()
56+
? string.Format(Resources.BetweenValidator_FeedbackMessage, this.Min, this.Max)
57+
: this.feedbackMessage;
5658
set => this.feedbackMessage = value;
5759
}
5860

@@ -66,6 +68,14 @@ public string FeedbackMessage
6668
/// </summary>
6769
public IComparable Max { get; set; }
6870

71+
/// <summary>
72+
/// Gets or sets a value indicating whether the range is inclusive.
73+
/// </summary>
74+
/// <remarks>
75+
/// By default, the value is <c>true</c>.
76+
/// </remarks>
77+
public bool Inclusive { get; set; } = true;
78+
6979
/// <summary>
7080
/// Executes data validation on the provided <paramref name="value"/>.
7181
/// </summary>
@@ -76,7 +86,14 @@ public void Validate(object value)
7686

7787
if (value is IComparable comparable)
7888
{
79-
isInvalid = comparable.IsLessThan(this.Min) || comparable.IsGreaterThan(this.Max);
89+
if (this.Inclusive)
90+
{
91+
isInvalid = comparable.IsLessThan(this.Min) || comparable.IsGreaterThan(this.Max);
92+
}
93+
else
94+
{
95+
isInvalid = comparable.IsLessThanOrEqualTo(this.Min) || comparable.IsGreaterThanOrEqualTo(this.Max);
96+
}
8097
}
8198

8299
this.IsInvalid = isInvalid;

0 commit comments

Comments
 (0)