using System.ComponentModel.DataAnnotations.Resources;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
namespace System.ComponentModel.DataAnnotations {
///
/// Specifies the maximum 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 MaxLengthAttribute : ValidationAttribute {
private const int MaxAllowableLength = -1;
///
/// Gets the maximum allowable length of the array/string data.
///
public int Length { get; private set; }
///
/// Initializes a new instance of the class.
///
///
/// The maximum allowable length of array/string data.
/// Value must be greater than zero.
///
public MaxLengthAttribute(int length)
: base(() => DefaultErrorMessageString) {
Length = length;
}
///
/// Initializes a new instance of the class.
/// The maximum allowable length supported by the database will be used.
///
public MaxLengthAttribute()
: base(() => DefaultErrorMessageString) {
Length = MaxAllowableLength;
}
private static string DefaultErrorMessageString {
get { return DataAnnotationsResources.MaxLengthAttribute_ValidationError; }
}
///
/// 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 less than or equal to the specified maximum length, otherwise false
/// Length is zero or less than negative one.
public override bool IsValid(object value) {
// 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 true;
}
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 MaxAllowableLength == Length || length <= Length;
}
///
/// Applies formatting to a specified error message. (Overrides )
///
/// The name to include in the formatted string.
/// A localized string to describe the maximum acceptable length.
public override string FormatErrorMessage(string name) {
// An error occurred, so we know the value is greater than the maximum if it was specified
return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, Length);
}
///
/// Checks that Length has a legal value.
///
/// Length is zero or less than negative one.
private void EnsureLegalLengths() {
if (Length == 0 || Length < -1) {
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.MaxLengthAttribute_InvalidMaxLength));
}
}
}
}