Skip to content

Commit 7cac239

Browse files
authored
Merge pull request #255 from yollosun/feature/251
Feature/#251. Edit types checking
2 parents 1796e80 + d09194e commit 7cac239

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

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

Lines changed: 10 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,16 @@ public void Validate_NullValue_NoExceptions()
4546
}
4647

4748
[Test]
48-
public void Validate_DifferentTypes_NoExceptions()
49+
public void Validate_DifferentTypes_ExceptionThrown()
4950
{
5051
// Act & Assert
51-
TestAttributeForValidValue((decimal)10.5);
52+
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(15.2));
53+
}
54+
55+
[Test]
56+
public void Validate_ValueTypeNotInheritIComparable_ExceptionThrown()
57+
{
58+
// Act & Assert
59+
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(new object()));
5260
}
5361
}

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

Lines changed: 10 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,16 @@ public void Validate_NullValue_NoExceptions()
4546
}
4647

4748
[Test]
48-
public void Validate_DifferentTypes_NoExceptions()
49+
public void Validate_DifferentTypes_ExceptionThrown()
4950
{
5051
// Act & Assert
51-
TestAttributeForValidValue((decimal)12.5);
52+
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(12.5));
53+
}
54+
55+
[Test]
56+
public void Validate_ValueTypeNotInheritIComparable_ExceptionThrown()
57+
{
58+
// Act & Assert
59+
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(new object()));
5260
}
5361
}

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 ArgumentException($"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 ArgumentException("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 ArgumentException($"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 ArgumentException("Type mismatch. The minimum value and property value should be of the same type.");
56+
}
4657
}

0 commit comments

Comments
 (0)