src/TanvirArjel.CustomValidation/Attributes/FixedLengthAttribute.cs
/// <summary>
/// To check whether the input value has exactly same as specified.
/// </summary>
/// <param name="value">Type of <see cref="string"/>.</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 value has exactly same as specified.
To check whether the input value has exactly same as specified .
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}'");
            }
            if (propertyInfo.PropertyType != typeof(string))
            {
                throw new ArgumentException($"The {nameof(FixedLengthAttribute)} is not valid on property type {propertyInfo.PropertyType}." +
                                            $" This Attribute is only valid on {typeof(string)} type.");
            }
            if (value != null)
            {
                string inputValue = value.ToString();
                if (inputValue.Length != FixedLength)
                {
                    string errorMessage = GetFormattedErrorMessage(ErrorMessage, validationContext.DisplayName, FixedLength);
                    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 } ' " ) ; } if ( propertyInfo . PropertyType != typeof ( string ) ) { throw new ArgumentException ( $" The  { nameof ( FixedLengthAttribute ) }  is not valid on property type  { propertyInfo . PropertyType } . " + $"  This Attribute is only valid on  { typeof ( string ) }  type. " ) ; } if ( value != null ) { string inputValue = value . ToString ( ) ; if ( inputValue . Length != FixedLength ) { string errorMessage = GetFormattedErrorMessage ( ErrorMessage , validationContext . DisplayName , FixedLength ) ; return new ValidationResult ( errorMessage ) ; } } return ValidationResult . Success ; }
To check whether the input value has exactly same as specified.
To check whether the input value has exactly same as specified .
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 .