From 5b4f7f956931ab43fffdc5494f34f0e29a65d6a5 Mon Sep 17 00:00:00 2001 From: "guillaume.caya" Date: Tue, 4 Jun 2024 11:52:43 -0400 Subject: [PATCH 1/2] Added timespan max/min attributes --- .../MaxTimeSpanValueAttributeTests.cs | 28 +++++++++++++++++++ .../MinTimeSpanValueAttributeTests.cs | 28 +++++++++++++++++++ .../MaxTimeSpanValueAttribute.cs | 14 ++++++++++ .../MinTimeSpanValueAttribute.cs | 14 ++++++++++ .../PublicAPI.Shipped.txt | 6 +++- .../PublicAPI.Unshipped.txt | 2 +- 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs create mode 100644 src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs create mode 100644 src/Workleap.ComponentModel.DataAnnotations/MaxTimeSpanValueAttribute.cs create mode 100644 src/Workleap.ComponentModel.DataAnnotations/MinTimeSpanValueAttribute.cs diff --git a/src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs b/src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs new file mode 100644 index 0000000..d31e7f6 --- /dev/null +++ b/src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs @@ -0,0 +1,28 @@ +using System; + +namespace Workleap.ComponentModel.DataAnnotations.Tests; + +public class MaxTimeSpanValueAttributeTests +{ + [Theory] + [InlineData("49:00:00", "50:00:00")] + [InlineData("-51:00:00", "-50:00:00")] + public void GivenValue_ShouldFailValidation_WhenValueIsInvalid(string maxValue, string valueBeingValidated) + { + var maxValueAttribute = new MaxTimeSpanValueAttribute(maxValue); + + Assert.False(maxValueAttribute.IsValid(valueBeingValidated)); + } + + [Theory] + [InlineData("50:00:00", "49:00:00")] + [InlineData("10675199.02:48:05.4775807", "49:00:00")] + [InlineData("-50:00:00", "-51:00:00")] + [InlineData("10675199.02:48:05.4775807", "-10675199.02:48:05.4775808")] + public void GivenValue_ShouldPassValidation_WhenValueIsValid(string maxValue, string valueBeingValidated) + { + var maxValueAttribute = new MaxTimeSpanValueAttribute(maxValue); + + Assert.True(maxValueAttribute.IsValid(valueBeingValidated)); + } +} diff --git a/src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs b/src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs new file mode 100644 index 0000000..e1018be --- /dev/null +++ b/src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs @@ -0,0 +1,28 @@ +using System; + +namespace Workleap.ComponentModel.DataAnnotations.Tests; + +public class MinTimeSpanValueAttributeTests +{ + [Theory] + [InlineData("50:00:00", "49:00:00")] + [InlineData("-50:00:00", "-51:00:00")] + public void GivenValue_ShouldFailValidation_WhenValueIsInvalid(string minValue, string valueBeingValidated) + { + var maxValueAttribute = new MinTimeSpanValueAttribute(minValue); + + Assert.False(maxValueAttribute.IsValid(valueBeingValidated)); + } + + [Theory] + [InlineData("49:00:00", "50:00:00")] + [InlineData("49:00:00", "10675199.02:48:05.4775807")] + [InlineData("-51:00:00", "-50:00:00")] + [InlineData("-10675199.02:48:05.4775808", "10675199.02:48:05.4775807")] + public void GivenValue_ShouldPassValidation_WhenValueIsValid(string minValue, string valueBeingValidated) + { + var maxValueAttribute = new MinTimeSpanValueAttribute(minValue); + + Assert.True(maxValueAttribute.IsValid(valueBeingValidated)); + } +} diff --git a/src/Workleap.ComponentModel.DataAnnotations/MaxTimeSpanValueAttribute.cs b/src/Workleap.ComponentModel.DataAnnotations/MaxTimeSpanValueAttribute.cs new file mode 100644 index 0000000..403ccaf --- /dev/null +++ b/src/Workleap.ComponentModel.DataAnnotations/MaxTimeSpanValueAttribute.cs @@ -0,0 +1,14 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Globalization; + +namespace Workleap.ComponentModel.DataAnnotations; + +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)] +public sealed class MaxTimeSpanValueAttribute : RangeAttribute +{ + public MaxTimeSpanValueAttribute(string maximum) + : base(typeof(TimeSpan), TimeSpan.MinValue.ToString("c", CultureInfo.InvariantCulture), maximum) + { + } +} \ No newline at end of file diff --git a/src/Workleap.ComponentModel.DataAnnotations/MinTimeSpanValueAttribute.cs b/src/Workleap.ComponentModel.DataAnnotations/MinTimeSpanValueAttribute.cs new file mode 100644 index 0000000..49eeea3 --- /dev/null +++ b/src/Workleap.ComponentModel.DataAnnotations/MinTimeSpanValueAttribute.cs @@ -0,0 +1,14 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Globalization; + +namespace Workleap.ComponentModel.DataAnnotations; + +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)] +public sealed class MinTimeSpanValueAttribute : RangeAttribute +{ + public MinTimeSpanValueAttribute(string minimum) + : base(typeof(TimeSpan), minimum, TimeSpan.MaxValue.ToString("c", CultureInfo.InvariantCulture)) + { + } +} \ No newline at end of file diff --git a/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Shipped.txt b/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Shipped.txt index d56f1fc..4327969 100644 --- a/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Shipped.txt +++ b/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Shipped.txt @@ -56,6 +56,10 @@ override Workleap.ComponentModel.DataAnnotations.TextBasedValidationAttribute.Is override Workleap.ComponentModel.DataAnnotations.TimeSpanAttribute.IsValid(object? value) -> bool override Workleap.ComponentModel.DataAnnotations.UrlOfKindAttribute.IsValid(object? value) -> bool override Workleap.ComponentModel.DataAnnotations.ValidatePropertiesAttribute.IsValid(object? value) -> bool +Workleap.ComponentModel.DataAnnotations.MaxTimeSpanValueAttribute +Workleap.ComponentModel.DataAnnotations.MaxTimeSpanValueAttribute.MaxTimeSpanValueAttribute(string! maximum) -> void +Workleap.ComponentModel.DataAnnotations.MinTimeSpanValueAttribute +Workleap.ComponentModel.DataAnnotations.MinTimeSpanValueAttribute.MinTimeSpanValueAttribute(string! minimum) -> void Workleap.ComponentModel.DataAnnotations.MaxValueAttribute Workleap.ComponentModel.DataAnnotations.MaxValueAttribute.MaxValueAttribute(double maximum) -> void Workleap.ComponentModel.DataAnnotations.MaxValueAttribute.MaxValueAttribute(int maximum) -> void @@ -69,4 +73,4 @@ Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyGuidsAttribute.Conta 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 \ No newline at end of file +override Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyStringsAttribute.IsValid(object? value) -> bool diff --git a/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Unshipped.txt b/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Unshipped.txt index 815c920..7dc5c58 100644 --- a/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Unshipped.txt +++ b/src/Workleap.ComponentModel.DataAnnotations/PublicAPI.Unshipped.txt @@ -1 +1 @@ -#nullable enable \ No newline at end of file +#nullable enable From 11cae24e7c729580e2b3664ba00ac3e4d7ae417c Mon Sep 17 00:00:00 2001 From: "guillaume.caya" Date: Tue, 4 Jun 2024 14:51:30 -0400 Subject: [PATCH 2/2] Fix --- .../MaxTimeSpanValueAttributeTests.cs | 2 -- .../MinTimeSpanValueAttributeTests.cs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs b/src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs index d31e7f6..86ba51d 100644 --- a/src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs +++ b/src/Workleap.ComponentModel.DataAnnotations.Tests/MaxTimeSpanValueAttributeTests.cs @@ -1,5 +1,3 @@ -using System; - namespace Workleap.ComponentModel.DataAnnotations.Tests; public class MaxTimeSpanValueAttributeTests diff --git a/src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs b/src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs index e1018be..9f41a1f 100644 --- a/src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs +++ b/src/Workleap.ComponentModel.DataAnnotations.Tests/MinTimeSpanValueAttributeTests.cs @@ -1,5 +1,3 @@ -using System; - namespace Workleap.ComponentModel.DataAnnotations.Tests; public class MinTimeSpanValueAttributeTests