Skip to content

Commit

Permalink
Test error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball committed Feb 23, 2024
1 parent 2344b79 commit f0e7e47
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Faithlife.DataAnnotations/ValidateObjectAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public sealed class ValidateObjectAttribute : ValidationAttribute
var innerErrorMessage = string.Join(" ", validationResults.Select(x => x.ErrorMessage).Where(x => x is not null));
return new ValidationResult(
errorMessage: $"{FormatErrorMessage(validationContext.DisplayName)}{(innerErrorMessage.Length == 0 ? "" : $" ({innerErrorMessage})")}",
memberNames: validationContext.MemberName is { } memberName ? new[] { memberName } : null);
memberNames: validationContext.MemberName is { } memberName ? [memberName] : null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;

namespace Faithlife.DataAnnotations.Tests;

public sealed class ValidateObjectAttributeTests
{
[Test]
public void ValidateRecursive()
{
var invalid = new ValidatableDto();
var validatable = new ValidatableDto { Required = "x", Validatable = invalid };

var results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results.Single().MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Validatable)));
Assert.That(results.Single().ErrorMessage, Is.EqualTo("The field Validatable is invalid. (The Required field is required.)"));

validatable.Validatable = new ValidatableDto { Required = "x", Validatable = invalid };
results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results.Single().MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Validatable)));
Assert.That(results.Single().ErrorMessage, Is.EqualTo("The field Validatable is invalid. (The field Validatable is invalid. (The Required field is required.))"));

validatable.Validatable.Validatable = new ValidatableDto { Required = "x" };
results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results, Is.Empty);
}

private sealed class ValidatableDto
{
[Required]
public string? Required { get; set; }

[ValidateObject]
public ValidatableDto? Validatable { get; set; }
}
}
33 changes: 9 additions & 24 deletions tests/Faithlife.DataAnnotations.Tests/ValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,35 @@ public void ValidateRequired()
var validatable = new ValidatableDto();

var results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results.Single().MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Required)));
AssertFieldIsRequired(results.Single());
var exception = Assert.Throws<ValidationException>(() => ValidatorUtility.Validate(validatable));
Assert.That(exception!.ValidationResult.MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Required)));
AssertFieldIsRequired(exception!.ValidationResult);

validatable.Required = "";
results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results.Single().MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Required)));
AssertFieldIsRequired(results.Single());
Assert.Throws<ValidationException>(() => ValidatorUtility.Validate(validatable));

validatable.Required = " ";
results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results.Single().MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Required)));
AssertFieldIsRequired(results.Single());
Assert.Throws<ValidationException>(() => ValidatorUtility.Validate(validatable));

validatable.Required = "x";
results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results, Is.Empty);
ValidatorUtility.Validate(validatable);
}

[Test]
public void ValidateRecursive()
{
var invalid = new ValidatableDto();
var validatable = new ValidatableDto { Required = "x", Validatable = invalid };

var results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results.Single().MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Validatable)));

validatable.Validatable = new ValidatableDto { Required = "x", Validatable = invalid };
results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results.Single().MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Validatable)));

validatable.Validatable.Validatable = new ValidatableDto { Required = "x" };
results = ValidatorUtility.GetValidationResults(validatable);
Assert.That(results, Is.Empty);
void AssertFieldIsRequired(ValidationResult result)
{
Assert.That(result.MemberNames.Single(), Is.EqualTo(nameof(ValidatableDto.Required)));
Assert.That(result.ErrorMessage, Is.EqualTo("The Required field is required."));
}
}

private sealed class ValidatableDto
{
[Required]
public string? Required { get; set; }

[ValidateObject]
public ValidatableDto? Validatable { get; set; }
}
}

0 comments on commit f0e7e47

Please sign in to comment.