< Summary

Information
Class: MyNet.Observable.LocalizedRuleBuilderExtensions
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Validation/Extensions/LocalizedRuleBuilderExtensions.cs
Tag: 323_28699572109
Line coverage
19%
Covered lines: 4
Uncovered lines: 17
Coverable lines: 21
Total lines: 199
Line coverage: 19%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Validation/Extensions/LocalizedRuleBuilderExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="LocalizedRuleBuilderExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using FluentValidation;
 10using MyNet.Observable.Localization;
 11using MyNet.Observable.Validation;
 12using MyNet.Primitives;
 13
 14#pragma warning disable IDE0130 // Namespace does not match folder structure
 15namespace 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>
 21public 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)
 1231            => 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)
 940            => 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)
 048            => 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)
 056            => 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
 970            => 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
 082            => 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
 094            => 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
 0106            => 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
 0118            => 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)
 0126            => 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)
 0134            => 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)
 0142            => 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)
 0150            => 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)
 0158            => 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)
 0166            => rule.WithMessage(instance =>
 0167                ValidationResources.PathPartNotFound.FormatWith(
 0168                    CultureInfo.CurrentCulture,
 0169                    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)
 30182            => 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)
 0195            => rule.WithMessage(instance =>
 0196                ValidationMessages.FormatForPropertyKey(template, propertyDisplayKey, additionalArgs(instance)));
 197    }
 198}
 199

Methods/Properties

WithRequiredLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithMaxLengthLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.Int32,System.String)
WithValidEmailLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithValidPhoneNumberLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithBetweenLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,TComparable,TComparable,System.String)
WithLessThanOrEqualLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,TComparable,System.String)
WithGreaterThanOrEqualLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,TComparable,System.String)
WithLessThanLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,TComparable,System.String)
WithGreaterThanLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,TComparable,System.String)
WithInPastLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithNotEmptyCollectionLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithUniqueItemsLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithValidFilePathLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithExistingFileLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String)
WithExistingFolderLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.Func`2<T,System.String>)
WithLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String,System.String,System.Object[])
WithLocalizedMessage(FluentValidation.IRuleBuilderOptions`2<T,TProperty>,System.String,System.String,System.Func`2<T,System.Object[]>)