using System.ComponentModel.DataAnnotations.Resources;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
namespace System.ComponentModel.DataAnnotations
{
///
/// Specifies the minimum length of array/string data allowed in a property.
///
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "We want users to be able to extend this class")]
public class MinLengthAttribute : ValidationAttribute
{
///
/// Gets the minimum allowable length of the array/string data.
///
public int Length { get; private set; }
///
/// Initializes a new instance of the class.
///
///
/// The minimum allowable length of array/string data.
/// Value must be greater than or equal to zero.
///
public MinLengthAttribute(int length)
: base(DataAnnotationsResources.MinLengthAttribute_ValidationError)
{
Length = length;
}
///
/// Determines whether a specified object is valid. (Overrides )
///
///
/// This method returns true if the is null.
/// It is assumed the is used if the value may not be null.
///
/// The object to validate.
/// true if the value is null or greater than or equal to the specified minimum length, otherwise false
/// Length is less than zero.
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// Check the lengths for legality
EnsureLegalLengths();
var length = 0;
// Automatically pass if value is null. RequiredAttribute should be used to assert a value is not null.
if (value == null)
{
return ValidationResult.Success;
}
else
{
var str = value as string;
if (str != null)
{
length = str.Length;
}
else
{
// We expect a cast exception if a non-{string|array} property was passed in.
length = ((Array)value).Length;
}
}
return length >= Length ? ValidationResult.Success : new ValidationResult(FormatErrorMessage(validationContext.MemberName));
}
///
/// Applies formatting to a specified error message. (Overrides )
///
/// The name to include in the formatted string.
/// A localized string to describe the minimum acceptable length.
public override string FormatErrorMessage(string name)
{
// An error occurred, so we know the value is less than the minimum
return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, Length);
}
///
/// Checks that Length has a legal value.
///
/// Length is less than zero.
private void EnsureLegalLengths()
{
if (Length < 0)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.MinLengthAttribute_InvalidMinLength));
}
}
}
}