public IEnumerable <ModelValidationResult> Validate(ModelMetadata metadata, IValidationContext validationContext, object container) { if (metadata.Model == null) { return(Enumerable.Empty <ModelValidationResult>()); } CustomizeValidatorAttribute validatorAttribute = null; foreach (var param in validationContext.ActionContext.ActionDescriptor.GetParameters() .Where(p => p.ParameterType == metadata.ModelType)) { validatorAttribute = param.GetCustomAttributes <CustomizeValidatorAttribute>().FirstOrDefault(); if (validatorAttribute != null) { break; } } if (validatorAttribute == null) { validatorAttribute = new CustomizeValidatorAttribute(); } var selector = validatorAttribute.ToValidatorSelector(); var interceptor = validatorAttribute.GetInterceptor(); var context = new ValidationContext(metadata.Model, new PropertyChain(), selector); if (interceptor != null) { // Allow the user to provide a customized context // However, if they return null then just use the original context. context = interceptor.BeforeWebApiValidation(validationContext.ActionContext, metadata, context) ?? context; } var result = validator.Validate(context); if (interceptor != null) { // allow the user to provice a custom collection of failures, which could be empty. // However, if they return null then use the original collection of failures. result = interceptor.AfterWebApiValidation(validationContext.ActionContext, metadata, context, result) ?? result; } return(!result.IsValid ? ConvertValidationResultToModelValidationResults(result) : Enumerable.Empty <ModelValidationResult>()); }
public IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, IValidationContext validationContext, object container) { if (metadata.Model == null) return Enumerable.Empty<ModelValidationResult>(); CustomizeValidatorAttribute validatorAttribute = null; foreach (var param in validationContext.ActionContext.ActionDescriptor.GetParameters() .Where(p => p.ParameterType == metadata.ModelType)) { validatorAttribute = param.GetCustomAttributes<CustomizeValidatorAttribute>().FirstOrDefault(); if (validatorAttribute != null) break; } if(validatorAttribute == null) validatorAttribute = new CustomizeValidatorAttribute(); var selector = validatorAttribute.ToValidatorSelector(); var interceptor = validatorAttribute.GetInterceptor(); var context = new ValidationContext(metadata.Model, new PropertyChain(), selector); if (interceptor != null) { // Allow the user to provide a customized context // However, if they return null then just use the original context. context = interceptor.BeforeWebApiValidation(validationContext.ActionContext, metadata, context) ?? context; } var result = validator.Validate(context); if (interceptor != null) { // allow the user to provice a custom collection of failures, which could be empty. // However, if they return null then use the original collection of failures. result = interceptor.AfterWebApiValidation(validationContext.ActionContext, metadata, context, result) ?? result; } return !result.IsValid ? ConvertValidationResultToModelValidationResults(result) : Enumerable.Empty<ModelValidationResult>(); }