From e9e961acfc048da06811893bf79f495d0d5b720b Mon Sep 17 00:00:00 2001 From: HaikAsatryan Date: Fri, 20 Dec 2024 15:50:42 +0400 Subject: [PATCH] validator changes and nuget updates --- Readme.md | 3 +++ src/SharedKernel/SharedKernel.csproj | 8 +++--- .../Validators/ValidatorExtensions.cs | 25 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index ffed50f..1a3c8ee 100644 --- a/Readme.md +++ b/Readme.md @@ -404,6 +404,9 @@ The package includes extension methods to simplify common validation scenarios: - String Validations: - IsValidJson(): Validates that a string is a valid JSON. - IsXssSanitized(): Validates that a string is sanitized against XSS attacks. + - IsEmail(): Validates that a string is a valid email address. Native one is not working correctly. + - IsPhoneNumber(): Validates that a string is a valid phone number. Format requires area code to be in `()`. + - IsEmailOrPhoneNumber(): Validates that a string is either a valid email address or a valid phone number. ## Cors diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index 0a3d9e3..bd30378 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -8,7 +8,7 @@ Readme.md Pandatech MIT - 1.0.22 + 1.0.23 Pandatech.SharedKernel Pandatech Shared Kernel Library Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar @@ -27,8 +27,8 @@ - - + + @@ -52,7 +52,7 @@ - + diff --git a/src/SharedKernel/ValidatorAndMediatR/Validators/ValidatorExtensions.cs b/src/SharedKernel/ValidatorAndMediatR/Validators/ValidatorExtensions.cs index 57a1a7a..8d78b97 100644 --- a/src/SharedKernel/ValidatorAndMediatR/Validators/ValidatorExtensions.cs +++ b/src/SharedKernel/ValidatorAndMediatR/Validators/ValidatorExtensions.cs @@ -1,10 +1,35 @@ using FluentValidation; using Microsoft.AspNetCore.Http; +using RegexBox; namespace SharedKernel.ValidatorAndMediatR.Validators; public static class ValidatorExtensions { + public static IRuleBuilderOptions IsEmail(this IRuleBuilder ruleBuilder) + { + return ruleBuilder.Must(x => x is null || PandaValidator.IsEmail(x)) + .WithMessage("email_format_is_not_valid"); + } + + public static IRuleBuilderOptions IsPhoneNumber(this IRuleBuilder ruleBuilder) + { + return ruleBuilder.Must(x => x is null || PandaValidator.IsPandaFormattedPhoneNumber(x)) + .WithMessage("phone_number_format_is_not_valid"); + } + + public static IRuleBuilderOptions IsEmailOrPhoneNumber(this IRuleBuilder ruleBuilder) + { + return ruleBuilder + .Must(x => x is null || PandaValidator.IsPandaFormattedPhoneNumber(x) || PandaValidator.IsEmail(x)) + .WithMessage("phone_number_or_email_format_is_not_valid"); + } + + public static IRuleBuilderOptions IsPhoneNumberOrEmail(this IRuleBuilder ruleBuilder) + { + return ruleBuilder.IsEmailOrPhoneNumber(); + } + public static IRuleBuilderOptions HasMaxFileSize(this IRuleBuilder ruleBuilder, int maxFileSizeInMb) {