forked from fanliang11/surging
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: 增加ValidateAttribute校验特性及相关校验处理器
- Loading branch information
褚知霖
committed
Jan 8, 2020
1 parent
0d4b85a
commit e301756
Showing
10 changed files
with
218 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Surging.Core/Surging.Core.CPlatform/Exceptions/ValidateException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace Surging.Core.CPlatform.Exceptions | ||
{ | ||
/// <summary> | ||
/// Model、DTO等对象校验异常 | ||
/// </summary> | ||
public class ValidateException : CPlatformException | ||
{ | ||
/// <summary> | ||
/// 初始构造函数 | ||
/// </summary> | ||
/// <param name="message">异常信息</param> | ||
/// <param name="innerException">内部异常</param> | ||
public ValidateException(string message, Exception innerException = null) : base(message, innerException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Surging.Core/Surging.Core.CPlatform/Validation/IValidationProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Reflection; | ||
|
||
namespace Surging.Core.CPlatform.Validation | ||
{ | ||
public interface IValidationProcessor | ||
{ | ||
void Validate(ParameterInfo parameterInfo, object value, ValidateAttribute methodValidateAttribute = null); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...rging.Core/Surging.Core.CPlatform/Validation/Implementation/DefaultValidationProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Surging.Core.CPlatform.Convertibles; | ||
using Surging.Core.CPlatform.Exceptions; | ||
using Surging.Core.CPlatform.Utilities; | ||
|
||
namespace Surging.Core.CPlatform.Validation.Implementation | ||
{ | ||
public class DefaultValidationProcessor : IValidationProcessor | ||
{ | ||
private readonly ITypeConvertibleService _typeConvertibleService; | ||
|
||
public DefaultValidationProcessor(ITypeConvertibleService typeConvertibleService) | ||
{ | ||
_typeConvertibleService = typeConvertibleService; | ||
} | ||
|
||
public void Validate(ParameterInfo parameterInfo, object value, ValidateAttribute methodValidateAttribute = null) | ||
{ | ||
Check.NotNull(parameterInfo, nameof(parameterInfo)); | ||
|
||
var parameterType = parameterInfo.ParameterType; | ||
if (value != null) | ||
{ | ||
var parameter = _typeConvertibleService.Convert(value, parameterType); | ||
var customAttributes = parameterInfo.GetCustomAttributes(true); | ||
if (customAttributes.Any(at => at is ValidateAttribute) || methodValidateAttribute != null) | ||
{ | ||
var validateAttr = | ||
(ValidateAttribute)customAttributes.FirstOrDefault(at => at is ValidateAttribute) ?? methodValidateAttribute; | ||
|
||
var customValidAttributes = customAttributes | ||
.Where(ca => ca.GetType() != typeof(ValidateAttribute)) | ||
.OfType<ValidationAttribute>() | ||
.ToList(); | ||
|
||
var validationContext = new ValidationContext(parameter); | ||
var validationResults = new List<ValidationResult>(); | ||
var isObjValid = Validator.TryValidateObject(parameter, validationContext, | ||
validationResults, | ||
true); | ||
|
||
var isValueValid = Validator.TryValidateValue(parameter, validationContext, | ||
validationResults, customValidAttributes); | ||
|
||
if (isObjValid && isValueValid) return; | ||
|
||
throw new ValidateException(validationResults.Select(p => p.ErrorMessage).First()); | ||
} | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Surging.Core/Surging.Core.CPlatform/Validation/ValidateAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace Surging.Core.CPlatform.Validation | ||
{ | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter)] | ||
public class ValidateAttribute : Attribute | ||
{ | ||
public ValidateAttribute() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.