1
0
This commit is contained in:
Debug_pro
2026-06-07 18:19:53 +03:00
commit fb13e5a20a
33 changed files with 2382 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
using System.Text.RegularExpressions;
namespace SimpleWiki.Services;
public static class InputValidator
{
private static readonly Regex EmailRegex = new(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$", RegexOptions.Compiled);
private static readonly Regex FullNameRegex = new(@"^[A-Za-zА-Яа-яЁё\-\s]{2,100}$", RegexOptions.Compiled);
private static readonly Regex PasswordRegex = new(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z\d]).{8,64}$", RegexOptions.Compiled);
private static readonly Regex CodeRegex = new(@"^\d{6}$", RegexOptions.Compiled);
public static string? ValidateFullName(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return "ФИО обязательно.";
if (!FullNameRegex.IsMatch(value.Trim())) return "ФИО: только буквы, пробелы и дефис, от 2 до 100 символов.";
return null;
}
public static string? ValidateEmail(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return "Email обязателен.";
if (value.Length > 256) return "Email слишком длинный.";
if (!EmailRegex.IsMatch(value.Trim())) return "Некорректный email.";
return null;
}
public static string? ValidatePassword(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return "Пароль обязателен.";
if (!PasswordRegex.IsMatch(value)) return "Пароль: 8-64 символа, минимум 1 строчная, 1 заглавная, 1 цифра и 1 спецсимвол.";
return null;
}
public static string? ValidateResetCode(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return "Код подтверждения обязателен.";
if (!CodeRegex.IsMatch(value.Trim())) return "Код должен состоять из 6 цифр.";
return null;
}
public static string? ValidateArticleTitle(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return "Заголовок обязателен.";
if (value.Trim().Length < 3 || value.Trim().Length > 150) return "Заголовок: от 3 до 150 символов.";
return null;
}
public static string? ValidateArticleSummary(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return "Краткое описание обязательно.";
if (value.Trim().Length < 10 || value.Trim().Length > 400) return "Краткое описание: от 10 до 400 символов.";
return null;
}
public static string? ValidateArticleMarkdown(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return "Текст статьи обязателен.";
if (value.Trim().Length < 20 || value.Trim().Length > 20000) return "Текст статьи: от 20 до 20000 символов.";
return null;
}
public static string JoinErrors(params string?[] errors)
{
return string.Join(Environment.NewLine, errors.Where(x => !string.IsNullOrWhiteSpace(x)));
}
}