| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="LocalizedRuleBuilderExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using FluentValidation; |
| | | 10 | | using MyNet.Observable.Localization; |
| | | 11 | | using MyNet.Observable.Validation; |
| | | 12 | | using MyNet.Primitives; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 15 | | namespace MyNet.Observable; |
| | | 16 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// FluentValidation extensions that apply <see cref="ValidationResources"/> messages. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class LocalizedRuleBuilderExtensions |
| | | 22 | | { |
| | | 23 | | extension<T, TProperty>(IRuleBuilderOptions<T, TProperty> rule) |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Sets the error message for the rule to a localized "field is required" message, using the provided property |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 29 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 30 | | public IRuleBuilderOptions<T, TProperty> WithRequiredLocalizedMessage(string propertyDisplayKey) |
| | 12 | 31 | | => rule.WithLocalizedMessage(ValidationResources.FieldIsRequired, propertyDisplayKey); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Sets the error message for the rule to a localized "field exceeds maximum length" message, using the provide |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="maxLength">The maximum length allowed for the property.</param> |
| | | 37 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 38 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 39 | | public IRuleBuilderOptions<T, TProperty> WithMaxLengthLocalizedMessage(int maxLength, string propertyDisplayKey) |
| | 9 | 40 | | => rule.WithLocalizedMessage(ValidationResources.FieldExceedsMaxLength, propertyDisplayKey, maxLength); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Sets the error message for the rule to a localized "field is not a valid email address" message, using the p |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 46 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 47 | | public IRuleBuilderOptions<T, TProperty> WithValidEmailLocalizedMessage(string propertyDisplayKey) |
| | 0 | 48 | | => rule.WithLocalizedMessage(ValidationResources.FieldInvalidEmail, propertyDisplayKey); |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Sets the error message for the rule to a localized "field is not a valid phone number" message, using the pr |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 54 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 55 | | public IRuleBuilderOptions<T, TProperty> WithValidPhoneNumberLocalizedMessage(string propertyDisplayKey) |
| | 0 | 56 | | => rule.WithLocalizedMessage(ValidationResources.FieldInvalidPhoneNumber, propertyDisplayKey); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Sets the error message for the rule to a localized "field must be between" message, using the provided prope |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="min">The minimum value allowed for the property.</param> |
| | | 62 | | /// <param name="max">The maximum value allowed for the property.</param> |
| | | 63 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 64 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 65 | | public IRuleBuilderOptions<T, TProperty> WithBetweenLocalizedMessage<TComparable>( |
| | | 66 | | TComparable min, |
| | | 67 | | TComparable max, |
| | | 68 | | string propertyDisplayKey) |
| | | 69 | | where TComparable : IComparable<TComparable>, IComparable |
| | 9 | 70 | | => rule.WithLocalizedMessage(ValidationResources.FieldMustBeBetween, propertyDisplayKey, min, max); |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Sets the error message for the rule to a localized "field must be less than or equal to" message, using the |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="maximum">The maximum value allowed for the property.</param> |
| | | 76 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 77 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 78 | | public IRuleBuilderOptions<T, TProperty> WithLessThanOrEqualLocalizedMessage<TComparable>( |
| | | 79 | | TComparable maximum, |
| | | 80 | | string propertyDisplayKey) |
| | | 81 | | where TComparable : IComparable<TComparable>, IComparable |
| | 0 | 82 | | => rule.WithLocalizedMessage(ValidationResources.FieldMustBeLessThanOrEqualTo, propertyDisplayKey, maximum); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Sets the error message for the rule to a localized "field must be greater than or equal to" message, using t |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="minimum">The minimum value allowed for the property.</param> |
| | | 88 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 89 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 90 | | public IRuleBuilderOptions<T, TProperty> WithGreaterThanOrEqualLocalizedMessage<TComparable>( |
| | | 91 | | TComparable minimum, |
| | | 92 | | string propertyDisplayKey) |
| | | 93 | | where TComparable : IComparable<TComparable>, IComparable |
| | 0 | 94 | | => rule.WithLocalizedMessage(ValidationResources.FieldMustBeGreaterThanOrEqualTo, propertyDisplayKey, minimu |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Sets the error message for the rule to a localized "field must be less than" message, using the provided pro |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="maximum">The maximum value allowed for the property.</param> |
| | | 100 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 101 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 102 | | public IRuleBuilderOptions<T, TProperty> WithLessThanLocalizedMessage<TComparable>( |
| | | 103 | | TComparable maximum, |
| | | 104 | | string propertyDisplayKey) |
| | | 105 | | where TComparable : IComparable<TComparable>, IComparable |
| | 0 | 106 | | => rule.WithLocalizedMessage(ValidationResources.FieldMustBeLessThan, propertyDisplayKey, maximum); |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Sets the error message for the rule to a localized "field must be greater than" message, using the provided |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="minimum">The minimum value allowed for the property.</param> |
| | | 112 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 113 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 114 | | public IRuleBuilderOptions<T, TProperty> WithGreaterThanLocalizedMessage<TComparable>( |
| | | 115 | | TComparable minimum, |
| | | 116 | | string propertyDisplayKey) |
| | | 117 | | where TComparable : IComparable<TComparable>, IComparable |
| | 0 | 118 | | => rule.WithLocalizedMessage(ValidationResources.FieldMustBeGreaterThan, propertyDisplayKey, minimum); |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Sets the error message for the rule to a localized "field must be in the past" message, using the provided p |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 124 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 125 | | public IRuleBuilderOptions<T, TProperty> WithInPastLocalizedMessage(string propertyDisplayKey) |
| | 0 | 126 | | => rule.WithLocalizedMessage(ValidationResources.FieldMustBeInPast, propertyDisplayKey); |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Sets the error message for the rule to a localized "collection requires at least one item" message, using th |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 132 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 133 | | public IRuleBuilderOptions<T, TProperty> WithNotEmptyCollectionLocalizedMessage(string propertyDisplayKey) |
| | 0 | 134 | | => rule.WithLocalizedMessage(ValidationResources.CollectionRequiresAtLeastOneItem, propertyDisplayKey); |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Sets the error message for the rule to a localized "collection contains duplicate items" message, using the |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 140 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 141 | | public IRuleBuilderOptions<T, TProperty> WithUniqueItemsLocalizedMessage(string propertyDisplayKey) |
| | 0 | 142 | | => rule.WithLocalizedMessage(ValidationResources.CollectionDuplicateItems, propertyDisplayKey); |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Sets the error message for the rule to a localized "invalid file path" message, using the provided property |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 148 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 149 | | public IRuleBuilderOptions<T, TProperty> WithValidFilePathLocalizedMessage(string propertyDisplayKey) |
| | 0 | 150 | | => rule.WithLocalizedMessage(ValidationResources.FieldInvalidFilePath, propertyDisplayKey); |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Sets the error message for the rule to a localized "file not found" message, using the provided property dis |
| | | 154 | | /// </summary> |
| | | 155 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 156 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 157 | | public IRuleBuilderOptions<T, TProperty> WithExistingFileLocalizedMessage(string propertyDisplayKey) |
| | 0 | 158 | | => rule.WithLocalizedMessage(ValidationResources.FieldFileNotFound, propertyDisplayKey); |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Sets the error message for the rule to a localized "folder not found" message, using the provided path selec |
| | | 162 | | /// </summary> |
| | | 163 | | /// <param name="pathSelector">A function that selects the path to be validated.</param> |
| | | 164 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 165 | | public IRuleBuilderOptions<T, TProperty> WithExistingFolderLocalizedMessage(Func<T, string?> pathSelector) |
| | 0 | 166 | | => rule.WithMessage(instance => |
| | 0 | 167 | | ValidationResources.PathPartNotFound.FormatWith( |
| | 0 | 168 | | CultureInfo.CurrentCulture, |
| | 0 | 169 | | pathSelector(instance) ?? string.Empty)); |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Sets the error message for the rule to a localized message, using the provided property display key and addi |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="template">The template for the localized message.</param> |
| | | 175 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 176 | | /// <param name="additionalArgs">Additional arguments for the message template.</param> |
| | | 177 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 178 | | public IRuleBuilderOptions<T, TProperty> WithLocalizedMessage( |
| | | 179 | | string template, |
| | | 180 | | string propertyDisplayKey, |
| | | 181 | | params object?[] additionalArgs) |
| | 30 | 182 | | => rule.WithMessage(_ => ValidationMessages.FormatForPropertyKey(template, propertyDisplayKey, additionalArg |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Sets the error message for the rule to a localized message, using the provided property display key and a fu |
| | | 186 | | /// </summary> |
| | | 187 | | /// <param name="template">The template for the localized message.</param> |
| | | 188 | | /// <param name="propertyDisplayKey">The key used to look up the localized display name of the property.</param> |
| | | 189 | | /// <param name="additionalArgs">A function that generates additional arguments for the message template.</param |
| | | 190 | | /// <returns>The rule builder with the localized error message applied.</returns> |
| | | 191 | | public IRuleBuilderOptions<T, TProperty> WithLocalizedMessage( |
| | | 192 | | string template, |
| | | 193 | | string propertyDisplayKey, |
| | | 194 | | Func<T, object?[]> additionalArgs) |
| | 0 | 195 | | => rule.WithMessage(instance => |
| | 0 | 196 | | ValidationMessages.FormatForPropertyKey(template, propertyDisplayKey, additionalArgs(instance))); |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | |