CompareAttribute.cs
1 using System;
2 using System.ComponentModel.DataAnnotations;
3 
4 namespace Sage.ES.S50.ValidationsAttribute
5 {
9  [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
10  public class CompareAttribute : ValidationAttribute
11  {
12  #region Enumns
13  public enum OperatorType
17  {
21  LessThan,
25  LessOrEqualThan,
29  GreatherThan,
33  GreatherOrEqualThan,
37  Equal,
41  Distinct
42  }
43  #endregion
44 
45  #region Members
46  private readonly string _comparisonProperty;
47  private readonly OperatorType _operatorType;
48  #endregion
49 
50  #region Constructor
51  public CompareAttribute( OperatorType operatorType, string comparisonProperty)
57  {
58  _comparisonProperty = comparisonProperty;
59  _operatorType = operatorType;
60  }
61  #endregion
62 
63  #region Private Methods
64  protected override ValidationResult IsValid(object value, ValidationContext validationContext)
71  {
72  ErrorMessage = ErrorMessageString;
73 
74  var property = validationContext.ObjectType.GetProperty(_comparisonProperty);
75 
76  if (property == null)
77  {
78  throw new ArgumentException("Comparison property with this name not found");
79  }
80 
81  var comparisonValue = property.GetValue(validationContext.ObjectInstance,null);
82 
83  // Si el tipo es una fecha, realizamos un caso especial para los valores nulos
84  if (comparisonValue.GetType() == typeof(DateTime) && value == null)
85  {
86  // Si el valor a comparar es nulo, no hay que realizar las validaciones
87  return ValidationResult.Success;
88  }
89 
90 
91  if (comparisonValue.GetType() == typeof(IComparable))
92  {
93  throw new ArgumentException("Comparison property has not implemented IComparable interface");
94  }
95 
96  if (value.GetType() == typeof(IComparable))
97  {
98  throw new ArgumentException("value has not implemented IComparable interface");
99  }
100 
101  var currentValue = (IComparable)value;
102 
103  if (!ReferenceEquals(value.GetType(), comparisonValue.GetType()))
104  {
105  throw new ArgumentException("The properties types must be the same");
106  }
107  bool validation = false;
108  int retCompare = currentValue.CompareTo((IComparable)comparisonValue);
109  switch (_operatorType)
110  {
111  case OperatorType.LessThan:
112  validation = retCompare < 0;
113  break;
114  case OperatorType.LessOrEqualThan:
115  validation = retCompare <= 0;
116  break;
117  case OperatorType.GreatherThan:
118  validation = retCompare > 0;
119  break;
120  case OperatorType.GreatherOrEqualThan:
121  validation = retCompare >= 0;
122  break;
123  case OperatorType.Equal:
124  validation = retCompare == 0;
125  break;
126  case OperatorType.Distinct:
127  validation = retCompare != 0;
128  break;
129  default:
130  break;
131  }
132  if (!validation)
133  {
134  return new ValidationResult(ErrorMessage, new string[] { validationContext.MemberName });
135  }
136 
137  return ValidationResult.Success;
138  }
139  #endregion
140  }
141 }
ValidaciĆ³n: Compara el valor con otra propiedad