1
+ using System ;
2
+ using System . Reflection ;
3
+ using Simplify . DI ;
4
+
5
+ namespace Simplify . Web . Model . Validation . Attributes ;
6
+
7
+ /// <summary>
8
+ /// Sets a range constraint
9
+ /// </summary>
10
+ [ AttributeUsage ( AttributeTargets . Property ) ]
11
+ public class RangeAttribute : ValidationAttribute
12
+ {
13
+ /// <summary>
14
+ /// Initializes a new instance of the <see cref="RangeAttribute"/> class.
15
+ /// </summary>
16
+ /// <param name="minValue">The minimum value, inclusive.</param>
17
+ /// <param name="maxValue">The maximum value, inclusive.</param>
18
+ /// <param name="errorMessage">The error message.</param>
19
+ /// <param name="isMessageFromStringTable">if set to <c>true</c> [is message from string table].</param>
20
+ public RangeAttribute ( IComparable minValue , IComparable maxValue , string ? errorMessage = null , bool isMessageFromStringTable = true ) : base ( errorMessage , isMessageFromStringTable )
21
+ {
22
+ MinValue = minValue ;
23
+ MaxValue = maxValue ;
24
+ }
25
+
26
+ /// <summary>
27
+ /// Gets or sets the minimum value for the range.
28
+ /// </summary>
29
+ /// <value>
30
+ /// The minimum value for the range.
31
+ /// </value>
32
+ public IComparable MinValue { get ; }
33
+
34
+ /// <summary>
35
+ /// Gets or sets the maximum value for the range.
36
+ /// </summary>
37
+ /// <value>
38
+ /// The maximum value for the range.
39
+ /// </value>
40
+ public IComparable MaxValue { get ; }
41
+
42
+ /// <summary>
43
+ /// Validates the specified property value.
44
+ /// </summary>
45
+ /// <param name="value">The object value.</param>
46
+ /// <param name="propertyInfo">Information about the property containing this attribute.</param>
47
+ /// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
48
+ public override void Validate ( object ? value , PropertyInfo propertyInfo , IDIResolver resolver )
49
+ {
50
+ if ( value == null )
51
+ return ;
52
+
53
+ if ( value is not IComparable comparableValue )
54
+ throw new ArgumentException ( $ "The type of specified property value must be inherited from { typeof ( IComparable ) } ") ;
55
+
56
+ ValidateTypesMatching ( comparableValue ) ;
57
+
58
+ TryThrowCustomOrStringTableException ( resolver ) ;
59
+
60
+ if ( comparableValue . CompareTo ( MinValue ) < 0 || comparableValue . CompareTo ( MaxValue ) > 0 )
61
+ throw new ModelValidationException (
62
+ $ "The value is out of range. The range constraint - { MinValue } - { MaxValue } , actual value: { value } ") ;
63
+ }
64
+
65
+ private void ValidateTypesMatching ( IComparable comparableValue )
66
+ {
67
+ if ( comparableValue . GetType ( ) != MinValue . GetType ( ) )
68
+ throw new ArgumentException ( "Type mismatch. The minimum value and property value should be of the same type." ) ;
69
+
70
+ if ( comparableValue . GetType ( ) != MaxValue . GetType ( ) )
71
+ throw new ArgumentException ( "Type mismatch. The maximum value and property value should be of the same type." ) ;
72
+ }
73
+ }
0 commit comments