Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaya committed Oct 31, 2023
1 parent 205691c commit 7b91672
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ The most useful validation attribute here is probably `ValidatePropertiesAttribu
| Attribute | Description |
|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `GuidAttribute` | Validates that a string property is a well-formatted GUID with an optional format |
| `NotEmptyAttribute` | Validate that an enumerable property is not empty |
| `ContainsNonEmptyGuidAttribute` | Validates that a Guid enumerable property contains at least one non-empty Guid |
| `NotEmptyAttribute` | Validates that an enumerable property is not empty |
| `ValidatePropertiesAttribute` | Validates **all properties of a complex type property** (nested object validation) |
| `TimeSpanAttribute` | Validates that a string property is a well-formatted TimeSpan with an optional format |
| `UrlOfKindAttribute` | Validates that a string or `Uri` property is a well-formatted url of the specified `UriKind` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@

namespace Workleap.ComponentModel.DataAnnotations.Tests;

public class ContainsValidGuidAttributeTests
public sealed class ContainsNonEmptyGuidAttributeTests
{
[Theory]
[ClassData(typeof(ValidData))]
public void Given_ValidGuids_When_Validate_Then_Valid(object? inputs)
{
var attr = new ContainsValidGuidAttribute();
var attr = new ContainsNonEmptyGuidAttribute();
Assert.True(attr.IsValid(inputs));
}

[Theory]
[ClassData(typeof(InvalidData))]
public void Given_InvalidGuids_When_Validate_Then_Invalid(object? inputs)
{
var attr = new ContainsValidGuidAttribute();
var attr = new ContainsNonEmptyGuidAttribute();
Assert.False(attr.IsValid(inputs));
}

[Fact]
public void Validator_TryValidateObject_Returns_The_Expected_Error_Message_When_Validation_Fails()
{
var something = new SomeClass();
var expectedErrorMessage = string.Format(CultureInfo.InvariantCulture, ContainsValidGuidAttribute.ErrorMessageFormat, nameof(SomeClass.Values));
var expectedErrorMessage = string.Format(CultureInfo.InvariantCulture, ContainsNonEmptyGuidAttribute.ErrorMessageFormat, nameof(SomeClass.Values));

var results = new List<ValidationResult>();
var context = new ValidationContext(something, serviceProvider: null, items: null);
Expand All @@ -40,10 +40,11 @@ public void Validator_TryValidateObject_Returns_The_Expected_Error_Message_When_
Assert.Equal(expectedErrorMessage, result.ErrorMessage);
}

private class ValidData : IEnumerable<object[]>
private class ValidData : IEnumerable<object?[]>
{
public IEnumerator<object[]> GetEnumerator()
public IEnumerator<object?[]> GetEnumerator()
{
yield return new object?[] { null };
yield return new object[] { new Guid[] { new("f8daff85-4393-42ae-9ab5-8620ab20c8da") } };
yield return new object[] { new string[] { "d78b48f9-37b8-47dd-8e47-0325dd3e7899" } };
yield return new object[] { new Guid[] { default, new("846398d2-416d-4f05-86b0-431e11117abc") } };
Expand All @@ -57,24 +58,25 @@ public IEnumerator<object[]> GetEnumerator()
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}

private class InvalidData : IEnumerable<object[]>
private class InvalidData : IEnumerable<object?[]>
{
public IEnumerator<object[]> GetEnumerator()
public IEnumerator<object?[]> GetEnumerator()
{
yield return new object[] { new Guid[] { default } };
yield return new object[] { new Guid?[] { default } };
yield return new object[] { new string[] { string.Empty } };
yield return new object[] { new string?[] { default } };
yield return new object[] { new Guid[] { Guid.Empty } };
yield return new object[] { new string[] { "00000000-0000-0000-0000-000000000000" } };
yield return new object[] { new int[] { 0 } };
}

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}

private class SomeClass
{
[ContainsValidGuid]
[ContainsNonEmptyGuid]
public Guid?[] Values { get; set; } = Array.Empty<Guid?>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
namespace Workleap.ComponentModel.DataAnnotations;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public sealed class ContainsValidGuidAttribute : ValidationAttribute
public sealed class ContainsNonEmptyGuidAttribute : ValidationAttribute
{
internal const string ErrorMessageFormat = "The field {0} must be an enumerable that contains at least one valid GUID";
internal const string ErrorMessageFormat = "The field {0} must be an collection that contains at least one non-empty GUID";

public ContainsValidGuidAttribute() : base(ErrorMessageFormat)
public ContainsNonEmptyGuidAttribute() : base(ErrorMessageFormat)
{
}

public override bool IsValid(object? value) => value switch
{
null => false,
null => true,
IEnumerable<Guid> enumerable => enumerable.Any(this.IsValidGuid),
IEnumerable<Guid?> enumerable => enumerable.Any(x => x != null && this.IsValidGuid(x.Value)),
IEnumerable<string> enumerable => enumerable.Any(this.IsValidGuid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
abstract Workleap.ComponentModel.DataAnnotations.TextBasedValidationAttribute.IsValid(string! value) -> bool
Workleap.ComponentModel.DataAnnotations.ContainsAttribute
Workleap.ComponentModel.DataAnnotations.ContainsAttribute.ContainsAttribute(string! text) -> void
Workleap.ComponentModel.DataAnnotations.ContainsValidGuidAttribute
Workleap.ComponentModel.DataAnnotations.ContainsValidGuidAttribute.ContainsValidGuidAttribute() -> void
Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyGuidAttribute
Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyGuidAttribute.ContainsNonEmptyGuidAttribute() -> void
Workleap.ComponentModel.DataAnnotations.EndsWithAttribute
Workleap.ComponentModel.DataAnnotations.EndsWithAttribute.EndsWithAttribute(string! text) -> void
Workleap.ComponentModel.DataAnnotations.GuidAttribute
Expand Down Expand Up @@ -45,7 +45,7 @@ Workleap.ComponentModel.DataAnnotations.UrlOfKindAttribute.Kind.get -> System.Ur
Workleap.ComponentModel.DataAnnotations.UrlOfKindAttribute.UrlOfKindAttribute(System.UriKind kind) -> void
Workleap.ComponentModel.DataAnnotations.ValidatePropertiesAttribute
Workleap.ComponentModel.DataAnnotations.ValidatePropertiesAttribute.ValidatePropertiesAttribute() -> void
override Workleap.ComponentModel.DataAnnotations.ContainsValidGuidAttribute.IsValid(object? value) -> bool
override Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyGuidAttribute.IsValid(object? value) -> bool
override Workleap.ComponentModel.DataAnnotations.GuidAttribute.FormatErrorMessage(string! name) -> string!
override Workleap.ComponentModel.DataAnnotations.GuidAttribute.IsValid(object? value) -> bool
override Workleap.ComponentModel.DataAnnotations.NotEmptyAttribute.IsValid(object? value) -> bool
Expand Down

0 comments on commit 7b91672

Please sign in to comment.