Exemple #1
0
        public static VerifyResult Verify(IValidator validator, Type declaringType, object instance, Type typeOfValidator)
        {
#if NET452
            var ret = validator.Validate(instance);
#else
            var ctx = FluentValidationContextFactory.Resolve(declaringType, instance);
            var ret = validator.Validate(ctx);
#endif

            if (ret.IsValid)
            {
                return(VerifyResult.Success);
            }

            var failures = ret.Errors.ConvertToVerifyFailures(typeOfValidator);
            return(new VerifyResult(failures)
            {
                NameOfExecutedRules = ret.RuleSetsExecuted.Copy()
            });
        }
Exemple #2
0
        public static VerifyResult VerifyOne(IValidator validator, VerifiableMemberContext context, Type typeOfValidator)
        {
            var propertyValidators = validator.CreateDescriptor().GetValidatorsForMember(context.MemberName);
            var parentContext      = FluentValidationContextFactory.Resolve(context.DeclaringType, default);

            var originalFailures    = new List <ValidationFailure>();
            var nameOfExecutedRules = new List <string>();

            foreach (var propertyValidator in propertyValidators)
            {
                foreach (var rule in (IEnumerable <IValidationRule>)validator)
                {
                    if (rule is PropertyRule propertyRule)
                    {
                        var propertyValidatorContext = new PropertyValidatorContext((ValidationContext)parentContext, propertyRule, context.MemberName, context.Value);

                        var localFailures = propertyValidator.Validate(propertyValidatorContext);

                        originalFailures.AddRange(localFailures);
                        nameOfExecutedRules.AddRange(propertyRule.RuleSets);
                    }
                }
            }

            if (!originalFailures.Any())
            {
                return(VerifyResult.Success);
            }

            var failures = originalFailures.ConvertToVerifyFailures(typeOfValidator);

            return(new VerifyResult(failures)
            {
                NameOfExecutedRules = nameOfExecutedRules.Distinct().ToArray()
            });
        }