Skip to content

Commit 1796e80

Browse files
authored
Merge pull request #252 from yollosun/feature/251
[#251][add] maxAttribute and maxAttributeTests
2 parents b74117f + cf58575 commit 1796e80

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using NUnit.Framework;
2+
using Simplify.Web.Model.Validation.Attributes;
3+
4+
namespace Simplify.Web.Tests.Model.Validation.Attributes;
5+
6+
[TestFixture]
7+
public class MaxAttributeTests : AttributesTestBase
8+
{
9+
public const int MaxValue = 12;
10+
11+
[OneTimeSetUp]
12+
public void SetupAttribute() => Attr = new MaxAttribute(MaxValue);
13+
14+
[Test]
15+
public void Validate_BelowMaxValue_Ok()
16+
{
17+
// Act & Assert
18+
TestAttributeForValidValue(10);
19+
}
20+
21+
[Test]
22+
public void Validate_MaxValueEqualsValue_Ok()
23+
{
24+
// Act & Assert
25+
TestAttributeForValidValue(12);
26+
}
27+
28+
[Test]
29+
public void Validate_AboveMaxValue_ExceptionThrown()
30+
{
31+
// Assign
32+
33+
var value = 15;
34+
var defaultMessage = $"Property '{nameof(TestEntityWithProperty.Prop1)}' required maximum value is {MaxValue}, actual value: {value}";
35+
36+
// Act & Assert
37+
TestAttribute(value, defaultMessage);
38+
}
39+
40+
[Test]
41+
public void Validate_NullValue_NoExceptions()
42+
{
43+
// Act & Assert
44+
TestAttributeForValidValue(null);
45+
}
46+
47+
[Test]
48+
public void Validate_DifferentTypes_NoExceptions()
49+
{
50+
// Act & Assert
51+
TestAttributeForValidValue((decimal)10.5);
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using NUnit.Framework;
2+
using Simplify.Web.Model.Validation.Attributes;
3+
4+
namespace Simplify.Web.Tests.Model.Validation.Attributes;
5+
6+
[TestFixture]
7+
public class MinAttributeTests : AttributesTestBase
8+
{
9+
public const int MinValue = 12;
10+
11+
[OneTimeSetUp]
12+
public void SetupAttribute() => Attr = new MinAttribute(MinValue);
13+
14+
[Test]
15+
public void Validate_AboveMinValue_Ok()
16+
{
17+
// Act & Assert
18+
TestAttributeForValidValue(15);
19+
}
20+
21+
[Test]
22+
public void Validate_MinValueEqualsValue_Ok()
23+
{
24+
// Act & Assert
25+
TestAttributeForValidValue(12);
26+
}
27+
28+
[Test]
29+
public void Validate_BelowMinValue_ExceptionThrown()
30+
{
31+
// Assign
32+
33+
var value = 8;
34+
var defaultMessage = $"Property '{nameof(TestEntityWithProperty.Prop1)}' required minimum value is {MinValue}, actual value: {value}";
35+
36+
// Act & Assert
37+
TestAttribute(value, defaultMessage);
38+
}
39+
40+
[Test]
41+
public void Validate_NullValue_NoExceptions()
42+
{
43+
// Act & Assert
44+
TestAttributeForValidValue(null);
45+
}
46+
47+
[Test]
48+
public void Validate_DifferentTypes_NoExceptions()
49+
{
50+
// Act & Assert
51+
TestAttributeForValidValue((decimal)12.5);
52+
}
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Reflection;
3+
using Simplify.DI;
4+
5+
namespace Simplify.Web.Model.Validation.Attributes;
6+
7+
/// <summary>
8+
/// Sets maximum required property value
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Property)]
11+
public class MaxAttribute : ValidationAttribute
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="MaxAttribute"/> class.
15+
/// </summary>
16+
/// <param name="maxValue">Maximum value of the property.</param>
17+
/// <param name="errorMessage">The error message.</param>
18+
/// <param name="isMessageFromStringTable">if set to <c>true</c> [is message from string table].</param>
19+
public MaxAttribute(IComparable maxValue, string? errorMessage = null, bool isMessageFromStringTable = true) : base(errorMessage, isMessageFromStringTable) => MaxValue = maxValue;
20+
21+
/// <summary>
22+
/// Gets or sets the maximum value of the property.
23+
/// </summary>
24+
/// <value>
25+
/// The maximum value of the property.
26+
/// </value>
27+
public IComparable MaxValue { get; }
28+
29+
/// <summary>
30+
/// Validates the specified property value.
31+
/// </summary>
32+
/// <param name="value">The object value.</param>
33+
/// <param name="propertyInfo">Information about the property containing this attribute.</param>
34+
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
35+
public override void Validate(object? value, PropertyInfo propertyInfo, IDIResolver resolver)
36+
{
37+
if (value is not IComparable comparableValue || comparableValue.GetType() != MaxValue.GetType())
38+
return;
39+
40+
TryThrowCustomOrStringTableException(resolver);
41+
42+
if (comparableValue.CompareTo(MaxValue) > 0)
43+
throw new ModelValidationException(
44+
$"Property '{propertyInfo.Name}' required maximum value is {MaxValue}, actual value: {value}");
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Reflection;
3+
using Simplify.DI;
4+
5+
namespace Simplify.Web.Model.Validation.Attributes;
6+
7+
/// <summary>
8+
/// Sets minimum required property value
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Property)]
11+
public class MinAttribute : ValidationAttribute
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="MinAttribute"/> class.
15+
/// </summary>
16+
/// <param name="minValue">Minimum value of the property.</param>
17+
/// <param name="errorMessage">The error message.</param>
18+
/// <param name="isMessageFromStringTable">if set to <c>true</c> [is message from string table].</param>
19+
public MinAttribute(IComparable minValue, string? errorMessage = null, bool isMessageFromStringTable = true) : base(errorMessage, isMessageFromStringTable) => MinValue = minValue;
20+
21+
/// <summary>
22+
/// Gets or sets the minimum value of the property.
23+
/// </summary>
24+
/// <value>
25+
/// The minimum value of the property.
26+
/// </value>
27+
public IComparable MinValue { get; }
28+
29+
/// <summary>
30+
/// Validates the specified property value.
31+
/// </summary>
32+
/// <param name="value">The object value.</param>
33+
/// <param name="propertyInfo">Information about the property containing this attribute.</param>
34+
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
35+
public override void Validate(object? value, PropertyInfo propertyInfo, IDIResolver resolver)
36+
{
37+
if (value is not IComparable comparableValue || comparableValue.GetType() != MinValue.GetType())
38+
return;
39+
40+
TryThrowCustomOrStringTableException(resolver);
41+
42+
if (comparableValue.CompareTo(MinValue) < 0)
43+
throw new ModelValidationException(
44+
$"Property '{propertyInfo.Name}' required minimum value is {MinValue}, actual value: {value}");
45+
}
46+
}

0 commit comments

Comments
 (0)