Files
Govor/Govor.Application/Infrastructure/Validators/UsernameValidator.cs
T
Artemy 3c785d5c89 Switch to SmartRes Result/Error, upgrade to .NET 10
Migrate codebase to use SmartRes Result<T, Error> (and Unit) across services and interfaces, replacing previous Result usage and updating many method signatures and implementations. Add ResultExtensions to convert SmartRes results to ASP.NET ActionResult and refactor AuthController to use functional Bind/Tap chains for registration/login flows. Upgrade projects to .NET 10 and bump related NuGet packages; add libs/SmartRes.dll and project references. Simplify DbContext registration to use Npgsql only with retry policies and enable detailed logging. Update Swagger/launch settings and other minor fixes (AutoMapper registration change, whitespace/exception handling, and removal of the Govor.ConsoleClient files).
2026-07-25 20:20:45 +07:00

118 lines
3.8 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.Text.RegularExpressions;
using Govor.Application.Authentication.Exceptions;
using Govor.Domain.Common.Constants;
using Govor.Domain.Common;
using Microsoft.Extensions.Configuration;
using SmartRes;
namespace Govor.Application.Infrastructure.Validators;
public class UsernameValidator : IUsernameValidator
{
private const string ErrorCode = nameof(InvalidUsernameException);
private readonly Regex _usernameRegex = new(@"^[А-Яа-яЁё]+[А-Яа-яЁё0-9]*$", RegexOptions.Compiled);
private readonly HashSet<string> _blockedExact;
private readonly List<string> _blockedContains;
private readonly HashSet<string> _reserved;
public UsernameValidator(IConfiguration config)
{
_blockedExact = config.GetSection("UsernameModeration:BlockedExact")
.Get<string[]>()?
.Select(Normalize)
.ToHashSet()
?? throw new InvalidOperationException("BlockedExact not set");
_blockedContains = config
.GetSection("UsernameModeration:BlockedContains")
.Get<string[]>()?
.Select(Normalize)
.ToList()
?? throw new InvalidOperationException("BlockedContains not set");
_reserved = config
.GetSection("UsernameModeration:Reserved")
.Get<string[]>()?
.Select(Normalize)
.ToHashSet()
?? throw new InvalidOperationException("Reserved not set");
}
public Result<Unit, Error> Validate(string username)
{
if (username.Length < UserConstants.MIN_LENGHT_OF_NAME || username.Length > UserConstants.MAX_LENGHT_OF_NAME)
{
return Result.Failure(Error.Validation(
ErrorCode,
$"Username must be between {UserConstants.MIN_LENGHT_OF_NAME} and {UserConstants.MAX_LENGHT_OF_NAME} characters.")
);
}
if (!_usernameRegex.IsMatch(username))
{
return Result.Failure(Error.Validation(
ErrorCode,
"The username must be in Cyrillic and start with a letter.")
);
}
if (Regex.IsMatch(username, @"(.)\1{4,}"))
{
return Result.Failure(Error.Validation(
ErrorCode,
"Too many repeating characters.")
);
}
var normalized = Normalize(username);
if (_reserved.Contains(normalized))
{
return Result.Failure(Error.Validation(
ErrorCode,
"This username is reserved.")
);
}
if (_blockedExact.Contains(normalized))
{
return Result.Failure(Error.Validation(
ErrorCode,
"This username is not allowed.")
);
}
foreach (var banned in _blockedContains)
{
if (normalized.Contains(banned))
{
return Result.Failure(Error.Validation(
ErrorCode,
"Username contains prohibited content.")
);
}
}
return Result.Success();
}
public bool TryValidate(string username)
{
return Validate(username).IsSuccess;
}
private static string Normalize(string username)
{
return username
.ToLower()
.Replace("0", "о")
.Replace("1", "и")
.Replace("3", "е")
.Replace("4", "а")
.Replace("6", "б")
.Replace("8", "в");
}
}