internal static CustomizeValidatorAttribute GetCustomizations(ActionContext actionContext, Type type, string prefix)
        {
            if (actionContext?.ActionDescriptor?.Parameters == null)
            {
                return(new CustomizeValidatorAttribute());
            }

            var descriptors = actionContext.ActionDescriptor.Parameters
                              .Where(x => x.ParameterType == type)
                              .Where(x => (x.BindingInfo != null && x.BindingInfo.BinderModelName != null && x.BindingInfo.BinderModelName == prefix) || x.Name == prefix || (prefix == string.Empty && x.BindingInfo?.BinderModelName == null))
                              .OfType <ControllerParameterDescriptor>()
                              .ToList();

            CustomizeValidatorAttribute attribute = null;

            if (descriptors.Count == 1)
            {
                attribute = descriptors[0].ParameterInfo.GetCustomAttributes(typeof(CustomizeValidatorAttribute), true).FirstOrDefault() as CustomizeValidatorAttribute;
            }

            if (descriptors.Count > 1)
            {
                // We found more than 1 matching with same prefix and name.
            }

            return(attribute ?? new CustomizeValidatorAttribute());
        }
 public CustomValidationVisitor(ActionContext actionContext, IModelValidatorProvider validatorProvider, ValidatorCache validatorCache, IModelMetadataProvider metadataProvider, ValidationStateDictionary validationState, IValidatorFactory validatorFactory, CustomizeValidatorAttribute cutomizations) : base(actionContext, validatorProvider, validatorCache, metadataProvider, validationState)
 {
     _modelState        = actionContext.ModelState;
     _validatorCache    = validatorCache;
     _metadataProvider  = metadataProvider;
     _validatorFactory  = validatorFactory;
     _cutomizations     = cutomizations;
     _actionContext     = actionContext;
     _validatorProvider = validatorProvider;
 }
Exemplo n.º 3
0
/*
 *              private IModelValidatorProvider GetModelValidatorProvider(IValidatorFactory validatorFactory, CustomizeValidatorAttribute customizations) {
 *                      var modelValidatorProvider = ImplicitlyValidateChildProperties ? new FluentValidationModelValidatorProvider(validatorFactory, customizations, _validatorProvider) : (IModelValidatorProvider) _validatorProvider;
 *                      return modelValidatorProvider;
 *              }
 */


        protected virtual void RunDefaultValidation(ActionContext actionContext,
                                                    IModelValidatorProvider validatorProvider, ValidationStateDictionary validationState, string prefix,
                                                    object model, ModelMetadata metadata, bool implicitlyValidateChildPropertiesUsingFluentValidation,
                                                    IValidatorFactory factory, CustomizeValidatorAttribute customizations)
        {
            ValidationVisitor visitor;

//			if (ImplicitlyValidateChildProperties) {
//				visitor = new CustomValidationVisitor(actionContext, validatorProvider, _validatorCache, _modelMetadataProvider, validationState, factory, customizations);
//			}
//			else {
            visitor = new ValidationVisitor(
                actionContext,
                validatorProvider,
                _validatorCache,
                _modelMetadataProvider,
                validationState);
//			}
            visitor.Validate(metadata, prefix, model);
        }
Exemplo n.º 4
0
        internal static CustomizeValidatorAttribute GetCustomizations(ActionContext actionContext, Type type, string prefix)
        {
            IList <TDescriptor> FilterParameterDescriptors <TDescriptor>(IList <ParameterDescriptor> parameters)
            {
                return(parameters
                       .Where(x => x.ParameterType == type)
                       .Where(x => (x.BindingInfo != null && x.BindingInfo.BinderModelName != null && x.BindingInfo.BinderModelName == prefix) || x.Name == prefix || (prefix == string.Empty && x.BindingInfo?.BinderModelName == null))
                       .OfType <TDescriptor>()
                       .ToList());
            }

            CustomizeValidatorAttribute attribute = null;

            if (actionContext is ControllerContext controllerContext && controllerContext.ActionDescriptor?.Parameters != null)
            {
                var descriptors = FilterParameterDescriptors <ControllerParameterDescriptor>(actionContext.ActionDescriptor.Parameters);

                if (descriptors.Count == 1)
                {
                    attribute = descriptors[0].ParameterInfo.GetCustomAttributes(typeof(CustomizeValidatorAttribute), true).FirstOrDefault() as CustomizeValidatorAttribute;
                }
            }
 public FluentValidationModelValidator(IValidator validator, CustomizeValidatorAttribute customizations)
 {
     _validator      = validator;
     _customizations = customizations;
 }