Files
Govor/Govor.Application/Infrastructure/Validators/UsernameValidator.cs
T
Artemy 437bedb117 Refactor friend request services and add SignalR error handling
Split IFriendRequestService into command and query interfaces, refactor related services and tests, and update dependency injection. Add HubResult response model and a SignalR HubExceptionFilter for consistent error handling. Move and update FriendsHub to use new command service and result pattern. Update username validation to allow digits after Cyrillic letters. Add new controllers and configuration for SignalR. Remove obsolete IFriendRequestService and related code.
2025-07-10 15:31:57 +07:00

35 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using Govor.Application.Exceptions.AuthService;
using Govor.Application.Interfaces.Authentication;
using Govor.Core.Infrastructure.Validators;
namespace Govor.Application.Infrastructure.Validators;
public class UsernameValidator : IUsernameValidator
{
private readonly Regex _usernameRegex = new(@"^[А-Яа-яЁё]+[А-Яа-яЁё0-9]*$", RegexOptions.Compiled);
public void Validate(string username)
{
if(username.Length < UserValidator.MIN_LENGHT_OF_NAME || username.Length > UserValidator.MAX_LENGHT_OF_NAME)
throw new InvalidUsernameException($"Username must be between {UserValidator.MIN_LENGHT_OF_NAME} and {UserValidator.MAX_LENGHT_OF_NAME} characters.");
if (!_usernameRegex.IsMatch(username))
throw new InvalidUsernameException("The username must be in Cyrillic and start with a letter.");
}
public bool TryValidate(string username)
{
try
{
Validate(username);
return true;
}
catch
{
return false;
}
}
}