-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#251][add] minAttribute and minAttributeTests
- Loading branch information
Showing
4 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
src/Simplify.Web.Tests/Model/Validation/Attributes/MaxAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using Simplify.Web.Model.Validation.Attributes; | ||
|
||
|
53 changes: 53 additions & 0 deletions
53
src/Simplify.Web.Tests/Model/Validation/Attributes/MinAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using NUnit.Framework; | ||
using Simplify.Web.Model.Validation.Attributes; | ||
|
||
namespace Simplify.Web.Tests.Model.Validation.Attributes; | ||
|
||
[TestFixture] | ||
public class MinAttributeTests : AttributesTestBase | ||
{ | ||
public const int MinValue = 12; | ||
|
||
[OneTimeSetUp] | ||
public void SetupAttribute() => Attr = new MinAttribute(MinValue); | ||
|
||
[Test] | ||
public void Validate_AboveMinValue_Ok() | ||
{ | ||
// Act & Assert | ||
TestAttributeForValidValue(15); | ||
} | ||
|
||
[Test] | ||
public void Validate_MinValueEqualsValue_Ok() | ||
{ | ||
// Act & Assert | ||
TestAttributeForValidValue(12); | ||
} | ||
|
||
[Test] | ||
public void Validate_BelowMinValue_ExceptionThrown() | ||
{ | ||
// Assign | ||
|
||
var value = 8; | ||
var defaultMessage = $"Property '{nameof(TestEntityWithProperty.Prop1)}' required minimum value is {MinValue}, actual value: {value}"; | ||
|
||
// Act & Assert | ||
TestAttribute(value, defaultMessage); | ||
} | ||
|
||
[Test] | ||
public void Validate_NullValue_NoExceptions() | ||
{ | ||
// Act & Assert | ||
TestAttributeForValidValue(null); | ||
} | ||
|
||
[Test] | ||
public void Validate_DifferentTypes_NoExceptions() | ||
{ | ||
// Act & Assert | ||
TestAttributeForValidValue((decimal)12.5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Simplify.Web/Model/Validation/Attributes/MinAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Reflection; | ||
using Simplify.DI; | ||
|
||
namespace Simplify.Web.Model.Validation.Attributes; | ||
|
||
/// <summary> | ||
/// Sets minimum required property value | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class MinAttribute : ValidationAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MinAttribute"/> class. | ||
/// </summary> | ||
/// <param name="minValue">Minimum value of the property.</param> | ||
/// <param name="errorMessage">The error message.</param> | ||
/// <param name="isMessageFromStringTable">if set to <c>true</c> [is message from string table].</param> | ||
public MinAttribute(IComparable minValue, string? errorMessage = null, bool isMessageFromStringTable = true) : base(errorMessage, isMessageFromStringTable) => MinValue = minValue; | ||
|
||
/// <summary> | ||
/// Gets or sets the minimum value of the property. | ||
/// </summary> | ||
/// <value> | ||
/// The minimum value of the property. | ||
/// </value> | ||
public IComparable MinValue { get; } | ||
|
||
/// <summary> | ||
/// Validates the specified property value. | ||
/// </summary> | ||
/// <param name="value">The object value.</param> | ||
/// <param name="propertyInfo">Information about the property containing this attribute.</param> | ||
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param> | ||
public override void Validate(object? value, PropertyInfo propertyInfo, IDIResolver resolver) | ||
{ | ||
if (value is not IComparable comparableValue || comparableValue.GetType() != MinValue.GetType()) | ||
return; | ||
|
||
TryThrowCustomOrStringTableException(resolver); | ||
|
||
if (comparableValue.CompareTo(MinValue) < 0) | ||
throw new ModelValidationException( | ||
$"Property '{propertyInfo.Name}' required minimum value is {MinValue}, actual value: {value}"); | ||
} | ||
} |