-
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.
Only non empty strings and guids attributes
- Loading branch information
1 parent
c541410
commit 94f78cb
Showing
7 changed files
with
249 additions
and
19 deletions.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
src/Workleap.ComponentModel.DataAnnotations.Tests/ContainsOnlyNonEmptyGuidsAttributeTests.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,86 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Globalization; | ||
|
||
namespace Workleap.ComponentModel.DataAnnotations.Tests; | ||
|
||
public sealed class ContainsOnlyNonEmptyGuidsAttributeTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(ValidData))] | ||
public void Given_ValidGuids_When_Validate_Then_Valid(object? inputs) | ||
{ | ||
var attr = new ContainsOnlyNonEmptyGuidsAttribute(); | ||
Assert.True(attr.IsValid(inputs)); | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(InvalidData))] | ||
public void Given_InvalidGuids_When_Validate_Then_Invalid(object? inputs) | ||
{ | ||
var attr = new ContainsOnlyNonEmptyGuidsAttribute(); | ||
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, ContainsOnlyNonEmptyGuidsAttribute.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 Guid[] { } }; | ||
yield return new object[] { new Guid?[] { } }; | ||
yield return new object[] { new string[] { } }; | ||
yield return new object[] { new string?[] { } }; | ||
yield return new object[] { new Guid[] { new("f8daff85-4393-42ae-9ab5-8620ab20c8da") } }; | ||
yield return new object[] { new string[] { "d78b48f9-37b8-47dd-8e47-0325dd3e7899" } }; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} | ||
|
||
private class InvalidData : IEnumerable<object?[]> | ||
{ | ||
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 } }; | ||
yield return new object[] { new Guid[] { default, new("846398d2-416d-4f05-86b0-431e11117abc") } }; | ||
yield return new object[] { new Guid[] { Guid.Empty, new("08bfec4a-da43-40de-a15a-0d10f227e6b7") } }; | ||
yield return new object[] { new Guid?[] { default, new Guid("2dd81281-7498-411c-aea1-5cec96f2dd8d") } }; | ||
yield return new object[] { new string[] { "00000000-0000-0000-0000-000000000000", "7d8c61f7-a334-4599-b801-45f0934099a4" } }; | ||
yield return new object[] { new string[] { string.Empty, "7c3cdfba-4466-44ab-bb95-da02eac67380" } }; | ||
yield return new object[] { new string?[] { default, "f8daff85-4393-42ae-9ab5-8620ab20c8da" } }; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} | ||
|
||
private class SomeClass | ||
{ | ||
[ContainsOnlyNonEmptyGuids] | ||
public Guid[] Values { get; set; } = new[] { Guid.Empty }; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...orkleap.ComponentModel.DataAnnotations.Tests/ContainsOnlyNonEmptyStringsAttributeTests.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,78 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Globalization; | ||
|
||
namespace Workleap.ComponentModel.DataAnnotations.Tests; | ||
|
||
public sealed class ContainsOnlyNonEmptyStringsAttributeTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(ValidData))] | ||
public void Given_ValidStrings_When_Validate_Then_Valid(object? inputs) | ||
{ | ||
var attr = new ContainsOnlyNonEmptyStringsAttribute(); | ||
Assert.True(attr.IsValid(inputs)); | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(InvalidData))] | ||
public void Given_InvalidGuids_When_Validate_Then_Invalid(object? inputs) | ||
{ | ||
var attr = new ContainsOnlyNonEmptyStringsAttribute(); | ||
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, ContainsOnlyNonEmptyStringsAttribute.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?[] { } }; | ||
yield return new object[] { new string[] { } }; | ||
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" } }; | ||
} | ||
|
||
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[] { " " } }; | ||
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 SomeClass | ||
{ | ||
[ContainsOnlyNonEmptyStrings] | ||
public string?[] Values { get; set; } = new[] { string.Empty }; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Workleap.ComponentModel.DataAnnotations/ContainsOnlyNonEmptyGuidsAttribute.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,35 @@ | ||
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 ContainsOnlyNonEmptyGuidsAttribute : ValidationAttribute | ||
{ | ||
internal const string ErrorMessageFormat = "The field {0} must be an collection that contains only non-empty GUIDs"; | ||
|
||
public ContainsOnlyNonEmptyGuidsAttribute() : base(ErrorMessageFormat) | ||
{ | ||
} | ||
|
||
public override bool IsValid(object? value) => value switch | ||
{ | ||
null => true, | ||
IEnumerable<Guid> enumerable => enumerable.All(this.IsValidGuid), | ||
IEnumerable<Guid?> enumerable => enumerable.All(x => x != null && this.IsValidGuid(x.Value)), | ||
IEnumerable<string> enumerable => enumerable.All(this.IsValidGuid), | ||
_ => false, | ||
}; | ||
|
||
private bool IsValidGuid(string valueAsString) | ||
{ | ||
return Guid.TryParse(valueAsString, out var guid) && this.IsValidGuid(guid); | ||
} | ||
|
||
private bool IsValidGuid(Guid guid) | ||
{ | ||
return guid != Guid.Empty; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Workleap.ComponentModel.DataAnnotations/ContainsOnlyNonEmptyStringsAttribute.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 ContainsOnlyNonEmptyStringsAttribute : ValidationAttribute | ||
{ | ||
internal const string ErrorMessageFormat = "The field {0} must be a collection that contains only non-empty Strings"; | ||
|
||
public ContainsOnlyNonEmptyStringsAttribute() : base(ErrorMessageFormat) | ||
{ | ||
} | ||
|
||
public override bool IsValid(object? value) => value switch | ||
{ | ||
null => true, | ||
IEnumerable<string> enumerable => enumerable.All(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
8 changes: 7 additions & 1 deletion
8
src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Unshipped.txt
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
#nullable enable | ||
#nullable enable | ||
Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyGuidsAttribute | ||
Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyGuidsAttribute.ContainsOnlyNonEmptyGuidsAttribute() -> void | ||
override Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyGuidsAttribute.IsValid(object? value) -> bool | ||
Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyStringsAttribute | ||
Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyStringsAttribute.ContainsOnlyNonEmptyStringsAttribute() -> void | ||
override Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyStringsAttribute.IsValid(object? value) -> bool |