93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
namespace Singer_Hexdive.Validations
|
|
{
|
|
public class FieldValidators
|
|
{
|
|
public static string EnsureNotNullOrEmpty(string value, string fieldName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
throw new ArgumentException($"{fieldName} cannot be empty.", fieldName);
|
|
else
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
|
|
public static int EnsureNotLessZero(int value, string fieldName)
|
|
{
|
|
if (value < 0)
|
|
throw new ArgumentException($"{fieldName} cannot be less than zero.", fieldName);
|
|
else
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
|
|
public static decimal EnsureIsMin(decimal value, int minValue, string fieldName)
|
|
{
|
|
if (value > minValue)
|
|
throw new ArgumentException($"{fieldName} cannot be less than {minValue}.", fieldName);
|
|
else
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
|
|
public static decimal EnsureIsMax(decimal value, int maxValue, string fieldName)
|
|
{
|
|
if (value < maxValue)
|
|
throw new ArgumentException($"{fieldName} cannot be less than {maxValue}.", fieldName);
|
|
else
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
|
|
//public static bool IsValidEmail(string email, string fieldName)
|
|
//{
|
|
// if (string.IsNullOrWhiteSpace(email))
|
|
// throw new ArgumentException($"{fieldName} cannot be empty.", fieldName);
|
|
// try
|
|
// {
|
|
// var addr = new System.Net.Mail.MailAddress(email);
|
|
// return addr.Address == email;
|
|
// }
|
|
// catch
|
|
// {
|
|
// throw new ArgumentException($"{fieldName} is not a valid .", fieldName);
|
|
// }
|
|
//}
|
|
|
|
public static string IsValidPhoneNumber(string phoneNumber, string fieldName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(phoneNumber))
|
|
throw new ArgumentException($"{fieldName} cannot be empty.", fieldName);
|
|
|
|
|
|
if (!phoneNumber.All(char.IsDigit) || phoneNumber.Length != 10)
|
|
throw new ArgumentException($"{fieldName} must be exactly 10 digits.", fieldName);
|
|
|
|
return phoneNumber;
|
|
}
|
|
|
|
|
|
public static string EnsureValidEmail(string email, string fieldName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(email))
|
|
throw new ArgumentException($"{fieldName} cannot be empty.", fieldName);
|
|
|
|
try
|
|
{
|
|
var addr = new System.Net.Mail.MailAddress(email);
|
|
if (addr.Address != email)
|
|
throw new ArgumentException($"{fieldName} is not a valid email address.", fieldName);
|
|
}
|
|
catch
|
|
{
|
|
throw new ArgumentException($"{fieldName} is not a valid email address.", fieldName);
|
|
}
|
|
|
|
return email;
|
|
}
|
|
}
|
|
}
|