67 lines
3.1 KiB
C#
67 lines
3.1 KiB
C#
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)));
|
||
}
|
||
}
|