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 ;
12using NUnit . Framework ;
23using 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}
Original file line number Diff line number Diff line change 1+ using System ;
12using NUnit . Framework ;
23using 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments