src/TanvirArjel.CustomValidation/Attributes/MaxDateAttribute.cs
/// <summary>
/// To check whether the input date violates the specified max date constraint.
/// </summary>
/// <param name="value">Type of <see cref="DateTime"/>.</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 whether the input date violates the specified max date constraint.
To check whether the input date violates the specified max date constraint .
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (validationContext == null)
            {
                throw new ArgumentNullException(nameof(validationContext));
            }
            PropertyInfo propertyInfo = validationContext.ObjectType.GetProperty(validationContext.MemberName);
            if (propertyInfo == null)
            {
                throw new ArgumentException($"The object does not contain any property with name '{validationContext.MemberName}'");
            }
            Type propertyType = propertyInfo.PropertyType;
            if (propertyType != typeof(DateTime) && propertyType != typeof(DateTime?))
            {
                throw new ArgumentException($"The {nameof(MaxDateAttribute)} is not valid on property type {propertyType}." +
                                            $" This Attribute is only valid on {typeof(DateTime)} and {typeof(DateTime?)}");
            }
            if (value != null)
            {
                DateTime inputDate = (DateTime)value;
                if (inputDate > MaxDate)
                {
                    string errorMessage = FormatErrorMessage(validationContext.DisplayName);
                    return new ValidationResult(errorMessage);
                }
            }
            return ValidationResult.Success;
        }
protected override ValidationResult IsValid ( object value , ValidationContext validationContext ) { if ( validationContext == null ) { throw new ArgumentNullException ( nameof ( validationContext ) ) ; } PropertyInfo propertyInfo = validationContext . ObjectType . GetProperty ( validationContext . MemberName ) ; if ( propertyInfo == null ) { throw new ArgumentException ( $" The object does not contain any property with name ' { validationContext . MemberName } ' " ) ; } Type propertyType = propertyInfo . PropertyType ; if ( propertyType != typeof ( DateTime ) && propertyType != typeof ( DateTime ? ) ) { throw new ArgumentException ( $" The  { nameof ( MaxDateAttribute ) }  is not valid on property type  { propertyType } . " + $"  This Attribute is only valid on  { typeof ( DateTime ) }  and  { typeof ( DateTime ? ) } " ) ; } if ( value != null ) { DateTime inputDate = ( DateTime ) value ; if ( inputDate > MaxDate ) { string errorMessage = FormatErrorMessage ( validationContext . DisplayName ) ; return new ValidationResult ( errorMessage ) ; } } return ValidationResult . Success ; }
To check whether the input date violates the specified max date constraint.
To check whether the input date violates the specified max date constraint .
None
Thrown if  is null.
Thrown if is null .
value
object
Type of .
Type of .
validationContext
ValidationContext
The request validation context.
The request validation context .