src/TanvirArjel.CustomValidation/Attributes/RequiredIfAttribute.cs
/// <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>
To check wether the property safisfy the required validaton based on the other property value.
To check wether the property safisfy the required validaton based on the other property value .
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");
            }
            PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherPropertyName);
            if (otherPropertyInfo == null)
            {
                throw new ArgumentException($"The object does not contain any property with name '{OtherPropertyName}'");
            }
            Type otherPropertyType = otherPropertyInfo.PropertyType;
            object otherPropertyContextValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
            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
            {
                throw new Exception($"The type is not supported in {nameof(RequiredIfAttribute)}.");
            }
            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;
        }
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 " ) ; } PropertyInfo otherPropertyInfo = validationContext . ObjectType . GetProperty ( OtherPropertyName ) ; if ( otherPropertyInfo == null ) { throw new ArgumentException ( $" The object does not contain any property with name ' { OtherPropertyName } ' " ) ; } Type otherPropertyType = otherPropertyInfo . PropertyType ; object otherPropertyContextValue = otherPropertyInfo . GetValue ( validationContext . ObjectInstance , null ) ; 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 { throw new Exception ( $" The type is not supported in  { nameof ( RequiredIfAttribute ) } . " ) ; } 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 ; }
To check wether the property safisfy the required validaton based on the other property value.
To check wether the property safisfy the required validaton based on the other property value .
// Cast value to the appropriate dynamic type. // Do comaprison and do the required validation.
None
Thrown if  is null.
Thrown if is null .
value
object
Value of the property.
Value of the property .
validationContext
ValidationContext
The request validation context.
The request validation context .