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

Added string enumerable attribute #39

Merged
merged 2 commits into from
Nov 17, 2023
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
@@ -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 };
Gcaya marked this conversation as resolved.
Show resolved Hide resolved
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