Skip to content

Commit 9954111

Browse files
committed
[#251][edit] throw exception if types mismatch in min and max attributes
1 parent cf58575 commit 9954111

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

src/Simplify.Web.Tests/Model/Validation/Attributes/MaxAttributeTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NUnit.Framework;
23
using Simplify.Web.Model.Validation.Attributes;
34

@@ -45,9 +46,26 @@ public void Validate_NullValue_NoExceptions()
4546
}
4647

4748
[Test]
48-
public void Validate_DifferentTypes_NoExceptions()
49+
public void Validate_DifferentTypes_ExceptionThrown()
4950
{
51+
// Assign
52+
53+
var value = 15.2;
54+
var defaultMessage = "Type mismatch. The maximum value and property value should be of the same type.";
55+
5056
// Act & Assert
51-
TestAttributeForValidValue((decimal)10.5);
57+
TestAttribute(value, defaultMessage);
58+
}
59+
60+
[Test]
61+
public void Validate_ValueTypeNotInheritIComparable_ExceptionThrown()
62+
{
63+
// Assign
64+
65+
var value = new object();
66+
var defaultMessage = $"The type of specified property value must be inherited from {typeof(IComparable)}";
67+
68+
// Act & Assert
69+
TestAttribute(value, defaultMessage);
5270
}
5371
}

src/Simplify.Web.Tests/Model/Validation/Attributes/MinAttributeTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NUnit.Framework;
23
using Simplify.Web.Model.Validation.Attributes;
34

@@ -45,9 +46,26 @@ public void Validate_NullValue_NoExceptions()
4546
}
4647

4748
[Test]
48-
public void Validate_DifferentTypes_NoExceptions()
49+
public void Validate_DifferentTypes_ExceptionThrown()
4950
{
51+
// Assign
52+
53+
var value = 12.5;
54+
var defaultMessage = "Type mismatch. The minimum value and property value should be of the same type.";
55+
56+
// Act & Assert
57+
TestAttribute(value, defaultMessage);
58+
}
59+
60+
[Test]
61+
public void Validate_ValueTypeNotInheritIComparable_ExceptionThrown()
62+
{
63+
// Assign
64+
65+
var value = new object();
66+
var defaultMessage = $"The type of specified property value must be inherited from {typeof(IComparable)}";
67+
5068
// Act & Assert
51-
TestAttributeForValidValue((decimal)12.5);
69+
TestAttribute(value, defaultMessage);
5270
}
5371
}

src/Simplify.Web/Model/Validation/Attributes/MaxAttribute.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,24 @@ public class MaxAttribute : ValidationAttribute
3434
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
3535
public override void Validate(object? value, PropertyInfo propertyInfo, IDIResolver resolver)
3636
{
37-
if (value is not IComparable comparableValue || comparableValue.GetType() != MaxValue.GetType())
37+
if (value == null)
3838
return;
3939

40+
if (value is not IComparable comparableValue)
41+
throw new ModelValidationException($"The type of specified property value must be inherited from {typeof(IComparable)}");
42+
43+
ValidateTypesMatching(comparableValue);
44+
4045
TryThrowCustomOrStringTableException(resolver);
4146

4247
if (comparableValue.CompareTo(MaxValue) > 0)
4348
throw new ModelValidationException(
4449
$"Property '{propertyInfo.Name}' required maximum value is {MaxValue}, actual value: {value}");
4550
}
51+
52+
private void ValidateTypesMatching(IComparable comparableValue)
53+
{
54+
if (comparableValue.GetType() != MaxValue.GetType())
55+
throw new ModelValidationException("Type mismatch. The maximum value and property value should be of the same type.");
56+
}
4657
}

src/Simplify.Web/Model/Validation/Attributes/MinAttribute.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,24 @@ public class MinAttribute : ValidationAttribute
3434
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
3535
public override void Validate(object? value, PropertyInfo propertyInfo, IDIResolver resolver)
3636
{
37-
if (value is not IComparable comparableValue || comparableValue.GetType() != MinValue.GetType())
37+
if (value == null)
3838
return;
3939

40+
if (value is not IComparable comparableValue)
41+
throw new ModelValidationException($"The type of specified property value must be inherited from {typeof(IComparable)}");
42+
43+
ValidateTypesMatching(comparableValue);
44+
4045
TryThrowCustomOrStringTableException(resolver);
4146

4247
if (comparableValue.CompareTo(MinValue) < 0)
4348
throw new ModelValidationException(
4449
$"Property '{propertyInfo.Name}' required minimum value is {MinValue}, actual value: {value}");
4550
}
51+
52+
private void ValidateTypesMatching(IComparable comparableValue)
53+
{
54+
if (comparableValue.GetType() != MinValue.GetType())
55+
throw new ModelValidationException("Type mismatch. The minimum value and property value should be of the same type.");
56+
}
4657
}

0 commit comments

Comments
 (0)