< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.Utilities.FluentValidation.MobileNumberValidator<T>
Assembly: ZhEaIsNsAaBn.Utilities.FluentValidation
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.FluentValidation/MobileNumber/MobileNumberValidator.cs
Line coverage
96%
Covered lines: 24
Uncovered lines: 1
Coverable lines: 25
Total lines: 191
Line coverage: 96%
Branch coverage
66%
Covered branches: 8
Total branches: 12
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
get_Name()100%210%
IsValid(...)75%88100%
IsValidPhone(...)50%44100%
GetDefaultMessageTemplate(...)100%11100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.FluentValidation/MobileNumber/MobileNumberValidator.cs

#LineLine coverage
 1namespace 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>
 5public 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
 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
 15    public NullableMobileNumberValidator( string regionCode, Func<T, bool>? customValidation = null ) : base( regionCode
 16
 17
 18    /// <inheritdoc />
 19    public override string Name => "NullableMobileNumberValidator";
 20
 21
 22    /// <inheritdoc />
 23    public override bool IsValid( ValidationContext<T> context, string value )
 24    {
 25        ArgumentNullException.ThrowIfNull( context );
 26
 27        if ( string.IsNullOrWhiteSpace( value ) ) { return true; }
 28
 29
 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>
 36public 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
 946    public MobileNumberValidator( Func<T, string> regionCodeFunc, Func<T, bool>? customValidation = null )
 47    {
 948        _regionCodeFunc   = regionCodeFunc;
 949        _customValidation = customValidation;
 950    }
 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
 2755    public MobileNumberValidator( string regionCode, Func<T, bool>? customValidation = null )
 56    {
 2757        _regionCode       = regionCode;
 2758        _customValidation = customValidation;
 2759    }
 60
 61
 62    /// <inheritdoc />
 063    public override string Name => "MobileNumberValidator";
 64
 65
 66    /// <inheritdoc />
 67    public override bool IsValid( ValidationContext<T> context, string value )
 68    {
 6003069        ArgumentNullException.ThrowIfNull( context );
 70
 6003071        var regionCode = _regionCodeFunc is not null ? _regionCodeFunc( context.InstanceToValidate ) : _regionCode;   //
 72
 73
 6003074        if ( ( _customValidation is null || _customValidation( context.InstanceToValidate ) ) && !IsValidPhone( value, r
 75        {
 3002176            context.MessageFormatter.AppendArgument( "MobileNumberValue", value );
 77
 3002178            return false;
 79        }
 80
 81
 3000982        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    {
 6003091        if ( string.IsNullOrWhiteSpace( value ) || string.IsNullOrWhiteSpace( regionCode ) ) { return false; }
 92
 93
 6003094        var phoneUtil = PhoneNumberUtil.GetInstance();
 95
 96
 97        PhoneNumber phone;
 98
 12004599        try { phone = phoneUtil.Parse( value, null ); }
 15100        catch ( NumberParseException )
 101        {
 18102            try { phone = phoneUtil.Parse( value, regionCode ); }
 24103            catch ( NumberParseException ) { return false; }
 3104        }
 105
 106
 60018107        return phoneUtil.IsValidNumberForRegion( phone, regionCode );
 12108    }
 109
 110    /// <inheritdoc />
 30021111    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>
 117public 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>
 174public 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}