-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from TimeWarpEngineering/Cramer/2024-05-20/Ph…
…oneNumberValidator Cramer/2024 05 20/phone number validator
- Loading branch information
Showing
5 changed files
with
32 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
TimeWarp.Architecture/Source/Common/Common.Contracts/Base/IAuthApiRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
TimeWarp.Architecture/Source/Common/Common.Contracts/GlobalUsings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
TimeWarp.Architecture/Source/Common/Common.Contracts/Validators/PhoneNumberValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
} |