Skip to content

Commit

Permalink
[#251][add] minAttribute and minAttributeTests
Browse files Browse the repository at this point in the history
  • Loading branch information
yollosun committed Jan 4, 2024
1 parent ff15343 commit cf58575
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
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;

Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MaxAttribute : ValidationAttribute
public MaxAttribute(IComparable maxValue, string? errorMessage = null, bool isMessageFromStringTable = true) : base(errorMessage, isMessageFromStringTable) => MaxValue = maxValue;

/// <summary>
/// Gets or sets the maximum length of the property.
/// Gets or sets the maximum value of the property.
/// </summary>
/// <value>
/// The maximum value of the property.
Expand Down
46 changes: 46 additions & 0 deletions src/Simplify.Web/Model/Validation/Attributes/MinAttribute.cs
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}");
}
}

0 comments on commit cf58575

Please sign in to comment.