-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add phonenumberlib for enhanced number validation #14
- Loading branch information
1 parent
cf2ea5b
commit 41f46fa
Showing
2 changed files
with
38 additions
and
4 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
41 changes: 37 additions & 4 deletions
41
backend/DefikarteBackend/DefikarteBackend/Validation/DefibrillatorRequestValidator.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 |
---|---|---|
@@ -1,24 +1,57 @@ | ||
using DefikarteBackend.Model; | ||
using FluentValidation; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace DefikarteBackend.Validation | ||
{ | ||
public class DefibrillatorRequestValidator : AbstractValidator<DefibrillatorRequest> | ||
{ | ||
private readonly string _phoneNumberPattern = @"^(\+41|0041|0)\s?(\d{2})\s?(\d{3})\s?(\d{2})\s?(\d{2})$"; | ||
|
||
public DefibrillatorRequestValidator() | ||
{ | ||
RuleFor(x => x.Latitude).NotEmpty(); | ||
RuleFor(x => x.Longitude).NotEmpty(); | ||
RuleFor(x => x.Reporter).NotEmpty(); | ||
RuleFor(x => x.Location).NotEmpty().MaximumLength(200); | ||
RuleFor(x => x.Description).MaximumLength(200); | ||
RuleFor(x => x.OperatorPhone).Matches(_phoneNumberPattern).When(x => !string.IsNullOrEmpty(x.OperatorPhone)); | ||
RuleFor(x => x.OperatorPhone).Custom((value, context) => PhoneNumberValid(value, context)).When(x => !string.IsNullOrEmpty(x.OperatorPhone)); | ||
RuleFor(x => x.Access).NotNull(); | ||
RuleFor(x => x.Indoor).NotNull(); | ||
RuleFor(x => x.EmergencyPhone).NotEmpty().Matches($"({_phoneNumberPattern})|112|144|117|118|1414"); | ||
RuleFor(x => x.EmergencyPhone).NotEmpty().Custom((value, context) => PhoneNumberValid(value, context)); | ||
// opening hours validation is missing | ||
} | ||
|
||
private void PhoneNumberValid(string phoneNumberRaw, ValidationContext<DefibrillatorRequest> context) | ||
{ | ||
if (string.IsNullOrEmpty(phoneNumberRaw)) | ||
{ | ||
return; | ||
} | ||
|
||
var result = Regex.Match(phoneNumberRaw, "112|144|117|118|1414"); | ||
if (result.Success) | ||
{ | ||
return; | ||
} | ||
|
||
var phoneNumberUtil = PhoneNumbers.PhoneNumberUtil.GetInstance(); | ||
try | ||
{ | ||
var value = phoneNumberUtil.Parse(phoneNumberRaw, "CH"); | ||
var valid = | ||
phoneNumberUtil.IsPossibleNumber(value) && | ||
phoneNumberUtil.IsValidNumber(value) && | ||
(phoneNumberUtil.IsValidNumberForRegion(value, "CH") | ||
|| phoneNumberUtil.IsValidNumberForRegion(value, "LI")); | ||
|
||
if (!valid) | ||
{ | ||
context.AddFailure(context.PropertyName, "Phonenumber not valid"); | ||
} | ||
} | ||
catch (System.Exception) | ||
{ | ||
context.AddFailure(context.PropertyName, "Phonenumber not valid"); | ||
} | ||
} | ||
} | ||
} |