예제 #1
0
        public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
        {
            var validatorAttribute = type.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ValidatorAttribute));

            if (validatorAttribute != null)
            {
                Type    validatorTypeDef = validatorAttribute.ConstructorArguments.First().Value as Type;
                dynamic validator        = validatorTypeDef.Assembly.CreateInstance(validatorTypeDef.FullName);

                schema.required = new List <string>();

                IValidatorDescriptor validatorDescriptor = validator.CreateDescriptor();

                if (schema.properties != null)
                {
                    foreach (var key in schema.properties.Keys)
                    {
                        var memberValidators = validatorDescriptor.GetMembersWithValidators().Where(i => i.Key.Equals(key, StringComparison.OrdinalIgnoreCase));

                        foreach (var validatorType in memberValidators)
                        {
                            if (validatorType.Any(v => v is NotEmptyValidator))
                            {
                                schema.required.Add(key);
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        private IEnumerable<PropertyValidatorResult> GetNestedPropertyValidators(IValidatorDescriptor desc, List<PropertyInfo> propertyInfo, int i)
        {
            if (i == propertyInfo.Count)
                return new List<PropertyValidatorResult>();

            var vals = desc.GetValidatorsForMember(propertyInfo[i].Name);
            var name = desc.GetName(propertyInfo[i].Name);

            var propertyValidators = new List<PropertyValidatorResult>();

            foreach (var inlineval in vals)
            {
                if (i == propertyInfo.Count - 1)
                    propertyValidators.Add(new PropertyValidatorResult(inlineval, name));

                IValidator val = GetValidator(inlineval, null);

                if (val == null)
                    continue;

                var morevals = GetNestedPropertyValidators(val.CreateDescriptor(), propertyInfo, i + 1);
                propertyValidators.AddRange(morevals.Select(x => new PropertyValidatorResult(x.PropertyValidator, x.DisplayName)));
            }

            return propertyValidators;
        }
        private void ApplyRulesToSchema(Schema schema, SchemaFilterContext context, IValidator validator)
        {
            IValidatorDescriptor validatorDescriptor = validator.CreateDescriptor();

            foreach (var key in schema?.Properties?.Keys ?? Array.Empty <string>())
            {
                foreach (var propertyValidator in validatorDescriptor.GetValidatorsForMemberIgnoreCase(key).NotNull())
                {
                    foreach (var rule in _rules)
                    {
                        if (rule.Matches(propertyValidator))
                        {
                            try
                            {
                                rule.Apply(new RuleContext(schema, context, key, propertyValidator));
                            }
                            catch (Exception e)
                            {
                                _logger?.LogWarning(0, e, $"Error on apply rule '{rule.Name}' for key '{key}'.");
                            }
                        }
                    }
                }
            }
        }
예제 #4
0
        private static IPropertyValidator[] GetModelLevelValidators(IValidatorDescriptor descriptor)
        {
            var rules = descriptor.GetRulesForMember(null).OfType <PropertyRule>();

            return(rules.Where(x => x.Expression.IsParameterExpression()).SelectMany(x => x.Validators)
                   .ToArray());
        }
예제 #5
0
        private IEnumerable <PropertyValidatorResult> GetNestedPropertyValidators(IValidatorDescriptor desc, List <PropertyInfo> propertyInfo, int i)
        {
            if (i == propertyInfo.Count)
            {
                return(new List <PropertyValidatorResult>());
            }

            var vals = desc.GetValidatorsForMember(propertyInfo[i].Name);
            var name = desc.GetName(propertyInfo[i].Name);

            var propertyValidators = new List <PropertyValidatorResult>();

            foreach (var inlineval in vals)
            {
                if (i == propertyInfo.Count - 1)
                {
                    propertyValidators.Add(new PropertyValidatorResult(inlineval, name));
                }

                IValidator val = GetValidator(inlineval, null);

                if (val == null)
                {
                    continue;
                }

                var morevals = GetNestedPropertyValidators(val.CreateDescriptor(), propertyInfo, i + 1);
                propertyValidators.AddRange(morevals.Select(x => new PropertyValidatorResult(x.PropertyValidator, x.DisplayName)));
            }

            return(propertyValidators);
        }
예제 #6
0
        private IEnumerable<PropertyValidatorResult> GetNestedPropertyValidators(IValidatorDescriptor desc, List<PropertyInfo> propertyInfo, int i)
        {
            if (i == propertyInfo.Count)
                return new List<PropertyValidatorResult>();

            var vals = desc.GetValidatorsForMember(propertyInfo[i].Name);
            var name = desc.GetName(propertyInfo[i].Name);

            var propertyValidators = new List<PropertyValidatorResult>();

            foreach (var inlineval in vals)
            {
                var valtype = inlineval.GetType();
                IValidator val = null;
                if (valtype == typeof(ChildCollectionValidatorAdaptor))
                    val = ((ChildCollectionValidatorAdaptor)inlineval).Validator;
                else if (valtype == typeof(ChildValidatorAdaptor))
                    val = ((ChildValidatorAdaptor)inlineval).Validator;

                if (i == propertyInfo.Count - 1)
                    propertyValidators.Add(new PropertyValidatorResult(inlineval, name));

                if (val == null)
                    continue;

                var morevals = GetNestedPropertyValidators(val.CreateDescriptor(), propertyInfo, i + 1);
                propertyValidators.AddRange(morevals.Select(x => new PropertyValidatorResult(x.PropertyValidator, name)));
            }

            return propertyValidators;
        }
예제 #7
0
        /// <summary>
        /// Crée un validator de propriété.
        /// </summary>
        /// <param name="attribute">L'attribut de validation.</param>
        /// <param name="validatedElement">La propriété validée.</param>
        /// <param name="builder">Le constructeur de validation composite.</param>
        /// <param name="mainValidatorFactory">La brique à utiliser lors de la construction des validateurs imbriqués..</param>
        /// <returns>La validateur de propriété.</returns>
        protected Validator CreateValidatorForValidatedElement(IValidatorDescriptor attribute, IValidatedElement validatedElement, CompositeValidatorBuilder builder, ValidatorFactory mainValidatorFactory)
        {
            Validator valueValidator = attribute.CreateValidator(validatedElement.TargetType, validatedElement.MemberInfo.ReflectedType, new MemberAccessValidatorBuilderFactory().MemberValueAccessBuilder, mainValidatorFactory);

            builder.AddValueValidator(valueValidator);

            return(builder.GetValidator());
        }
        private void AddRulesForMember(IValidatorDescriptor descriptor, string memberName, string propertyPrefix = "")
        {
            var propertyRules = descriptor
                                .GetRulesForMember(memberName)
                                .OfType <PropertyRule>();

            foreach (var rule in propertyRules)
            {
                foreach (var validator in rule.Validators)
                {
                    this.AddRuleOrDescendantRules(rule, validator, $"{propertyPrefix}{memberName}");
                }
            }
        }
예제 #9
0
        private static IEnumerable <IPropertyValidator> GetPropertyValidators(IValidatorDescriptor descriptor, string propertyName, Func <IPropertyValidator, bool> predicate)
        {
            // todo: recursive scan for includes?

            var propertyRules = descriptor.GetRulesForMember(propertyName);

            if (propertyRules != null)
            {
                foreach (var propertyRule in propertyRules)
                {
                    foreach (var validator in propertyRule.Components.Select(c => c.Validator))
                    {
                        if (predicate(validator))
                        {
                            yield return(validator);
                        }
                    }
                }
            }

            var rules = descriptor.GetRulesForMember(null);

            if (rules != null)
            {
                foreach (var includeRule in rules.Where(r => typeof(IIncludeRule).IsAssignableFrom(r.GetType())))
                {
                    var includedPropertyRules = includeRule.Components.Select(c => c.Validator).OfType <IChildValidatorAdaptor>()
                                                .Select(a => Activator.CreateInstance(a.ValidatorType))
                                                .Cast <IEnumerable <IValidationRule> >()
                                                .SelectMany(x => x)
                                                .ToArray();

                    foreach (var includedPropertyRule in includedPropertyRules)
                    {
                        if (includedPropertyRule.Member.Name != propertyName)
                        {
                            continue;
                        }

                        foreach (var validator in includedPropertyRule.Components.Select(c => c.Validator))
                        {
                            if (predicate(validator))
                            {
                                yield return(validator);
                            }
                        }
                    }
                }
            }
        }
예제 #10
0
        private IEnumerable <PropertyValidatorResult> GetNestedPropertyValidators(IValidatorDescriptor desc, List <PropertyInfo> propertyInfo, int i)
        {
            if (i == propertyInfo.Count)
            {
                return(new List <PropertyValidatorResult>());
            }

            var vals = desc.GetValidatorsForMember(propertyInfo[i].Name);
            var name = desc.GetName(propertyInfo[i].Name);

            var propertyValidators = new List <PropertyValidatorResult>();

            foreach (var inlineval in vals)
            {
                var        valtype = inlineval.GetType();
                IValidator val     = null;
                if (valtype == typeof(ChildCollectionValidatorAdaptor))
                {
                    val = ((ChildCollectionValidatorAdaptor)inlineval).Validator;
                }
                else if (valtype == typeof(ChildValidatorAdaptor))
                {
                    val = ((ChildValidatorAdaptor)inlineval).Validator;
                }

                if (i == propertyInfo.Count - 1)
                {
                    propertyValidators.Add(new PropertyValidatorResult(inlineval, name));
                }

                if (val == null)
                {
                    continue;
                }

                var morevals = GetNestedPropertyValidators(val.CreateDescriptor(), propertyInfo, i + 1);
                propertyValidators.AddRange(morevals.Select(x => new PropertyValidatorResult(x.PropertyValidator, name)));
            }

            return(propertyValidators);
        }
 public static IEnumerable <IPropertyValidator> GetValidatorsForMemberIgnoreCase(this IValidatorDescriptor validatorDescriptor, string name)
 {
     return(validatorDescriptor.GetMembersWithValidators().FirstOrDefault(grouping => grouping.Key.Equals(name, StringComparison.InvariantCultureIgnoreCase)));
 }
예제 #12
0
        private static IEnumerable <IPropertyValidator> GetDependentRules <T, TProperty>(string expressionMemberName, Expression <Func <T, TProperty> > expression, IValidatorDescriptor descriptor)
        {
            var member = expression.IsParameterExpression() ? null : expressionMemberName;
            var rules  = descriptor.GetRulesForMember(member).OfType <PropertyRule>().SelectMany(x => x.DependentRules)
                         .SelectMany(x => x.Validators);

            return(rules);
        }
 /// <summary>
 /// Initialises a new instance of the <see cref="ValidationListerBase"/> class with a specified <see cref="IValidator"/>.
 /// </summary>
 /// <param name="validator">An instance of a FluentValidation <see cref="IValidator"/>.</param>
 /// <param name="modelType">The <see cref="Type"/> of the model being validated.</param>
 protected ValidationListerBase(IValidator validator, Type modelType)
 {
     this.validatorDescriptor = validator?.CreateDescriptor() ?? throw new ArgumentNullException(nameof(validator));
     this.ModelType           = modelType ?? throw new ArgumentNullException(nameof(modelType));
 }