| | | 1 | | // ReSharper disable once CheckNamespace |
| | | 2 | | namespace FluentValidation; |
| | | 3 | | |
| | | 4 | | /// <summary>General rules: GUID, conditional required values, website and e-mail text.</summary> |
| | | 5 | | public static class FluentValidationHelpers |
| | | 6 | | { |
| | 3 | 7 | | private static readonly Regex _website = new( @"(?i)^(https?://)(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,6}\b |
| | | 8 | | |
| | 3 | 9 | | private static readonly Regex _email = |
| | 3 | 10 | | new( @"^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|""(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\ |
| | 3 | 11 | | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant ); |
| | | 12 | | |
| | | 13 | | |
| | | 14 | | /// <summary>The GUID must not be <see cref="Guid.Empty"/>.</summary> |
| | | 15 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 16 | | /// <param name="ruleBuilder">The rule builder.</param> |
| | | 17 | | /// <returns>The rule builder options.</returns> |
| | 6 | 18 | | public static IRuleBuilderOptions<T, Guid> NotEmptyGuid< T >( this IRuleBuilder<T, Guid> ruleBuilder ) => ruleBuilde |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The value must be set (not <see langword="null"/>, not the type's default, not blank text) unless <paramref name |
| | | 22 | | /// returns <see langword="true"/> (a <see langword="null"/> <paramref name="nullable"/> allows empty). |
| | | 23 | | /// </summary> |
| | | 24 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 25 | | /// <typeparam name="TProperty">The property type.</typeparam> |
| | | 26 | | /// <param name="ruleBuilder">The rule builder.</param> |
| | | 27 | | /// <param name="nullable">Decides, per instance and value, whether an empty value is allowed.</param> |
| | | 28 | | /// <returns>The rule builder options.</returns> |
| | | 29 | | public static IRuleBuilderOptions<T, TProperty> NotEmptyIf< T, TProperty >( this IRuleBuilder<T, TProperty> ruleBuil |
| | 12 | 30 | | => ruleBuilder.Must( ( entity, value ) => ( nullable?.Invoke( entity, value ) ?? true ) || !IsEmpty( value ) ).W |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The text must not be blank unless <paramref name="nullable"/> returns <see langword="true"/> (a <see langword="n |
| | | 34 | | /// <paramref name="nullable"/> allows empty). |
| | | 35 | | /// </summary> |
| | | 36 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 37 | | /// <param name="ruleBuilder">The rule builder.</param> |
| | | 38 | | /// <param name="nullable">Decides, per instance and value, whether an empty value is allowed.</param> |
| | | 39 | | /// <returns>The rule builder options.</returns> |
| | | 40 | | public static IRuleBuilderOptions<T, string> NotEmptyStringIf< T >( this IRuleBuilder<T, string> ruleBuilder, Func<T |
| | 18 | 41 | | => ruleBuilder.Must( ( entity, value ) => ( nullable?.Invoke( entity, value ) ?? true ) || !string.IsNullOrWhite |
| | | 42 | | |
| | | 43 | | /// <summary>The text must be an <c>http</c>/<c>https</c> URL.</summary> |
| | | 44 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 45 | | /// <param name="ruleBuilder">The rule builder.</param> |
| | | 46 | | /// <returns>The rule builder options.</returns> |
| | 9 | 47 | | public static IRuleBuilderOptions<T, string> IsValidWebsite< T >( this IRuleBuilder<T, string> ruleBuilder ) => rule |
| | | 48 | | |
| | | 49 | | /// <summary>The whole text must be one e-mail address (RFC 5322 style, case-insensitive).</summary> |
| | | 50 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 51 | | /// <param name="ruleBuilder">The rule builder.</param> |
| | | 52 | | /// <returns>The rule builder options.</returns> |
| | 21 | 53 | | public static IRuleBuilderOptions<T, string> IsValidEmail< T >( this IRuleBuilder<T, string> ruleBuilder ) => ruleBu |
| | | 54 | | |
| | | 55 | | |
| | | 56 | | private static bool IsEmpty< TProperty >( TProperty value ) |
| | 9 | 57 | | => value is null || value is string text && string.IsNullOrWhiteSpace( text ) || EqualityComparer<TProperty>.Def |
| | | 58 | | } |