validation updates
This commit is contained in:
@@ -1,117 +1,47 @@
|
||||
using FluentValidation;
|
||||
using System.Globalization;
|
||||
using System.Net.Mail;
|
||||
using zero.Validation.Validators;
|
||||
|
||||
namespace zero.Validation;
|
||||
|
||||
public static class ValidatorExtensions
|
||||
{
|
||||
private const char DOT = '.';
|
||||
|
||||
private const char KLAMMERAFFE = '@';
|
||||
|
||||
private static string HEX_REGEX = "(^$)|(#[0-9a-fA-F]{3,8})";
|
||||
|
||||
/// <summary>
|
||||
/// Validate a color input as HEX (#aabbccdd or #aabbcc or #abc)
|
||||
/// </summary>
|
||||
public static IRuleBuilderOptions<T, string> Hex<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Matches(HEX_REGEX).WithMessage("@errors.forms.hex_format");
|
||||
return ruleBuilder.SetValidator(new HexValidator<T>());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validate an email
|
||||
/// </summary>
|
||||
public static IRuleBuilderOptions<T, string> Url<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must((root, value, context) =>
|
||||
{
|
||||
return value.IsNullOrWhiteSpace() || Uri.IsWellFormedUriString(value, UriKind.Absolute);
|
||||
}).WithMessage("@errors.forms.url_format");
|
||||
return ruleBuilder.SetValidator(new UrlValidator<T>());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validate an email
|
||||
/// </summary>
|
||||
public static IRuleBuilderOptions<T, string> Email<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must((root, value, context) => ValidateEmail(value)).WithMessage("@errors.forms.email_invalid");
|
||||
return ruleBuilder.SetValidator(new EmailValidator<T>());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validate one or multiple emails
|
||||
/// </summary>
|
||||
public static IRuleBuilderOptions<T, string> Emails<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must((root, value, context) =>
|
||||
{
|
||||
if (value.IsNullOrWhiteSpace())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
string[] mails = value.Split(',', ';').Select(x => x.Trim()).Where(x => !x.IsNullOrWhiteSpace()).ToArray();
|
||||
|
||||
if (!mails.Any())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (string mail in mails)
|
||||
{
|
||||
if (!ValidateEmail(mail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}).WithMessage("@errors.forms.emails_invalid");
|
||||
return ruleBuilder.SetValidator(new EmailsValidator<T>());
|
||||
}
|
||||
|
||||
|
||||
private static bool ValidateEmail(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int index = value.IndexOf(KLAMMERAFFE);
|
||||
|
||||
return
|
||||
index > 0 &&
|
||||
index != value.Length - 1 &&
|
||||
index == value.LastIndexOf(KLAMMERAFFE) &&
|
||||
MailAddress.TryCreate(value, out _);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validates a culture identifier
|
||||
/// </summary>
|
||||
public static IRuleBuilderOptions<T, string> Culture<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must((root, value, context) =>
|
||||
{
|
||||
if (value.IsNullOrWhiteSpace())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
CultureInfo info = CultureInfo.GetCultureInfo(value);
|
||||
return info != null && !info.EnglishName.Equals(value, StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}).WithMessage("@errors.forms.culture");
|
||||
return ruleBuilder.SetValidator(new CultureValidator<T>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using FluentValidation.Validators;
|
||||
using System.Globalization;
|
||||
|
||||
namespace zero.Validation.Validators;
|
||||
|
||||
public class CultureValidator<T> : PropertyValidator<T, string>
|
||||
{
|
||||
public override string Name => "CultureValidator";
|
||||
|
||||
protected override string GetDefaultMessageTemplate(string errorCode) => Localized(errorCode, Name);
|
||||
|
||||
public override bool IsValid(FluentValidation.ValidationContext<T> context, string value)
|
||||
{
|
||||
if (value.IsNullOrWhiteSpace())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
CultureInfo info = CultureInfo.GetCultureInfo(value);
|
||||
return info != null && !info.EnglishName.Equals(value, StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using FluentValidation.Validators;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace zero.Validation.Validators;
|
||||
|
||||
public class EmailValidator<T> : PropertyValidator<T, string>, IEmailValidator
|
||||
{
|
||||
public override string Name => "EmailValidator";
|
||||
|
||||
private const char KLAMMERAFFE = '@';
|
||||
|
||||
protected override string GetDefaultMessageTemplate(string errorCode) => Localized(errorCode, Name);
|
||||
|
||||
public override bool IsValid(FluentValidation.ValidationContext<T> context, string value)
|
||||
{
|
||||
return ValidateEmail(value);
|
||||
}
|
||||
|
||||
public static bool ValidateEmail(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int index = value.IndexOf(KLAMMERAFFE);
|
||||
|
||||
return
|
||||
index > 0 &&
|
||||
index != value.Length - 1 &&
|
||||
index == value.LastIndexOf(KLAMMERAFFE) &&
|
||||
MailAddress.TryCreate(value, out _);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using FluentValidation.Validators;
|
||||
|
||||
namespace zero.Validation.Validators;
|
||||
|
||||
public class EmailsValidator<T> : PropertyValidator<T, string>, IEmailValidator
|
||||
{
|
||||
public override string Name => "EmailValidator";
|
||||
|
||||
protected override string GetDefaultMessageTemplate(string errorCode) => Localized(errorCode, Name);
|
||||
|
||||
public override bool IsValid(FluentValidation.ValidationContext<T> context, string value)
|
||||
{
|
||||
if (value.IsNullOrWhiteSpace())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
string[] mails = value.Split(',', ';').Select(x => x.Trim()).Where(x => !x.IsNullOrWhiteSpace()).ToArray();
|
||||
|
||||
if (!mails.Any())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (string mail in mails)
|
||||
{
|
||||
if (!EmailValidator<T>.ValidateEmail(mail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace zero.Validation.Validators;
|
||||
|
||||
public class HexValidator<T> : RegexValidator<T>
|
||||
{
|
||||
public override string Name => "HexValidator";
|
||||
|
||||
static Regex Regex = CreateRegex("(^$)|(#[0-9a-fA-F]{3,8})");
|
||||
|
||||
public HexValidator() : base(Regex) { }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using FluentValidation.Validators;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace zero.Validation.Validators;
|
||||
|
||||
public abstract class RegexValidator<T> : PropertyValidator<T, string>
|
||||
{
|
||||
private readonly Regex _regex = null;
|
||||
|
||||
public RegexValidator(Regex regex)
|
||||
{
|
||||
_regex = regex;
|
||||
}
|
||||
|
||||
protected static Regex CreateRegex(string expression)
|
||||
{
|
||||
const RegexOptions options = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;
|
||||
return new Regex(expression, options, TimeSpan.FromSeconds(2.0));
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate(string errorCode) => Localized(errorCode, Name);
|
||||
|
||||
public override bool IsValid(FluentValidation.ValidationContext<T> context, string value)
|
||||
{
|
||||
if (value == null) return true;
|
||||
|
||||
if (!_regex.IsMatch(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using FluentValidation.Validators;
|
||||
|
||||
namespace zero.Validation.Validators;
|
||||
|
||||
public class UrlValidator<T> : PropertyValidator<T, string>
|
||||
{
|
||||
public override string Name => "UrlValidator";
|
||||
|
||||
protected override string GetDefaultMessageTemplate(string errorCode) => Localized(errorCode, Name);
|
||||
|
||||
public override bool IsValid(FluentValidation.ValidationContext<T> context, string value)
|
||||
{
|
||||
return value.IsNullOrWhiteSpace() || Uri.IsWellFormedUriString(value, UriKind.Absolute);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user