| | | 1 | | // ReSharper disable once CheckNamespace |
| | | 2 | | namespace FluentValidation; |
| | | 3 | | |
| | | 4 | | /// <summary>Rules for <see cref="DateOnly"/> values and for Gregorian date text. "Today" is <see cref="DateTime.Today"/ |
| | | 5 | | public static class FluentValidationDateOnlyHelpers |
| | | 6 | | { |
| | 3 | 7 | | private static readonly string[] _dateFormats = |
| | 3 | 8 | | [ |
| | 3 | 9 | | "yyyy/MM/dd", "yyyy/M/d", "dd/MM/yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", |
| | 3 | 10 | | "yyyy-MM-dd", "yyyy-M-d", "dd-MM-yyyy", "d-M-yyyy", "dd-M-yyyy", "d-MM-yyyy", |
| | 3 | 11 | | "yyyy MM dd", "yyyy M d", "dd MM yyyy", "d M yyyy", "dd M yyyy", "d MM yyyy" |
| | 3 | 12 | | ]; |
| | | 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 |
| | 3 | 21 | | => 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 |
| | 3 | 29 | | => 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 |
| | 6 | 36 | | => 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> |
| | 3 | 42 | | 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 ) |
| | 3 | 49 | | => 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 |
| | 24 | 57 | | => 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> |
| | 3 | 63 | | 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 | | { |
| | 39 | 73 | | if ( string.IsNullOrWhiteSpace( value ) ) { return false; } |
| | | 74 | | |
| | | 75 | | |
| | | 76 | | try |
| | | 77 | | { |
| | 33 | 78 | | var date = DateTime.ParseExact( value, _dateFormats, CultureInfo.GetCultureInfo( "en-US" ).DateTimeFormat, D |
| | | 79 | | |
| | | 80 | | |
| | 15 | 81 | | return date.Year is >= 1900 and <= 2100; |
| | | 82 | | } |
| | 36 | 83 | | catch ( FormatException ) { return false; } |
| | 33 | 84 | | } |
| | | 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> |
| | 3 | 91 | | 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 |
| | 3 | 99 | | => 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> |
| | 6 | 105 | | 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> |
| | 3 | 111 | | 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 |
| | 3 | 118 | | => ruleBuilder.Must( value => value is null || value >= Today ).WithErrorCode( nameof( NullableGreaterThanOrEqua |
| | | 119 | | |
| | | 120 | | |
| | 33 | 121 | | private static DateOnly Today => DateOnly.FromDateTime( DateTime.Today ); |
| | | 122 | | } |