Skip to content

Commit

Permalink
updates to common library
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud Swehli committed Feb 17, 2024
1 parent a2e58a6 commit b8597aa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Muljin.Common/Muljin.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Muljin</RootNamespace>
<AssemblyName>Muljin.Common</AssemblyName>
<PackageId>Muljin.Common</PackageId>
Expand All @@ -12,7 +12,7 @@
<Copyright>Muljin Pte. Ltd.</Copyright>
<PackageProjectUrl>https://www.muljin.com</PackageProjectUrl>
<RepositoryUrl>https://github.com/Muljin/Muljin.Net</RepositoryUrl>
<Version>2.2.11</Version>
<Version>3.0.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

Expand Down
88 changes: 72 additions & 16 deletions Muljin.Common/Utils/Guards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,84 @@ public static void AlphaNumeric(string obj, string paramName = null, string mess
}

/// <summary>
/// Ensure a string only consists of numbers
/// Check value is greater than limit
/// </summary>
/// <param name="value"></param>
/// <param name="limit"></param>
public static void GreaterThan(int value, int limit, string paramName, string message = null)
{
if(value <= limit)
{
throw new ArgumentException(message ?? $"Invalid paramter. Parameter must be greater than {limit}", paramName);
}

}

/// <summary>
/// Validate an enum is defiend is not the default 0 (e.g. None) enum
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="paramName"></param>
/// <param name="message"></param>
/// <exception cref="Exception"></exception>
/// <exception cref="ArgumentException"></exception>
public static void Numeric(string obj, string paramName = null, string message = null)
public static void EnumDefined<T>(T obj, string paramName = null, string message = null) where T : struct, Enum
{
foreach(var o in obj)
if(!typeof(T).IsEnum){
throw new Exception("Invalid type used");
}

if(!Enum.IsDefined<T>(obj))
{
if (!Char.IsDigit(o))
{
throw new ArgumentException(message ?? "Invalid input. Input must consist of numbers only", paramName);
}
throw new ArgumentException("Enum not defined", paramName);
}

if( ((IConvertible)obj).ToInt32(null) == 0){
throw new ArgumentException("Enum cannot be default value", paramName);
}
}

/// <summary>
/// Check value is greater than limit
/// Validate a specified length for a string, ignoring whitespaces and trimming
/// </summary>
/// <param name="value"></param>
/// <param name="limit"></param>
public static void GreaterThan(int value, int limit, string paramName, string message = null)
/// <param name="input"></param>
/// <param name="min"></param>
/// <param name="max"></param>
public static void Length(string input, int min = -1, int max = -1, bool allowWhiteSpace = false)
{
if(value <= limit)
if(allowWhiteSpace)
{
throw new ArgumentException(message ?? $"Invalid paramter. Parameter must be greater than {limit}", paramName);
ArgumentNullException.ThrowIfNull(input, nameof(input));
}else{
ArgumentNullException.ThrowIfNullOrWhiteSpace(input, nameof(input));
input = input.Trim();
}

var len = input.Length;

//check min length
if(min > -1 && len < min)
{
throw new ArgumentException("Invalid input length.");
}

//check max length
if(max >-1 && len > max){
throw new ArgumentException("Invalid input length.");
}
}

/// <summary>
/// Check value is less than given limit
/// </summary>
/// <param name="value"></param>
/// <param name="limit"></param>
public static void LessThan(int value, int limit, string paramName, string message = null)
public static void LessThan(int value, int limit, string paramName = null, string message = null)
{
if(value >= limit)
{
throw new ArgumentException(message ?? $"Invalid parameter. {paramName} must be less than {limit}", paramName);
throw new ArgumentException(message ?? $"Invalid parameter. {paramName ?? string.Empty} must be less than {limit}", paramName);
}
}

Expand Down Expand Up @@ -112,7 +149,7 @@ public static void NotDefault(object obj, string paramName, string message = nul
/// <param name="obj"></param>
/// <param name="paramName"></param>
/// <param name="message"></param>
public static void NotDefault(string obj, string paramName, string message = null)
public static void NotDefault(string obj, string paramName = null, string message = null)
{
if (obj == null)
{
Expand Down Expand Up @@ -166,5 +203,24 @@ public static void NotNullOrDefault(Guid obj, string paramName = null, string me
throw new ArgumentException(message ?? "Guid cannot be empty", paramName);
}
}

/// <summary>
/// Ensure a string only consists of numbers
/// </summary>
/// <param name="obj"></param>
/// <param name="paramName"></param>
/// <param name="message"></param>
/// <exception cref="ArgumentException"></exception>
public static void Numeric(string obj, string paramName = null, string message = null)
{
foreach(var o in obj)
{
if (!Char.IsDigit(o))
{
throw new ArgumentException(message ?? "Invalid input. Input must consist of numbers only", paramName);
}
}
}

}
}

0 comments on commit b8597aa

Please sign in to comment.