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

IDP-666 - Added guid enumerable attribute #36

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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,80 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace Workleap.ComponentModel.DataAnnotations.Tests;

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

[Theory]
[ClassData(typeof(InvalidData))]
public void Given_InvalidGuids_When_Validate_Then_Invalid(object? inputs)
{
var attr = new ContainsValidGuidAttribute();
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 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[] { new Guid[] { new("f8daff85-4393-42ae-9ab5-8620ab20c8da") } };
Gcaya marked this conversation as resolved.
Show resolved Hide resolved
yield return new object[] { new string[] { "d78b48f9-37b8-47dd-8e47-0325dd3e7899" } };
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 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" } };
}

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

private class SomeClass
{
[ContainsValidGuid]
public Guid?[] Values { get; set; } = Array.Empty<Guid?>();
}
}
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 ContainsValidGuidAttribute : ValidationAttribute
Gcaya marked this conversation as resolved.
Show resolved Hide resolved
{
internal const string ErrorMessageFormat = "The field {0} must be an enumerable that contains at least one valid GUID";
Gcaya marked this conversation as resolved.
Show resolved Hide resolved

public ContainsValidGuidAttribute() : base(ErrorMessageFormat)
Gcaya marked this conversation as resolved.
Show resolved Hide resolved
{
}

public override bool IsValid(object? value) => value switch
{
null => false,
Gcaya marked this conversation as resolved.
Show resolved Hide resolved
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),
_ => 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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.EndsWithAttribute
Workleap.ComponentModel.DataAnnotations.EndsWithAttribute.EndsWithAttribute(string! text) -> void
Workleap.ComponentModel.DataAnnotations.GuidAttribute
Expand Down Expand Up @@ -43,6 +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.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