Problem:I wrote derived class from DataAnnotation.StringLengthAttribute attribute that allows exact number of characters instead of Min/Max.
My string:enter image description here
How can I override this default message for ErrorMessage?
Then I've tried to change ErrorMessage property, but it doesn't appers if string is invalid. Just default message appears only.
/// <summary> /// Specifies the exact length of characters that are allowed in a data field. /// </summary> public class ExactStringLengthAttribute : StringLengthAttribute { public ExactStringLengthAttribute(int length) : base(length) { //ErrorMessageResourceName = nameof(ValidationMessages.ExactStringLength); //ErrorMessageResourceType = typeof(ValidationMessages); ErrorMessage = nameof(ValidationMessages.ExactStringLength); MinimumLength = length; } }
I've tried to inherit from ValidationAttribute instead of StringLengthAttribute, but in this case there is no message at all. (It's the second question.)
/// <summary>/// Specifies the exact length of characters that are allowed in a data field./// </summary>[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,AllowMultiple = false)]public class ExactStringLengthAttribute : ValidationAttribute{ public ExactStringLengthAttribute(int length) : base(() => ValidationMessages.ExactStringLength) { //ErrorMessageResourceName = nameof(ValidationMessages.ExactStringLength); //ErrorMessageResourceType = typeof(ValidationMessages); //ErrorMessage = nameof(ValidationMessages.ExactStringLength); Length = length; } /// <summary> /// Gets the maximum acceptable length of the string /// </summary> public int Length { get; } /// <summary> /// Override of <see cref="ValidationAttribute.IsValid(object)" /> /// </summary> /// <remarks> /// This method returns <c>true</c> if the <paramref name="value" /> is null. /// It is assumed the <see cref="RequiredAttribute" /> is used if the value may not be null. /// </remarks> /// <param name="value">The value to test.</param> /// <returns><c>true</c> if the value is null or less than or equal to the set maximum length</returns> /// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception> public override bool IsValid(object? value) { // Check the lengths for legality EnsureLegalLengths(); // Automatically pass if value is null. RequiredAttribute should be used to assert a value is not null. // We expect a cast exception if a non-string was passed in. if (value == null) { return true; } int length = ((string)value).Length; return length == Length; } /// <summary> /// Override of <see cref="ValidationAttribute.FormatErrorMessage" /> /// </summary> /// <param name="name">The name to include in the formatted string</param> /// <returns>A localized string to describe the maximum acceptable length</returns> /// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception> public override string FormatErrorMessage(string name) { EnsureLegalLengths(); // it's ok to pass in the minLength even for the error message without a {2} param since string.Format will just // ignore extra arguments return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, Length); } /// <summary> /// Checks that MinimumLength and MaximumLength have legal values. Throws InvalidOperationException if not. /// </summary> private void EnsureLegalLengths() { if (Length < 0) { throw new InvalidOperationException(""); } }}
Possibly similar thread:https://github.com/dotnet/aspnetcore/issues/4848?ysclid=lrun12evt921520779
I didn't find any information about client's validation in Swagger UI and how can I configure it.