File tree Expand file tree Collapse file tree 4 files changed +44
-6
lines changed
Simplify.Web.Tests/Model/Validation/Attributes
Simplify.Web/Model/Validation/Attributes Expand file tree Collapse file tree 4 files changed +44
-6
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
1
2
using NUnit . Framework ;
2
3
using Simplify . Web . Model . Validation . Attributes ;
3
4
@@ -45,9 +46,16 @@ public void Validate_NullValue_NoExceptions()
45
46
}
46
47
47
48
[ Test ]
48
- public void Validate_DifferentTypes_NoExceptions ( )
49
+ public void Validate_DifferentTypes_ExceptionThrown ( )
49
50
{
50
51
// 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 ( ) ) ) ;
52
60
}
53
61
}
Original file line number Diff line number Diff line change
1
+ using System ;
1
2
using NUnit . Framework ;
2
3
using Simplify . Web . Model . Validation . Attributes ;
3
4
@@ -45,9 +46,16 @@ public void Validate_NullValue_NoExceptions()
45
46
}
46
47
47
48
[ Test ]
48
- public void Validate_DifferentTypes_NoExceptions ( )
49
+ public void Validate_DifferentTypes_ExceptionThrown ( )
49
50
{
50
51
// 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 ( ) ) ) ;
52
60
}
53
61
}
Original file line number Diff line number Diff line change @@ -34,13 +34,24 @@ public class MaxAttribute : ValidationAttribute
34
34
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
35
35
public override void Validate ( object ? value , PropertyInfo propertyInfo , IDIResolver resolver )
36
36
{
37
- if ( value is not IComparable comparableValue || comparableValue . GetType ( ) != MaxValue . GetType ( ) )
37
+ if ( value == null )
38
38
return ;
39
39
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
+
40
45
TryThrowCustomOrStringTableException ( resolver ) ;
41
46
42
47
if ( comparableValue . CompareTo ( MaxValue ) > 0 )
43
48
throw new ModelValidationException (
44
49
$ "Property '{ propertyInfo . Name } ' required maximum value is { MaxValue } , actual value: { value } ") ;
45
50
}
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
+ }
46
57
}
Original file line number Diff line number Diff line change @@ -34,13 +34,24 @@ public class MinAttribute : ValidationAttribute
34
34
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
35
35
public override void Validate ( object ? value , PropertyInfo propertyInfo , IDIResolver resolver )
36
36
{
37
- if ( value is not IComparable comparableValue || comparableValue . GetType ( ) != MinValue . GetType ( ) )
37
+ if ( value == null )
38
38
return ;
39
39
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
+
40
45
TryThrowCustomOrStringTableException ( resolver ) ;
41
46
42
47
if ( comparableValue . CompareTo ( MinValue ) < 0 )
43
48
throw new ModelValidationException (
44
49
$ "Property '{ propertyInfo . Name } ' required minimum value is { MinValue } , actual value: { value } ") ;
45
50
}
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
+ }
46
57
}
You can’t perform that action at this time.
0 commit comments