| | | 1 | | namespace ZhEaIsNsAaBn.Utilities.FluentValidation; |
| | | 2 | | |
| | | 3 | | /// <summary><see cref="MobileNumberValidator{T}"/> that also accepts an empty value.</summary> |
| | | 4 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 5 | | public class NullableMobileNumberValidator< T > : MobileNumberValidator<T> |
| | | 6 | | { |
| | | 7 | | /// <summary>Creates the validator with a per-instance region.</summary> |
| | | 8 | | /// <param name="regionCodeFunc">Returns the ISO 3166 region for the instance.</param> |
| | | 9 | | /// <param name="customValidation">When given, the number is checked only if it returns <see langword="true"/>.</par |
| | 6 | 10 | | public NullableMobileNumberValidator( Func<T, string> regionCodeFunc, Func<T, bool>? customValidation = null ) : bas |
| | | 11 | | |
| | | 12 | | /// <summary>Creates the validator with a fixed region.</summary> |
| | | 13 | | /// <param name="regionCode">The ISO 3166 region, e.g. <c>SA</c>.</param> |
| | | 14 | | /// <param name="customValidation">When given, the number is checked only if it returns <see langword="true"/>.</par |
| | 30 | 15 | | public NullableMobileNumberValidator( string regionCode, Func<T, bool>? customValidation = null ) : base( regionCode |
| | | 16 | | |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | 0 | 19 | | public override string Name => "NullableMobileNumberValidator"; |
| | | 20 | | |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public override bool IsValid( ValidationContext<T> context, string value ) |
| | | 24 | | { |
| | 18 | 25 | | ArgumentNullException.ThrowIfNull( context ); |
| | | 26 | | |
| | 21 | 27 | | if ( string.IsNullOrWhiteSpace( value ) ) { return true; } |
| | | 28 | | |
| | | 29 | | |
| | 15 | 30 | | return base.IsValid( context, value ); |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Checks that a text is a valid mobile number for a region (libphonenumber).</summary> |
| | | 35 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 36 | | public class MobileNumberValidator< T > : PropertyValidator<T, string> |
| | | 37 | | { |
| | | 38 | | private readonly Func<T, bool>? _customValidation; |
| | | 39 | | private readonly string? _regionCode; |
| | | 40 | | private readonly Func<T, string>? _regionCodeFunc; |
| | | 41 | | |
| | | 42 | | |
| | | 43 | | /// <summary>Creates the validator with a per-instance region.</summary> |
| | | 44 | | /// <param name="regionCodeFunc">Returns the ISO 3166 region for the instance.</param> |
| | | 45 | | /// <param name="customValidation">When given, the number is checked only if it returns <see langword="true"/>.</par |
| | | 46 | | public MobileNumberValidator( Func<T, string> regionCodeFunc, Func<T, bool>? customValidation = null ) |
| | | 47 | | { |
| | | 48 | | _regionCodeFunc = regionCodeFunc; |
| | | 49 | | _customValidation = customValidation; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Creates the validator with a fixed region.</summary> |
| | | 53 | | /// <param name="regionCode">The ISO 3166 region, e.g. <c>SA</c>.</param> |
| | | 54 | | /// <param name="customValidation">When given, the number is checked only if it returns <see langword="true"/>.</par |
| | | 55 | | public MobileNumberValidator( string regionCode, Func<T, bool>? customValidation = null ) |
| | | 56 | | { |
| | | 57 | | _regionCode = regionCode; |
| | | 58 | | _customValidation = customValidation; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public override string Name => "MobileNumberValidator"; |
| | | 64 | | |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public override bool IsValid( ValidationContext<T> context, string value ) |
| | | 68 | | { |
| | | 69 | | ArgumentNullException.ThrowIfNull( context ); |
| | | 70 | | |
| | | 71 | | var regionCode = _regionCodeFunc is not null ? _regionCodeFunc( context.InstanceToValidate ) : _regionCode; // |
| | | 72 | | |
| | | 73 | | |
| | | 74 | | if ( ( _customValidation is null || _customValidation( context.InstanceToValidate ) ) && !IsValidPhone( value, r |
| | | 75 | | { |
| | | 76 | | context.MessageFormatter.AppendArgument( "MobileNumberValue", value ); |
| | | 77 | | |
| | | 78 | | return false; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | |
| | | 82 | | return true; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary>Checks <paramref name="value"/> against <paramref name="regionCode"/>, in international or local form.< |
| | | 86 | | /// <param name="value">The number.</param> |
| | | 87 | | /// <param name="regionCode">The ISO 3166 region.</param> |
| | | 88 | | /// <returns><see langword="true"/> when it is a valid number for the region.</returns> |
| | | 89 | | protected static bool IsValidPhone( string? value, string? regionCode ) |
| | | 90 | | { |
| | | 91 | | if ( string.IsNullOrWhiteSpace( value ) || string.IsNullOrWhiteSpace( regionCode ) ) { return false; } |
| | | 92 | | |
| | | 93 | | |
| | | 94 | | var phoneUtil = PhoneNumberUtil.GetInstance(); |
| | | 95 | | |
| | | 96 | | |
| | | 97 | | PhoneNumber phone; |
| | | 98 | | |
| | | 99 | | try { phone = phoneUtil.Parse( value, null ); } |
| | | 100 | | catch ( NumberParseException ) |
| | | 101 | | { |
| | | 102 | | try { phone = phoneUtil.Parse( value, regionCode ); } |
| | | 103 | | catch ( NumberParseException ) { return false; } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | |
| | | 107 | | return phoneUtil.IsValidNumberForRegion( phone, regionCode ); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | | 111 | | protected override string GetDefaultMessageTemplate( string errorCode ) => "{PropertyName} Invalid Mobile number {Mo |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary>Checks that a text is a valid Saudi (<c>SA</c>) mobile number.</summary> |
| | | 115 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 116 | | /// <param name="customValidation">When given, the number is checked only if it returns <see langword="true"/>.</param> |
| | | 117 | | public class KSAMobileNumberValidator< T >( Func<T, bool>? customValidation = null ) : PropertyValidator<T, string> |
| | | 118 | | { |
| | | 119 | | /// <summary>The region checked: <c>SA</c>.</summary> |
| | | 120 | | protected const string Region = "SA"; |
| | | 121 | | |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | | 124 | | public override string Name => "KSAMobileNumberValidator"; |
| | | 125 | | |
| | | 126 | | |
| | | 127 | | /// <inheritdoc /> |
| | | 128 | | public override bool IsValid( ValidationContext<T> context, string value ) |
| | | 129 | | { |
| | | 130 | | ArgumentNullException.ThrowIfNull( context ); |
| | | 131 | | |
| | | 132 | | if ( ( customValidation is null || customValidation( context.InstanceToValidate ) ) && !IsValidPhone( value ) ) |
| | | 133 | | { |
| | | 134 | | context.MessageFormatter.AppendArgument( "MobileNumberValue", value ); |
| | | 135 | | |
| | | 136 | | return false; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | |
| | | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary>Checks <paramref name="value"/> as a Saudi number, in international or local form.</summary> |
| | | 144 | | /// <param name="value">The number.</param> |
| | | 145 | | /// <returns><see langword="true"/> when it is a valid <c>SA</c> number.</returns> |
| | | 146 | | protected static bool IsValidPhone( string? value ) |
| | | 147 | | { |
| | | 148 | | if ( string.IsNullOrWhiteSpace( value ) ) { return false; } |
| | | 149 | | |
| | | 150 | | |
| | | 151 | | var phoneUtil = PhoneNumberUtil.GetInstance(); |
| | | 152 | | |
| | | 153 | | |
| | | 154 | | PhoneNumber phone; |
| | | 155 | | |
| | | 156 | | try { phone = phoneUtil.Parse( value, null ); } |
| | | 157 | | catch ( NumberParseException ) |
| | | 158 | | { |
| | | 159 | | try { phone = phoneUtil.Parse( value, Region ); } |
| | | 160 | | catch ( NumberParseException ) { return false; } |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | |
| | | 164 | | return phoneUtil.IsValidNumberForRegion( phone, Region ); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <inheritdoc /> |
| | | 168 | | protected override string GetDefaultMessageTemplate( string errorCode ) => "{PropertyName} Invalid Mobile number {Mo |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary><see cref="KSAMobileNumberValidator{T}"/> that also accepts an empty value.</summary> |
| | | 172 | | /// <typeparam name="T">The validated type.</typeparam> |
| | | 173 | | /// <param name="customValidation">When given, the number is checked only if it returns <see langword="true"/>.</param> |
| | | 174 | | public class KSANullableMobileNumberValidator< T >( Func<T, bool>? customValidation = null ) : KSAMobileNumberValidator< |
| | | 175 | | { |
| | | 176 | | /// <inheritdoc /> |
| | | 177 | | public override bool IsValid( ValidationContext<T> context, string value ) |
| | | 178 | | { |
| | | 179 | | ArgumentNullException.ThrowIfNull( context ); |
| | | 180 | | |
| | | 181 | | if ( ( customValidation is null || customValidation( context.InstanceToValidate ) ) && !string.IsNullOrWhiteSpac |
| | | 182 | | { |
| | | 183 | | context.MessageFormatter.AppendArgument( "MobileNumberValue", value ); |
| | | 184 | | |
| | | 185 | | return false; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | |
| | | 189 | | return true; |
| | | 190 | | } |
| | | 191 | | } |