diff --git a/src/Workleap.ComponentModel.DataAnnotations.Tests/GuidAttributeTests.cs b/src/Workleap.ComponentModel.DataAnnotations.Tests/GuidAttributeTests.cs index f2060c3..a08f82f 100644 --- a/src/Workleap.ComponentModel.DataAnnotations.Tests/GuidAttributeTests.cs +++ b/src/Workleap.ComponentModel.DataAnnotations.Tests/GuidAttributeTests.cs @@ -7,6 +7,8 @@ namespace Workleap.ComponentModel.DataAnnotations.Tests; public class GuidAttributeTests { + private const string ACustomErrorMessage = "A custom error message."; + private static readonly IEnumerable ValidGuidFormats = new HashSet { "N", "D", "B", "P", "X", @@ -96,19 +98,21 @@ public void Validator_TryValidateObject_Returns_The_Expected_Error_Messages_When var expectedValue2ErrorMessage = GuidAttribute.ErrorMessageWithFormatFormat.FormatInvariant(nameof(Something.Value2), string.Empty, "D"); var expectedValue3ErrorMessage = GuidAttribute.ErrorMessageWithoutFormatFormat.FormatInvariant(nameof(Something.Value3), GuidAttribute.ErrorMessageNonEmptyPart); var expectedValue4ErrorMessage = GuidAttribute.ErrorMessageWithoutFormatFormat.FormatInvariant(nameof(Something.Value4), GuidAttribute.ErrorMessageNonEmptyPart); + var expectedValue5ErrorMessage = ACustomErrorMessage; var results = new List(); var context = new ValidationContext(something, serviceProvider: null, items: null); var isValid = Validator.TryValidateObject(something, context, results, validateAllProperties: true); Assert.False(isValid); - Assert.Equal(4, results.Count); + Assert.Equal(5, results.Count); var errorMessages = results.Select(x => x.ErrorMessage).ToArray(); Assert.Single(errorMessages, expectedValue1ErrorMessage); Assert.Single(errorMessages, expectedValue2ErrorMessage); Assert.Single(errorMessages, expectedValue3ErrorMessage); Assert.Single(errorMessages, expectedValue4ErrorMessage); + Assert.Single(errorMessages, expectedValue5ErrorMessage); } private class Something @@ -125,10 +129,13 @@ private class Something [Guid(AllowEmpty = false)] public Guid Value4 => Guid.Empty; + [Guid(ErrorMessage = ACustomErrorMessage)] + public string Value5 => "not_a_valid_guid_string"; + [Guid] - public Guid Value5 => Guid.NewGuid(); + public Guid Value6 => Guid.NewGuid(); [Guid] - public Guid Value6 => Guid.Empty; + public Guid Value7 => Guid.Empty; } } \ No newline at end of file diff --git a/src/Workleap.ComponentModel.DataAnnotations/GuidAttribute.cs b/src/Workleap.ComponentModel.DataAnnotations/GuidAttribute.cs index 2ff1f49..e123c55 100644 --- a/src/Workleap.ComponentModel.DataAnnotations/GuidAttribute.cs +++ b/src/Workleap.ComponentModel.DataAnnotations/GuidAttribute.cs @@ -50,6 +50,11 @@ private bool IsValidGuid(Guid guid) public override string FormatErrorMessage(string name) { + if (this.ErrorMessage != null) + { + return this.ErrorMessage; + } + var emptiness = this.AllowEmpty ? string.Empty : ErrorMessageNonEmptyPart; return this.Format == null ? string.Format(CultureInfo.InvariantCulture, ErrorMessageWithoutFormatFormat, name, emptiness)