169 lines
5.3 KiB
C#
169 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
namespace Tnb.VmodelEngine;
|
|
|
|
public static class ThrowIf
|
|
{
|
|
public static void When(bool isMatch, string message)
|
|
{
|
|
if (isMatch)
|
|
{
|
|
throw new ArgumentException(message);
|
|
}
|
|
}
|
|
|
|
public static T IsNull<T>([NotNull] T? value, string parameterName, string? message = null)
|
|
{
|
|
if (value == null)
|
|
{
|
|
throw new ArgumentNullException(parameterName, message);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static T IsNullOrDefault<T>([NotNull] T? value, string parameterName) where T : struct
|
|
{
|
|
if (!value.HasValue)
|
|
{
|
|
throw new ArgumentException(parameterName + " is null!", parameterName);
|
|
}
|
|
|
|
if (value.Value.Equals(default(T)))
|
|
{
|
|
throw new ArgumentException(parameterName + " has a default value!", parameterName);
|
|
}
|
|
|
|
return value.Value;
|
|
}
|
|
|
|
|
|
public static string IsNullOrWhiteSpace(string value, string parameterName, int maxLength = int.MaxValue, int minLength = 0)
|
|
{
|
|
if (value.IsNullOrWhiteSpace())
|
|
{
|
|
throw new ArgumentException(parameterName + " can not be null, empty or white space!", parameterName);
|
|
}
|
|
|
|
if (value.Length > maxLength)
|
|
{
|
|
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
|
|
}
|
|
|
|
if (minLength > 0 && value.Length < minLength)
|
|
{
|
|
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static string IsNullOrEmpty(string value, string parameterName, int maxLength = int.MaxValue, int minLength = 0)
|
|
{
|
|
if (value.IsNullOrEmpty())
|
|
{
|
|
throw new ArgumentException(parameterName + " can not be null or empty!", parameterName);
|
|
}
|
|
|
|
if (value.Length > maxLength)
|
|
{
|
|
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
|
|
}
|
|
|
|
if (minLength > 0 && value.Length < minLength)
|
|
{
|
|
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
//public static ICollection<T> NotNullOrEmpty<T>(ICollection<T> value, string parameterName)
|
|
//{
|
|
// if (value.IsNullOrEmpty())
|
|
// {
|
|
// throw new ArgumentException(parameterName + " can not be null or empty!", parameterName);
|
|
// }
|
|
|
|
// return value;
|
|
//}
|
|
|
|
//public static Type AssignableTo<TBaseType>(Type type, string parameterName)
|
|
//{
|
|
// NotNull(type, parameterName);
|
|
// if (!type.IsAssignableTo<TBaseType>())
|
|
// {
|
|
// throw new ArgumentException(parameterName + " (type of " + type.AssemblyQualifiedName + ") should be assignable to the " + typeof(TBaseType).GetFullNameWithAssemblyName() + "!");
|
|
// }
|
|
|
|
// return type;
|
|
//}
|
|
|
|
public static short OutOfRange(short value, string parameterName, short minimumValue, short maximumValue = short.MaxValue)
|
|
{
|
|
if (value < minimumValue || value > maximumValue)
|
|
{
|
|
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static int OutOfRange(int value, string parameterName, int minimumValue, int maximumValue = int.MaxValue)
|
|
{
|
|
if (value < minimumValue || value > maximumValue)
|
|
{
|
|
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static long OutOfRange(long value, string parameterName, long minimumValue, long maximumValue = long.MaxValue)
|
|
{
|
|
if (value < minimumValue || value > maximumValue)
|
|
{
|
|
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static float OutOfRange(float value, string parameterName, float minimumValue, float maximumValue = float.MaxValue)
|
|
{
|
|
if (value < minimumValue || value > maximumValue)
|
|
{
|
|
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static double OutOfRange(double value, string parameterName, double minimumValue, double maximumValue = double.MaxValue)
|
|
{
|
|
if (value < minimumValue || value > maximumValue)
|
|
{
|
|
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static decimal OutOfRange(decimal value, string parameterName, decimal minimumValue, decimal maximumValue = decimal.MaxValue)
|
|
{
|
|
if (value < minimumValue || value > maximumValue)
|
|
{
|
|
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
}
|