From 41f46fac07d17482441edd61b79a58cbad4b294c Mon Sep 17 00:00:00 2001 From: MUEHLHEIM Marc Date: Thu, 19 Oct 2023 19:58:39 +0200 Subject: [PATCH] Add phonenumberlib for enhanced number validation #14 --- .../DefikarteBackend/DefikarteBackend.csproj | 1 + .../DefibrillatorRequestValidator.cs | 41 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/backend/DefikarteBackend/DefikarteBackend/DefikarteBackend.csproj b/backend/DefikarteBackend/DefikarteBackend/DefikarteBackend.csproj index 209443c..51e1a38 100644 --- a/backend/DefikarteBackend/DefikarteBackend/DefikarteBackend.csproj +++ b/backend/DefikarteBackend/DefikarteBackend/DefikarteBackend.csproj @@ -9,6 +9,7 @@ + diff --git a/backend/DefikarteBackend/DefikarteBackend/Validation/DefibrillatorRequestValidator.cs b/backend/DefikarteBackend/DefikarteBackend/Validation/DefibrillatorRequestValidator.cs index 013881f..515221b 100644 --- a/backend/DefikarteBackend/DefikarteBackend/Validation/DefibrillatorRequestValidator.cs +++ b/backend/DefikarteBackend/DefikarteBackend/Validation/DefibrillatorRequestValidator.cs @@ -1,12 +1,11 @@ using DefikarteBackend.Model; using FluentValidation; +using System.Text.RegularExpressions; namespace DefikarteBackend.Validation { public class DefibrillatorRequestValidator : AbstractValidator { - 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(); @@ -14,11 +13,45 @@ public DefibrillatorRequestValidator() 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 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"); + } + } } } \ No newline at end of file