Skip to content

Commit

Permalink
Merge pull request #201 from TimeWarpEngineering/Cramer/2024-05-20/Ph…
Browse files Browse the repository at this point in the history
…oneNumberValidator

Cramer/2024 05 20/phone number validator
  • Loading branch information
StevenTCramer authored May 21, 2024
2 parents 7a86ff8 + 0c514db commit a9d6816
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion TimeWarp.Architecture/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageVersion Include="Grpc.Net.Client" Version="2.62.0" />
<PackageVersion Include="Grpc.Net.Client.Web" Version="2.62.0" />
<PackageVersion Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageVersion Include="libphonenumber-csharp" Version="8.13.37" />
<PackageVersion Include="MediatR" Version="12.2.0" />
<PackageVersion Include="MediatR.Contracts" Version="2.0.1" />
<PackageVersion Include="MicroElements.Swashbuckle.FluentValidation" Version="6.0.0" />
Expand Down Expand Up @@ -97,4 +98,4 @@
<PackageVersion Include="timewarp-heroicons" Version="2.0.19" />
<PackageVersion Include="timewarp-simple-icons" Version="11.11.0" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace TimeWarp.Architecture.Features;

using FluentValidation;

public interface IAuthApiRequest : IApiRequest
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PackageReference Include="Ardalis.GuardClauses" />
<PackageReference Include="FluentValidation" />
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="libphonenumber-csharp" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="OneOf" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
global using Ardalis.GuardClauses;
global using FluentValidation;
global using FluentValidation.Validators;
global using JetBrains.Annotations;
global using Microsoft.Extensions.Options;
global using OneOf;
global using PhoneNumbers;
global using System.Collections.Specialized;
global using System.Net;
global using System.Net.Http;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace TimeWarp.Architecture.Validators;

public class PhoneNumberValidator<T> : PropertyValidator<T, string?>
{
private readonly PhoneNumberUtil PhoneNumberUtil = PhoneNumberUtil.GetInstance();

public override string Name => "PhoneNumberValidator";

public override bool IsValid(ValidationContext<T> context, string? value)
{
if (string.IsNullOrWhiteSpace(value)) return false;

try
{
PhoneNumber? phoneNumber = PhoneNumberUtil.Parse(value, null);
return PhoneNumberUtil.IsValidNumber(phoneNumber);
}
catch (NumberParseException)
{
return false;
}
}

protected override string GetDefaultMessageTemplate(string errorCode) =>
"{PropertyName} is not a valid phone number.";
}

0 comments on commit a9d6816

Please sign in to comment.