Skip to content

Commit

Permalink
Added string enumerable attribute (#39)
Browse files Browse the repository at this point in the history
* Added string enumerable attribute

* Fixed unsealed class
  • Loading branch information
ruvyas authored Nov 17, 2023
1 parent de39bb9 commit b121fd4
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace Workleap.ComponentModel.DataAnnotations.Tests;

public sealed class ContainsNonEmptyStringAttributeTests
{
[Theory]
[ClassData(typeof(ValidData))]
public void Given_ValidStrings_When_Validate_Then_Valid(object? inputs)
{
var attr = new ContainsNonEmptyStringAttribute();
Assert.True(attr.IsValid(inputs));
}

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

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

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);
var result = Assert.Single(results);
Assert.NotNull(result.ErrorMessage);
Assert.Equal(expectedErrorMessage, result.ErrorMessage);
}

private class ValidData : IEnumerable<object?[]>
{
public IEnumerator<object?[]> GetEnumerator()
{
yield return new object?[] { null };
yield return new object[] { new string[] { "Lorem ipsum dolor sit amet" } };
yield return new object[] { new string[] { "Lorem ipsum dolor sit amet", "Etiam porta velit non nisi feugiat pulvinar" } };
yield return new object[] { new string[] { string.Empty, "Lorem ipsum dolor sit amet" } };
yield return new object[] { new string?[] { default, "Lorem ipsum dolor sit amet" } };
yield return new object[] { new string[] { "Lorem ipsum dolor sit amet", " " } };
}

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

private class InvalidData : IEnumerable<object?[]>
{
public IEnumerator<object?[]> GetEnumerator()
{
yield return new object[] { new string?[] { string.Empty, default } };
yield return new object[] { new string?[] { string.Empty } };
yield return new object[] { new string?[] { default } };
yield return new object[] { new string[] { " " } };
}

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

private class SomeClass
{
[ContainsNonEmptyString]
public string?[] Values { get; set; } = Array.Empty<string?>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Workleap.ComponentModel.DataAnnotations;

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

public ContainsNonEmptyStringAttribute() : base(ErrorMessageFormat)
{
}

public override bool IsValid(object? value) => value switch
{
null => true,
IEnumerable<string> enumerable => enumerable.Any(x => !string.IsNullOrWhiteSpace(x)),
_ => false,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Workleap.ComponentModel.DataAnnotations.ContainsAttribute
Workleap.ComponentModel.DataAnnotations.ContainsAttribute.ContainsAttribute(string! text) -> void
Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyGuidAttribute
Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyGuidAttribute.ContainsNonEmptyGuidAttribute() -> void
Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyStringAttribute
Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyStringAttribute.ContainsNonEmptyStringAttribute() -> void
Workleap.ComponentModel.DataAnnotations.EndsWithAttribute
Workleap.ComponentModel.DataAnnotations.EndsWithAttribute.EndsWithAttribute(string! text) -> void
Workleap.ComponentModel.DataAnnotations.GuidAttribute
Expand Down Expand Up @@ -46,6 +48,7 @@ Workleap.ComponentModel.DataAnnotations.UrlOfKindAttribute.UrlOfKindAttribute(Sy
Workleap.ComponentModel.DataAnnotations.ValidatePropertiesAttribute
Workleap.ComponentModel.DataAnnotations.ValidatePropertiesAttribute.ValidatePropertiesAttribute() -> void
override Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyGuidAttribute.IsValid(object? value) -> bool
override Workleap.ComponentModel.DataAnnotations.ContainsNonEmptyStringAttribute.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 b121fd4

Please sign in to comment.