Skip to content

Commit

Permalink
Modify validation processing based on ValidateAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
fanliang11 committed Jan 8, 2020
1 parent 76c7616 commit 0fa0718
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
Empty file added FETCH_HEAD
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ private ServiceEntry Create(MethodInfo method, string serviceName, string routeT
}
var fastInvoker = GetHandler(serviceId, method);

var methodValidateAttribute = method.GetCustomAttributes(typeof(ValidateAttribute))
.Cast<ValidateAttribute>()
.FirstOrDefault();
var methodValidateAttribute = attributes.Where(p => p is ValidateAttribute)
.Cast<ValidateAttribute>().FirstOrDefault();

return new ServiceEntry
{
Expand Down Expand Up @@ -135,7 +134,8 @@ private ServiceEntry Create(MethodInfo method, string serviceName, string routeT
}
var value = parameters[parameterInfo.Name];

_validationProcessor.Validate(parameterInfo, value, methodValidateAttribute);
if(methodValidateAttribute !=null)
_validationProcessor.Validate(parameterInfo, value);

var parameterType = parameterInfo.ParameterType;
var parameter = _typeConvertibleService.Convert(value, parameterType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Surging.Core.CPlatform.Validation
{
public interface IValidationProcessor
{
void Validate(ParameterInfo parameterInfo, object value, ValidateAttribute methodValidateAttribute = null);
void Validate(ParameterInfo parameterInfo, object value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,31 @@ public DefaultValidationProcessor(ITypeConvertibleService typeConvertibleService
_typeConvertibleService = typeConvertibleService;
}

public void Validate(ParameterInfo parameterInfo, object value, ValidateAttribute methodValidateAttribute = null)
public void Validate(ParameterInfo parameterInfo, object value)
{
Check.NotNull(parameterInfo, nameof(parameterInfo));

var parameterType = parameterInfo.ParameterType;
if (value != null)
{
var parameterType = parameterInfo.ParameterType;
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 customAttributes = parameterInfo.GetCustomAttributes(true);
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 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);

var isValueValid = Validator.TryValidateValue(parameter, validationContext,
validationResults, customValidAttributes);
if (isObjValid && isValueValid) return;

if (isObjValid && isValueValid) return;
throw new ValidateException(validationResults.Select(p => p.ErrorMessage).First());

throw new ValidateException(validationResults.Select(p => p.ErrorMessage).First());
}
}
}
}
Expand Down

0 comments on commit 0fa0718

Please sign in to comment.