< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: FluentValidation.FluentValidationDateOnlyHelpers
Assembly: ZhEaIsNsAaBn.Utilities.FluentValidation
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.FluentValidation/FluentValidationDateOnlyHelpers.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 122
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
GreaterThan(...)100%11100%
GreaterThanOrEqualTo(...)100%11100%
GreaterThanOrEqualToToday(...)100%11100%
GreaterThanToday(...)100%11100%
IsNullableValidDate(...)100%11100%
IsValidDate(...)100%11100%
IsValidDate(...)100%11100%
IsValidDate(...)100%44100%
LessThan(...)100%11100%
LessThanOrEqualTo(...)100%11100%
LessThanOrEqualToToday(...)100%11100%
LessThanToday(...)100%11100%
NullableGreaterThanOrEqualToToday(...)100%11100%
get_Today()100%11100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.FluentValidation/FluentValidationDateOnlyHelpers.cs

#LineLine coverage
 1// ReSharper disable once CheckNamespace
 2namespace FluentValidation;
 3
 4/// <summary>Rules for <see cref="DateOnly"/> values and for Gregorian date text. "Today" is <see cref="DateTime.Today"/
 5public static class FluentValidationDateOnlyHelpers
 6{
 37    private static readonly string[] _dateFormats =
 38        [
 39            "yyyy/MM/dd", "yyyy/M/d", "dd/MM/yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy",
 310            "yyyy-MM-dd", "yyyy-M-d", "dd-MM-yyyy", "d-M-yyyy", "dd-M-yyyy", "d-MM-yyyy",
 311            "yyyy MM dd", "yyyy M d", "dd MM yyyy", "d M yyyy", "dd M yyyy", "d MM yyyy"
 312        ];
 13
 14
 15    /// <summary>The date must be after <paramref name="comparer"/>.</summary>
 16    /// <typeparam name="T">The validated type.</typeparam>
 17    /// <param name="ruleBuilder">The rule builder.</param>
 18    /// <param name="comparer">The date to compare with.</param>
 19    /// <returns>The rule builder options.</returns>
 20    public static IRuleBuilderOptions<T, DateOnly> GreaterThan< T >( this IRuleBuilder<T, DateOnly> ruleBuilder, DateOnl
 321        => ruleBuilder.Must( value => value > comparer ).WithErrorCode( nameof( GreaterThan ) );
 22
 23    /// <summary>The date must be on or after <paramref name="comparer"/>.</summary>
 24    /// <typeparam name="T">The validated type.</typeparam>
 25    /// <param name="ruleBuilder">The rule builder.</param>
 26    /// <param name="comparer">The date to compare with.</param>
 27    /// <returns>The rule builder options.</returns>
 28    public static IRuleBuilderOptions<T, DateOnly> GreaterThanOrEqualTo< T >( this IRuleBuilder<T, DateOnly> ruleBuilder
 329        => ruleBuilder.Must( value => value >= comparer ).WithErrorCode( nameof( GreaterThanOrEqualTo ) );
 30
 31    /// <summary>The date must be today or later.</summary>
 32    /// <typeparam name="T">The validated type.</typeparam>
 33    /// <param name="ruleBuilder">The rule builder.</param>
 34    /// <returns>The rule builder options.</returns>
 35    public static IRuleBuilderOptions<T, DateOnly> GreaterThanOrEqualToToday< T >( this IRuleBuilder<T, DateOnly> ruleBu
 636        => ruleBuilder.Must( value => value >= Today ).WithErrorCode( nameof( GreaterThanOrEqualToToday ) );
 37
 38    /// <summary>The date must be after today.</summary>
 39    /// <typeparam name="T">The validated type.</typeparam>
 40    /// <param name="ruleBuilder">The rule builder.</param>
 41    /// <returns>The rule builder options.</returns>
 342    public static IRuleBuilderOptions<T, DateOnly> GreaterThanToday< T >( this IRuleBuilder<T, DateOnly> ruleBuilder ) =
 43
 44    /// <summary>The text must be empty or a valid Gregorian date (see <see cref="IsValidDate(string)"/>).</summary>
 45    /// <typeparam name="T">The validated type.</typeparam>
 46    /// <param name="ruleBuilder">The rule builder.</param>
 47    /// <returns>The rule builder options.</returns>
 48    public static IRuleBuilderOptions<T, string> IsNullableValidDate< T >( this IRuleBuilder<T, string> ruleBuilder )
 349        => ruleBuilder.Must( value => string.IsNullOrWhiteSpace( value ) || IsValidDate( value ) ).WithErrorCode( nameof
 50
 51    /// <summary>The text must be a valid Gregorian date; when <paramref name="nullable"/> returns <see langword="true"/
 52    /// <typeparam name="T">The validated type.</typeparam>
 53    /// <param name="ruleBuilder">The rule builder.</param>
 54    /// <param name="nullable">Decides, per instance, whether an empty value is allowed.</param>
 55    /// <returns>The rule builder options.</returns>
 56    public static IRuleBuilderOptions<T, string> IsValidDate< T >( this IRuleBuilder<T, string> ruleBuilder, Func<T, boo
 2457        => ruleBuilder.Must( ( entity, value ) => ( ( nullable?.Invoke( entity ) ?? false ) && string.IsNullOrWhiteSpace
 58
 59    /// <summary>The text must be a valid Gregorian date (see <see cref="IsValidDate(string)"/>).</summary>
 60    /// <typeparam name="T">The validated type.</typeparam>
 61    /// <param name="ruleBuilder">The rule builder.</param>
 62    /// <returns>The rule builder options.</returns>
 363    public static IRuleBuilderOptions<T, string> IsValidDate< T >( this IRuleBuilder<T, string> ruleBuilder ) => ruleBui
 64
 65    /// <summary>
 66    /// Checks that <paramref name="value"/> is a Gregorian date in a supported format (<c>yyyy/MM/dd</c>, <c>dd-MM-yyyy
 67    /// <c>yyyy MM dd</c>, … with <c>/</c>, <c>-</c> or space) and its year is between 1900 and 2100.
 68    /// </summary>
 69    /// <param name="value">The text.</param>
 70    /// <returns><see langword="true"/> when it is such a date.</returns>
 71    public static bool IsValidDate( string? value )
 72    {
 3973        if ( string.IsNullOrWhiteSpace( value ) ) { return false; }
 74
 75
 76        try
 77        {
 3378            var date = DateTime.ParseExact( value, _dateFormats, CultureInfo.GetCultureInfo( "en-US" ).DateTimeFormat, D
 79
 80
 1581            return date.Year is >= 1900 and <= 2100;
 82        }
 3683        catch ( FormatException ) { return false; }
 3384    }
 85
 86    /// <summary>The date must be before <paramref name="comparer"/>.</summary>
 87    /// <typeparam name="T">The validated type.</typeparam>
 88    /// <param name="ruleBuilder">The rule builder.</param>
 89    /// <param name="comparer">The date to compare with.</param>
 90    /// <returns>The rule builder options.</returns>
 391    public static IRuleBuilderOptions<T, DateOnly> LessThan< T >( this IRuleBuilder<T, DateOnly> ruleBuilder, DateOnly c
 92
 93    /// <summary>The date must be on or before <paramref name="comparer"/>.</summary>
 94    /// <typeparam name="T">The validated type.</typeparam>
 95    /// <param name="ruleBuilder">The rule builder.</param>
 96    /// <param name="comparer">The date to compare with.</param>
 97    /// <returns>The rule builder options.</returns>
 98    public static IRuleBuilderOptions<T, DateOnly> LessThanOrEqualTo< T >( this IRuleBuilder<T, DateOnly> ruleBuilder, D
 399        => ruleBuilder.Must( value => value <= comparer ).WithErrorCode( nameof( LessThanOrEqualTo ) );
 100
 101    /// <summary>The date must be today or earlier.</summary>
 102    /// <typeparam name="T">The validated type.</typeparam>
 103    /// <param name="ruleBuilder">The rule builder.</param>
 104    /// <returns>The rule builder options.</returns>
 6105    public static IRuleBuilderOptions<T, DateOnly> LessThanOrEqualToToday< T >( this IRuleBuilder<T, DateOnly> ruleBuild
 106
 107    /// <summary>The date must be before today.</summary>
 108    /// <typeparam name="T">The validated type.</typeparam>
 109    /// <param name="ruleBuilder">The rule builder.</param>
 110    /// <returns>The rule builder options.</returns>
 3111    public static IRuleBuilderOptions<T, DateOnly> LessThanToday< T >( this IRuleBuilder<T, DateOnly> ruleBuilder ) => r
 112
 113    /// <summary>The date must be empty or today or later.</summary>
 114    /// <typeparam name="T">The validated type.</typeparam>
 115    /// <param name="ruleBuilder">The rule builder.</param>
 116    /// <returns>The rule builder options.</returns>
 117    public static IRuleBuilderOptions<T, DateOnly?> NullableGreaterThanOrEqualToToday< T >( this IRuleBuilder<T, DateOnl
 3118        => ruleBuilder.Must( value => value is null || value >= Today ).WithErrorCode( nameof( NullableGreaterThanOrEqua
 119
 120
 33121    private static DateOnly Today => DateOnly.FromDateTime( DateTime.Today );
 122}