diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Check.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Check.cs index 2a814a585a..6d314d0685 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Check.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Check.cs @@ -1,7 +1,7 @@ -using System; +using JetBrains.Annotations; +using System; using System.Collections.Generic; using System.Diagnostics; -using JetBrains.Annotations; namespace Volo.Abp { @@ -9,7 +9,9 @@ namespace Volo.Abp public static class Check { [ContractAnnotation("value:null => halt")] - public static T NotNull(T value, [InvokerParameterName] [NotNull] string parameterName) + public static T NotNull( + T value, + [InvokerParameterName] [NotNull] string parameterName) { if (value == null) { @@ -20,7 +22,10 @@ namespace Volo.Abp } [ContractAnnotation("value:null => halt")] - public static T NotNull(T value, [InvokerParameterName] [NotNull] string parameterName, string message) + public static T NotNull( + T value, + [InvokerParameterName] [NotNull] string parameterName, + string message) { if (value == null) { @@ -31,24 +36,77 @@ namespace Volo.Abp } [ContractAnnotation("value:null => halt")] - public static string NotNullOrWhiteSpace(string value, [InvokerParameterName] [NotNull] string parameterName) + public static string NotNull( + string value, + [InvokerParameterName] [NotNull] string parameterName, + int maxLength = int.MaxValue, + int minLength = 0) + { + if (value == null) + { + throw new ArgumentException($"{parameterName} can not be null!", 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; + } + + [ContractAnnotation("value:null => halt")] + public static string NotNullOrWhiteSpace( + string value, + [InvokerParameterName] [NotNull] 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; } [ContractAnnotation("value:null => halt")] - public static string NotNullOrEmpty(string value, [InvokerParameterName] [NotNull] string parameterName) + public static string NotNullOrEmpty( + string value, + [InvokerParameterName] [NotNull] 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; } @@ -62,5 +120,32 @@ namespace Volo.Abp return value; } + + public static string Length( + string value, + [InvokerParameterName] [NotNull] string parameterName, + int maxLength, + int minLength = 0) + { + if (minLength > 0) + { + if (string.IsNullOrEmpty(value)) + { + throw new ArgumentException(parameterName + " can not be null or empty!", parameterName); + } + + if (value.Length < minLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); + } + } + + if (value != null && value.Length > maxLength) + { + throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); + } + + return value; + } } }