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-1261 - Added timespan max/min attributes #71

Merged
merged 3 commits into from
Jun 4, 2024
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,26 @@
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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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));
}
}
Original file line number Diff line number Diff line change
@@ -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)
{
}
}
Original file line number Diff line number Diff line change
@@ -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))
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
override Workleap.ComponentModel.DataAnnotations.ContainsOnlyNonEmptyStringsAttribute.IsValid(object? value) -> bool
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#nullable enable
#nullable enable