Skip to content

Commit

Permalink
Merge pull request #249 from MADE-Apps/feature/new-validators
Browse files Browse the repository at this point in the history
Added collection of new data validators
  • Loading branch information
jamesmcroft authored May 22, 2022
2 parents 855d5fc + e07bbff commit aee8e4a
Show file tree
Hide file tree
Showing 21 changed files with 1,101 additions and 19 deletions.
32 changes: 31 additions & 1 deletion docs/articles/features/data-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ If the value contains only letters, the validator will report valid; otherwise,

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

### Base64Validator

The `Base64Validator` checks whether a string value is a valid Base64 encoded string.

### BetweenValidator

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

The range can be configured by setting the `Min` and `Max` values.
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`).

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).

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

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

### GuidValidator

The `GuidValidator` checks whether a string value is a valid GUID.

The underlying implementation uses the `Guid.TryParse` method to validate the string.

### IpAddressValidator

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

### LatitudeValidator

The `LatitudeValidator` validates a value is within the valid range for a latitude value (-90 and 90).

### LongitudeValidator

The `LongitudeValidator` validates a value is within the valid range for a longitude value (-180 and 180).

### MacAddressValidator

The `MacAddressValidator` is a simple data validator which ensures that a value is a valid MAC address.

The implementation uses the .NET `PhysicalAddress` class to parse the provided value.

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).

### MaxValueValidator

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

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).

### PredicateValidator

The `PredicateValidator` validates a value using a custom predicate to ensure that a condition is met.

### RegexValidator

The `RegexValidator` is a generic data validator which validates a value based on a configurable regular expression pattern.
Expand Down
29 changes: 18 additions & 11 deletions src/MADE.Data.Validation/MADE.Data.Validation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
<Product>MADE.NET Data Validation</Product>
<Description>
This package includes:
- AlphaNumericValidator for validating whether a string contains only alphanumeric characters.
- AlphaValidator for validating whether a string contains only alpha characters.
- BetweenValidator for validating whether a value is within a minimum and maximum range.
- EmailValidator for validating whether a value is an email address.
- IpAddressValidator for validating whether a value is a valid IP address.
- MaxLengthValidator for validating whether a value is below a maximum length.
- MaxValueValidator for validating whether a value is below a maximum value.
- MinLengthValidator for validating whether a value is above a minimum length.
- MinValueValidator for validating whether a value is above a minimum value.
- RegexValidator for validating whether a value matches a regular expression.
- RequiredValidator for validating whether a value has been provided.
- AlphaNumericValidator for ensuring a string contains only alphanumeric characters.
- AlphaValidator for ensuring a string contains only alpha characters.
- Base64Validator for ensuring a string is a valid base64 string.
- BetweenValidator for ensuring a value is within a minimum and maximum range.
- EmailValidator for ensuring a value is an email address.
- GuidValidator for ensuring a value is a GUID.
- IpAddressValidator for ensuring a value is a valid IP address.
- LatitudeValidator for ensuring a value is a valid latitude.
- LongitudeValidator for ensuring a value is a valid longitude.
- MacAddressValidator for ensuring a value is a valid MAC address.
- MaxLengthValidator for ensuring a value is below a maximum length.
- MaxValueValidator for ensuring a value is below a maximum value.
- MinLengthValidator for ensuring a value is above a minimum length.
- MinValueValidator for ensuring a value is above a minimum value.
- PredicateValidator for ensuring a value meets the condition of a value predicate.
- RegexValidator for ensuring a value matches a regular expression.
- RequiredValidator for ensuring a value has been provided.
- WellFormedUrlValidator for ensuring a value is a well-formed URL.
</Description>
<PackageTags>MADE Data Validation RegEx Range Email IpAddress Min Max Required Length</PackageTags>
</PropertyGroup>
Expand Down
50 changes: 47 additions & 3 deletions src/MADE.Data.Validation/Strings/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/MADE.Data.Validation/Strings/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,24 @@
<data name="AlphaValidator_FeedbackMessage" xml:space="preserve">
<value>The value must only contain letters.</value>
</data>
<data name="Base64Validator_FeedbackMessage" xml:space="preserve">
<value>The value must be a valid base64 string.</value>
</data>
<data name="BetweenValidator_FeedbackMessage" xml:space="preserve">
<value>The value must be between {0} and {1}.</value>
</data>
<data name="EmailValidator_FeedbackMessage" xml:space="preserve">
<value>The value must be a valid email address, e.g. test@example.com.</value>
</data>
<data name="GuidValidator_FeedbackMessage" xml:space="preserve">
<value>The value must be a valid GUID.</value>
</data>
<data name="IpAddressValidator_FeedbackMessage" xml:space="preserve">
<value>The value must be a valid IP address, e.g. 192.168.0.1.</value>
</data>
<data name="MacAddressValidator_FeedbackMessage" xml:space="preserve">
<value>The value must be a valid MAC address, e.g. 00:11:22:33:44:55.</value>
</data>
<data name="MaxLengthValidator_FeedbackMessage" xml:space="preserve">
<value>The length must be less than {0}.</value>
</data>
Expand All @@ -144,10 +153,16 @@
<data name="MinValueValidator_FeedbackMessage" xml:space="preserve">
<value>The value must be greater than {0}.</value>
</data>
<data name="PredicateValidator_FeedbackMessage" xml:space="preserve">
<value>The value is not valid.</value>
</data>
<data name="RegexValidator_FeedbackMessage" xml:space="preserve">
<value>The value does not match the valid mask.</value>
</data>
<data name="RequiredValidator_FeedbackMessage" xml:space="preserve">
<value>A value is required.</value>
</data>
<data name="UrlValidator_FeedbackMessage" xml:space="preserve">
<value>The value must be a valid URL, e.g. https://example.com.</value>
</data>
</root>
57 changes: 57 additions & 0 deletions src/MADE.Data.Validation/Validators/Base64Validator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// MADE Apps licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace MADE.Data.Validation.Validators
{
using System.Text.RegularExpressions;
using MADE.Data.Validation.Extensions;
using MADE.Data.Validation.Strings;

/// <summary>
/// Defines a data validator for ensuring a value is a valid base64 value.
/// </summary>
public class Base64Validator : RegexValidator
{
private string feedbackMessage;

/// <summary>
/// Initializes a new instance of the <see cref="Base64Validator"/> class.
/// </summary>
public Base64Validator()
{
this.Key = nameof(Base64Validator);
this.Pattern = @"^[a-zA-Z0-9\+/]*={0,3}$";
}

/// <summary>
/// Gets or sets the feedback message to display when <see cref="IValidator.IsInvalid"/> is true.
/// </summary>
public override string FeedbackMessage
{
get => this.feedbackMessage.IsNullOrWhiteSpace()
? Resources.Base64Validator_FeedbackMessage
: this.feedbackMessage;
set => this.feedbackMessage = value;
}

/// <summary>
/// Executes data validation on the provided <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to be validated.</param>
/// <exception cref="RegexMatchTimeoutException">Thrown if a Regex time-out occurred.</exception>
public override void Validate(object value)
{
var stringValue = value?.ToString() ?? string.Empty;
if (stringValue.Length % 4 != 0)
{
this.IsInvalid = true;
}
else
{
base.Validate(value);
}

this.IsDirty = true;
}
}
}
21 changes: 19 additions & 2 deletions src/MADE.Data.Validation/Validators/BetweenValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public BetweenValidator(IComparable min, IComparable max)
/// </summary>
public string FeedbackMessage
{
get => this.feedbackMessage.IsNullOrWhiteSpace() ? string.Format(Resources.BetweenValidator_FeedbackMessage, this.Min, this.Max) : this.feedbackMessage;
get => this.feedbackMessage.IsNullOrWhiteSpace()
? string.Format(Resources.BetweenValidator_FeedbackMessage, this.Min, this.Max)
: this.feedbackMessage;
set => this.feedbackMessage = value;
}

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

/// <summary>
/// Gets or sets a value indicating whether the range is inclusive.
/// </summary>
/// <remarks>
/// By default, the value is <c>true</c>.
/// </remarks>
public bool Inclusive { get; set; } = true;

/// <summary>
/// Executes data validation on the provided <paramref name="value"/>.
/// </summary>
Expand All @@ -76,7 +86,14 @@ public void Validate(object value)

if (value is IComparable comparable)
{
isInvalid = comparable.IsLessThan(this.Min) || comparable.IsGreaterThan(this.Max);
if (this.Inclusive)
{
isInvalid = comparable.IsLessThan(this.Min) || comparable.IsGreaterThan(this.Max);
}
else
{
isInvalid = comparable.IsLessThanOrEqualTo(this.Min) || comparable.IsGreaterThanOrEqualTo(this.Max);
}
}

this.IsInvalid = isInvalid;
Expand Down
Loading

0 comments on commit aee8e4a

Please sign in to comment.