-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added string enumerable attribute (#39)
* Added string enumerable attribute * Fixed unsealed class
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/Workleap.ComponentModel.DataAnnotations.Tests/ContainsNonEmptyStringAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?>(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Workleap.ComponentModel.DataAnnotations/ContainsNonEmptyStringAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters