Skip to content

Commit

Permalink
Allow override of error message on GuidAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFICE\arnaud.foulem committed Feb 13, 2024
1 parent 80dc744 commit 0fac5f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Workleap.ComponentModel.DataAnnotations.Tests;

public class GuidAttributeTests
{
private const string ACustomErrorMessage = "A custom error message.";

private static readonly IEnumerable<string> ValidGuidFormats = new HashSet<string>
{
"N", "D", "B", "P", "X",
Expand Down Expand Up @@ -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<ValidationResult>();
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
Expand All @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/Workleap.ComponentModel.DataAnnotations/GuidAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0fac5f7

Please sign in to comment.