Browse Source

Merge pull request #17358 from bindtech/Fix-Nullable-validation-in-Check-Class

Accept nullable values in validation methods
pull/17377/head
liangshiwei 3 years ago
committed by GitHub
parent
commit
0a6cef2301
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      framework/src/Volo.Abp.Core/Volo/Abp/Check.cs

20
framework/src/Volo.Abp.Core/Volo/Abp/Check.cs

@ -10,7 +10,7 @@ public static class Check
{ {
[ContractAnnotation("value:null => halt")] [ContractAnnotation("value:null => halt")]
public static T NotNull<T>( public static T NotNull<T>(
T value, T? value,
[InvokerParameterName][NotNull] string parameterName) [InvokerParameterName][NotNull] string parameterName)
{ {
if (value == null) if (value == null)
@ -23,7 +23,7 @@ public static class Check
[ContractAnnotation("value:null => halt")] [ContractAnnotation("value:null => halt")]
public static T NotNull<T>( public static T NotNull<T>(
T value, T? value,
[InvokerParameterName][NotNull] string parameterName, [InvokerParameterName][NotNull] string parameterName,
string message) string message)
{ {
@ -37,7 +37,7 @@ public static class Check
[ContractAnnotation("value:null => halt")] [ContractAnnotation("value:null => halt")]
public static string NotNull( public static string NotNull(
string value, string? value,
[InvokerParameterName][NotNull] string parameterName, [InvokerParameterName][NotNull] string parameterName,
int maxLength = int.MaxValue, int maxLength = int.MaxValue,
int minLength = 0) int minLength = 0)
@ -77,7 +77,7 @@ public static class Check
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
} }
if (minLength > 0 && value.Length < minLength) if (minLength > 0 && value!.Length < minLength)
{ {
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
} }
@ -102,7 +102,7 @@ public static class Check
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
} }
if (minLength > 0 && value.Length < minLength) if (minLength > 0 && value!.Length < minLength)
{ {
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
} }
@ -111,9 +111,9 @@ public static class Check
} }
[ContractAnnotation("value:null => halt")] [ContractAnnotation("value:null => halt")]
public static ICollection<T> NotNullOrEmpty<T>(ICollection<T> value, [InvokerParameterName][NotNull] string parameterName) public static ICollection<T> NotNullOrEmpty<T>(ICollection<T>? value, [InvokerParameterName][NotNull] string parameterName)
{ {
if (value.IsNullOrEmpty()) if (value == null || value.Count <= 0)
{ {
throw new ArgumentException(parameterName + " can not be null or empty!", parameterName); throw new ArgumentException(parameterName + " can not be null or empty!", parameterName);
} }
@ -167,11 +167,11 @@ public static class Check
Int16 value, Int16 value,
[InvokerParameterName][NotNull] string parameterName) [InvokerParameterName][NotNull] string parameterName)
{ {
if(value == 0) if (value == 0)
{ {
throw new ArgumentException($"{parameterName} is equal to zero"); throw new ArgumentException($"{parameterName} is equal to zero");
} }
else if(value < 0) else if (value < 0)
{ {
throw new ArgumentException($"{parameterName} is less than zero"); throw new ArgumentException($"{parameterName} is less than zero");
} }
@ -259,7 +259,7 @@ public static class Check
Int16 minimumValue, Int16 minimumValue,
Int16 maximumValue = Int16.MaxValue) Int16 maximumValue = Int16.MaxValue)
{ {
if(value < minimumValue || value > maximumValue) if (value < minimumValue || value > maximumValue)
{ {
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
} }

Loading…
Cancel
Save