protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var result = ValidationResult.Success; var otherValue = validationContext.ObjectType.GetProperty(OtherPropertyName)?.GetValue(validationContext.ObjectInstance, null); if (value == null) { return(result); } if (!(value is DateTime)) { return(result); } if (otherValue == null) { return(result); } if (!(otherValue is DateTime)) { return(result); } if (!OtherPropertyName.Contains("EventDateTime")) { if ((DateTime)value > (DateTime)otherValue) { result = new ValidationResult(ErrorMessage); } } else { if ((DateTime)value < (DateTime)otherValue) { result = new ValidationResult(ErrorMessage); } } if ((DateTime)value == (DateTime)otherValue && !AllowEquality) { result = new ValidationResult(ErrorMessage); } return(result); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (OtherPropertyName.IsNullOrEmptyOrWhiteSpace()) { return(new ValidationResult("Other property name is empty")); } var otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherPropertyName); if (otherPropertyInfo == null) { return(new ValidationResult($"Unknown property: {OtherPropertyName}")); } var otherValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null); if (Equals(OtherPropertyValue, otherValue)) { return(base.IsValid(value, validationContext)); } return(null); }
/// <summary> /// To check wether the property safisfy the required validaton based on the other property value. /// </summary> /// <param name="value">Value of the property.</param> /// <param name="validationContext">The request validation context.</param> /// <returns>Returns <see cref="ValidationResult"/>.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="validationContext"/> is null.</exception> protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (validationContext == null) { throw new ArgumentNullException(nameof(validationContext)); } if (string.IsNullOrWhiteSpace(OtherPropertyName)) { throw new ArgumentException($"Other property name is empty"); } object parentObj = validationContext.ObjectInstance; PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherPropertyName); if (otherPropertyInfo == null) { string[] propertyNames = OtherPropertyName.Split('.'); otherPropertyInfo = validationContext.ObjectType.GetProperty(propertyNames[0]); if (otherPropertyInfo == null) { throw new ArgumentException($"The object does not contain any property with name '{OtherPropertyName}'"); } for (int i = 1; i < propertyNames.Length; i++) { parentObj = otherPropertyInfo.GetValue(parentObj, null); otherPropertyInfo = otherPropertyInfo.PropertyType.GetProperty(propertyNames[i]); if (otherPropertyInfo == null) { throw new ArgumentException($"The object does not contain any property with name '{OtherPropertyName}'"); } } } Type otherPropertyType = otherPropertyInfo.PropertyType; object otherPropertyContextValue = otherPropertyInfo.GetValue(parentObj, null); // Cast value to the appropriate dynamic type. dynamic otherPropertyContextValueDynamic; dynamic otherPropertyValueDynamic; if (otherPropertyType.IsDateTimeType()) { otherPropertyContextValueDynamic = otherPropertyContextValue.ToDateTime(); otherPropertyValueDynamic = OtherPropertyValue.ToDateTime(); } else if (otherPropertyType.IsNumericType()) { otherPropertyContextValueDynamic = Convert.ToDecimal(otherPropertyContextValue, CultureInfo.InvariantCulture); otherPropertyValueDynamic = Convert.ToDecimal(OtherPropertyValue, CultureInfo.InvariantCulture); } else if (otherPropertyType == typeof(string)) { if (this.ComparisonType == ComparisonType.Equal || this.ComparisonType == ComparisonType.NotEqual) { otherPropertyContextValueDynamic = otherPropertyContextValue?.ToString() ?? string.Empty; otherPropertyValueDynamic = OtherPropertyValue?.ToString() ?? string.Empty; } else { otherPropertyContextValueDynamic = otherPropertyContextValue?.ToString().Length ?? 0; otherPropertyValueDynamic = OtherPropertyValue?.ToString().Length ?? 0; } } else if (otherPropertyType.IsTimeSpanType()) { otherPropertyContextValueDynamic = otherPropertyContextValue.ToTimeSpan(); otherPropertyValueDynamic = OtherPropertyValue.ToTimeSpan(); } else if (otherPropertyType == typeof(bool)) { if (ComparisonType != ComparisonType.Equal && ComparisonType != ComparisonType.NotEqual) { throw new Exception($"The comparison type with boolean is not supported in {nameof(RequiredIfAttribute)}. Use Equal or NotEqual."); } otherPropertyContextValueDynamic = Convert.ToBoolean(otherPropertyContextValue, CultureInfo.InvariantCulture); otherPropertyValueDynamic = Convert.ToBoolean(OtherPropertyValue, CultureInfo.InvariantCulture); } else { throw new Exception($"The type is not supported in {nameof(RequiredIfAttribute)}."); } // Do comaprison and do the required validation. if (ComparisonType == ComparisonType.Equal) { if (otherPropertyContextValueDynamic == otherPropertyValueDynamic) { return(IsRequired(value, validationContext)); } } else if (ComparisonType == ComparisonType.NotEqual) { if (otherPropertyContextValueDynamic != otherPropertyValueDynamic) { return(IsRequired(value, validationContext)); } } else if (ComparisonType == ComparisonType.GreaterThan) { if (otherPropertyContextValueDynamic > otherPropertyValueDynamic) { return(IsRequired(value, validationContext)); } } else if (ComparisonType == ComparisonType.GreaterThanOrEqual) { if (otherPropertyContextValueDynamic >= otherPropertyValueDynamic) { return(IsRequired(value, validationContext)); } } else if (ComparisonType == ComparisonType.SmallerThan) { if (otherPropertyContextValueDynamic < otherPropertyValueDynamic) { return(IsRequired(value, validationContext)); } } else if (ComparisonType == ComparisonType.SmallerThanOrEqual) { if (otherPropertyContextValueDynamic <= otherPropertyValueDynamic) { return(IsRequired(value, validationContext)); } } return(ValidationResult.Success); }