| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="LocalizedStringRuleBuilderExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using FluentValidation; |
| | | 10 | | using MyNet.Observable.Validation; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Observable; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Localized validation rules for string properties. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class LocalizedStringRuleBuilderExtensions |
| | | 20 | | { |
| | | 21 | | extension<T>(LocalizedRuleBuilder<T, string> rule) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Adds a "not empty" validation rule with a localized message indicating that the field is required. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <returns>The rule builder options.</returns> |
| | | 27 | | public IRuleBuilderOptions<T, string> NotEmptyRequired() |
| | | 28 | | => rule.RuleBuilder.NotEmpty().WithRequiredLocalizedMessage(rule.PropertyDisplayKey); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Adds a "maximum length" validation rule with a localized message indicating the maximum allowed length for t |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="maxLength">The maximum allowed length of the field.</param> |
| | | 34 | | /// <returns>The rule builder options.</returns> |
| | | 35 | | public IRuleBuilderOptions<T, string> MaximumLength(int maxLength) |
| | | 36 | | => rule.RuleBuilder.MaximumLength(maxLength).WithMaxLengthLocalizedMessage(maxLength, rule.PropertyDisplayKe |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Adds an "email address" validation rule with a localized message indicating that the field must be a valid e |
| | | 40 | | /// </summary> |
| | | 41 | | /// <returns>The rule builder options.</returns> |
| | | 42 | | public IRuleBuilderOptions<T, string> EmailAddress() |
| | | 43 | | => rule.RuleBuilder.EmailAddress().WithValidEmailLocalizedMessage(rule.PropertyDisplayKey); |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Localized validation rules for collection properties. |
| | | 49 | | /// </summary> |
| | | 50 | | public static class LocalizedCollectionRuleBuilderExtensions |
| | | 51 | | { |
| | | 52 | | extension<T, TElement>(LocalizedRuleBuilder<T, IEnumerable<TElement>> rule) |
| | | 53 | | { |
| | | 54 | | /// <summary> |
| | | 55 | | /// Adds a "not empty" validation rule for collections with a localized message indicating that the collection i |
| | | 56 | | /// </summary> |
| | | 57 | | /// <returns>The rule builder options.</returns> |
| | | 58 | | public IRuleBuilderOptions<T, IEnumerable<TElement>> NotEmptyRequired() |
| | 0 | 59 | | => rule.RuleBuilder.NotEmpty().WithNotEmptyCollectionLocalizedMessage(rule.PropertyDisplayKey); |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Localized validation rules for comparable properties. |
| | | 65 | | /// </summary> |
| | | 66 | | public static class LocalizedComparableRuleBuilderExtensions |
| | | 67 | | { |
| | | 68 | | extension<T, TComparable>(LocalizedRuleBuilder<T, TComparable> rule) |
| | | 69 | | where TComparable : IComparable<TComparable>, IComparable |
| | | 70 | | { |
| | | 71 | | /// <summary> |
| | | 72 | | /// Adds an "inclusive between" validation rule with a localized message indicating that the field must be betwe |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="min">The minimum allowed value.</param> |
| | | 75 | | /// <param name="max">The maximum allowed value.</param> |
| | | 76 | | /// <returns>The rule builder options.</returns> |
| | | 77 | | public IRuleBuilderOptions<T, TComparable> InclusiveBetween(TComparable min, TComparable max) |
| | | 78 | | => rule.RuleBuilder.InclusiveBetween(min, max).WithBetweenLocalizedMessage(min, max, rule.PropertyDisplayKey |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Adds a "less than or equal to" validation rule with a localized message indicating that the field must be le |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="maximum">The maximum allowed value.</param> |
| | | 84 | | /// <returns>The rule builder options.</returns> |
| | | 85 | | public IRuleBuilderOptions<T, TComparable> LessThanOrEqualTo(TComparable maximum) |
| | | 86 | | => rule.RuleBuilder.LessThanOrEqualTo(maximum).WithLessThanOrEqualLocalizedMessage(maximum, rule.PropertyDis |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Adds a "greater than or equal to" validation rule with a localized message indicating that the field must be |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="minimum">The minimum allowed value.</param> |
| | | 92 | | /// <returns>The rule builder options.</returns> |
| | | 93 | | public IRuleBuilderOptions<T, TComparable> GreaterThanOrEqualTo(TComparable minimum) |
| | | 94 | | => rule.RuleBuilder.GreaterThanOrEqualTo(minimum).WithGreaterThanOrEqualLocalizedMessage(minimum, rule.Prope |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Localized validation rules for date properties. |
| | | 100 | | /// </summary> |
| | | 101 | | public static class LocalizedDateRuleBuilderExtensions |
| | | 102 | | { |
| | | 103 | | extension<T>(LocalizedRuleBuilder<T, DateTime> rule) |
| | | 104 | | { |
| | | 105 | | /// <summary> |
| | | 106 | | /// Adds a "less than" validation rule with a localized message indicating that the date must be in the past, i. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <returns>The rule builder options.</returns> |
| | | 109 | | public IRuleBuilderOptions<T, DateTime> InPast() |
| | | 110 | | => rule.RuleBuilder.LessThan(DateTime.Now).WithInPastLocalizedMessage(rule.PropertyDisplayKey); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | extension<T>(LocalizedRuleBuilder<T, DateTimeOffset> rule) |
| | | 114 | | { |
| | | 115 | | /// <summary> |
| | | 116 | | /// Adds a "less than" validation rule with a localized message indicating that the date must be in the past, i. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <returns>The rule builder options.</returns> |
| | | 119 | | public IRuleBuilderOptions<T, DateTimeOffset> InPast() |
| | | 120 | | => rule.RuleBuilder.LessThan(DateTimeOffset.Now).WithInPastLocalizedMessage(rule.PropertyDisplayKey); |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |