Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IDP-935] Allow override of error message on GuidAttribute #52

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
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
Loading