public override IEnumerable <MetaValidationRuleValidationResult> Validate(IEnumerable <LengthValidator> validationRules)
        {
            var rules = validationRules.ToArray();

            if (!rules.Any())
            {
                yield return
                    (MetaValidationRuleValidationResult.CreateInvalidResult(
                         "'{0}' failed for property '{1}.{2}': No max-length validation rules defined.",
                         GetType().Name,
                         _propertyInfo.ReflectedType.FullName,
                         _propertyInfo.Name));
            }
            else if (rules.Where(r => r.Max > _maxLength).Any())
            {
                yield return
                    (MetaValidationRuleValidationResult.CreateInvalidResult(
                         "'{0}' failed for property '{1}.{2}': Max-length validation rule value '{3}' exceeds meta validation rule max-length value of '{4}'.",
                         GetType().Name,
                         _propertyInfo.ReflectedType.FullName,
                         _propertyInfo.Name,
                         rules.Max(r => r.Max),
                         _maxLength));
            }
            else
            {
                yield return(MetaValidationRuleValidationResult.CreateValidResult());
            }
        }
        public IRuleBuilderOptions <TValidatedType, TProperty> AddMetaValidationRule <TValidator> (
            Expression <Func <IEnumerable <TValidator>, bool> > metaValidationRuleExpression)
            where TValidator : IPropertyValidator
        {
            ArgumentUtility.CheckNotNull("metaValidationRuleExpression", metaValidationRuleExpression);

            var metaValidationRuleExecutor = metaValidationRuleExpression.Compile();

            var metaValidationRule = new DelegateMetaValidationRule <TValidator> (
                validationRules =>
            {
                var isValid = metaValidationRuleExecutor(validationRules);
                if (isValid)
                {
                    return(MetaValidationRuleValidationResult.CreateValidResult());
                }

                return(MetaValidationRuleValidationResult.CreateInvalidResult(
                           "Meta validation rule '{0}' failed for validator '{1}' on property '{2}.{3}'.",
                           metaValidationRuleExpression,
                           typeof(TValidator).FullName,
                           _addingComponentPropertyRule.Property.DeclaringType.FullName,
                           _addingComponentPropertyRule.Property.Name));
            });

            _addingMetaValidationPropertyRule.RegisterMetaValidationRule(metaValidationRule);
            return(this);
        }
        public void Validate()
        {
            var fakeResult   = MetaValidationRuleValidationResult.CreateValidResult();
            var delegateRule = new DelegateMetaValidationRule <IPropertyValidator> (v => fakeResult);

            var result = delegateRule.Validate(new IPropertyValidator[0]).First();

            Assert.That(result, Is.SameAs(fakeResult));
        }
        public void SetUp()
        {
            _validationCollectorProviderMock        = MockRepository.GenerateStrictMock <IValidationCollectorProvider>();
            _validationCollectorMergerMock          = MockRepository.GenerateStrictMock <IValidationCollectorMerger>();
            _metaRulesValidatorFactoryStub          = MockRepository.GenerateStub <IMetaRulesValidatorFactory>();
            _metaRuleValidatorMock                  = MockRepository.GenerateStrictMock <IMetaRuleValidator>();
            _validationRuleGlobalizationServiceMock = MockRepository.GenerateStrictMock <IValidationRuleMetadataService>();
            _memberInformationNameResolverMock      = MockRepository.GenerateStrictMock <IMemberInformationNameResolver>();
            _collectorValidatorMock                 = MockRepository.GenerateStrictMock <ICollectorValidator> ();

            _metaValidationRule1Stub = MockRepository.GenerateStub <IAddingComponentPropertyMetaValidationRule>();
            _metaValidationRule2Stub = MockRepository.GenerateStub <IAddingComponentPropertyMetaValidationRule>();
            _metaValidationRule3Stub = MockRepository.GenerateStub <IAddingComponentPropertyMetaValidationRule>();

            _componenValidationCollectorStub1 = MockRepository.GenerateStub <IComponentValidationCollector>();
            _componenValidationCollectorStub1.Stub(stub => stub.AddedPropertyMetaValidationRules).Return(new[] { _metaValidationRule1Stub });
            _componenValidationCollectorStub2 = MockRepository.GenerateStub <IComponentValidationCollector>();
            _componenValidationCollectorStub2.Stub(stub => stub.AddedPropertyMetaValidationRules)
            .Return(new[] { _metaValidationRule2Stub, _metaValidationRule3Stub });
            _componenValidationCollectorStub3 = MockRepository.GenerateStub <IComponentValidationCollector>();
            _componenValidationCollectorStub3.Stub(stub => stub.AddedPropertyMetaValidationRules)
            .Return(new IAddingComponentPropertyMetaValidationRule[0]);

            _validationCollectorInfo1 = new ValidationCollectorInfo(
                _componenValidationCollectorStub1,
                typeof(ApiBasedComponentValidationCollectorProvider));
            _validationCollectorInfo2 = new ValidationCollectorInfo(
                _componenValidationCollectorStub2,
                typeof(ApiBasedComponentValidationCollectorProvider));
            _validationCollectorInfo3 = new ValidationCollectorInfo(
                _componenValidationCollectorStub3,
                typeof(ApiBasedComponentValidationCollectorProvider));

            _validationRuleStub1 = MockRepository.GenerateStub <IValidationRule>();
            _validationRuleStub2 = MockRepository.GenerateStub <IValidationRule>();
            _validationRuleStub3 = PropertyRule.Create(ExpressionHelper.GetTypedMemberExpression <Customer, string> (c => c.FirstName));
            _validationRuleStub4 = PropertyRule.Create(ExpressionHelper.GetTypedMemberExpression <Customer, string> (c => c.LastName));

            _fakeValidationRuleResult           = new[] { _validationRuleStub1, _validationRuleStub2, _validationRuleStub3, _validationRuleStub4 };
            _fakeValidationCollectorMergeResult = new ValidationCollectorMergeResult(_fakeValidationRuleResult, MockRepository.GenerateStub <ILogContext>());

            _fluentValidationBuilder = new FluentValidatorBuilder(
                _validationCollectorProviderMock,
                _validationCollectorMergerMock,
                _metaRulesValidatorFactoryStub,
                _validationRuleGlobalizationServiceMock,
                _memberInformationNameResolverMock,
                _collectorValidatorMock);

            _validMetaValidationResult1   = MetaValidationRuleValidationResult.CreateValidResult();
            _validMetaValidationResult2   = MetaValidationRuleValidationResult.CreateValidResult();
            _invalidMetaValidationResult1 = MetaValidationRuleValidationResult.CreateInvalidResult("Error1");
            _invalidMetaValidationResult2 = MetaValidationRuleValidationResult.CreateInvalidResult("Error2");
        }
Beispiel #5
0
 public override IEnumerable <MetaValidationRuleValidationResult> Validate(IEnumerable <IPropertyValidator> validationRules)
 {
     if (validationRules.Count() > 3)
     {
         yield return
             (MetaValidationRuleValidationResult.CreateInvalidResult(
                  "More than three validators per property are not allowed!"));
     }
     else
     {
         yield return(MetaValidationRuleValidationResult.CreateValidResult());
     }
 }
        public void AddMetaValidationRule_FuncOverload()
        {
            var fakeValidationResult = MetaValidationRuleValidationResult.CreateValidResult();

            _addingComponentPropertyMetaValidationRuleMock.Expect(
                mock => mock.RegisterMetaValidationRule(
                    Arg <IMetaValidationRule> .Matches(
                        rule =>
                        rule.GetType() == typeof(DelegateMetaValidationRule <IPropertyValidator>) &&
                        rule.Validate(new IPropertyValidator[0]).First() == fakeValidationResult)));

            _addingComponentBuilder.AddMetaValidationRule(v => fakeValidationResult);

            _addingComponentPropertyMetaValidationRuleMock.VerifyAllExpectations();
        }
Beispiel #7
0
        public MetaValidationTestClassCollector4()
        {
            AddRule(c => c.Property1).AddMetaValidationRule(lengthRules =>
            {
                if (lengthRules.Count() > 1)
                {
                    return(MetaValidationRuleValidationResult.CreateInvalidResult("Invalid length validator count!"));
                }

                return(MetaValidationRuleValidationResult.CreateValidResult());
            });

            AddRule(c => c.Property1).Length(4, 10);
            AddRule(c => c.Property1).Length(6, 9);
        }
 public override IEnumerable <MetaValidationRuleValidationResult> Validate(IEnumerable <IPropertyValidator> validationRules)
 {
     if (!validationRules.Any())
     {
         yield return
             (MetaValidationRuleValidationResult.CreateInvalidResult(
                  "'{0}' failed for property '{1}.{2}': No validation rules defined.",
                  GetType().Name,
                  _property.ReflectedType.FullName,
                  _property.Name));
     }
     else
     {
         yield return(MetaValidationRuleValidationResult.CreateValidResult());
     }
 }
        public override IEnumerable <MetaValidationRuleValidationResult> Validate(IEnumerable <LengthValidator> validationRules)
        {
            var invalidValidators = validationRules.Where(lengthValidator => lengthValidator.Max > 50).ToArray();

            if (invalidValidators.Any())
            {
                foreach (var lengthValidator in invalidValidators)
                {
                    yield return
                        (MetaValidationRuleValidationResult.CreateInvalidResult(
                             "MaxLength-Constraints greater 50 not allowed for validator '{0}'!", lengthValidator.GetType().Name));
                }
            }
            else
            {
                yield return(MetaValidationRuleValidationResult.CreateValidResult());
            }
        }
Beispiel #10
0
        public void Validate()
        {
            var userNameExpression = ExpressionHelper.GetTypedMemberExpression <Customer, string> (c => c.UserName);
            var lastNameExpression = ExpressionHelper.GetTypedMemberExpression <Person, string> (c => c.LastName);

            var propertyRule1        = AddingComponentPropertyRule.Create(userNameExpression, typeof(CustomerValidationCollector1));
            var propertyRule2        = AddingComponentPropertyRule.Create(lastNameExpression, typeof(CustomerValidationCollector1));
            var propertyRule3        = AddingComponentPropertyRule.Create(lastNameExpression, typeof(CustomerValidationCollector2));
            var filteredPropertyRule = MockRepository.GenerateStub <IValidationRule>();

            propertyRule1.AddValidator(_propertyValidatorStub1);
            propertyRule1.AddValidator(_propertyValidatorStub2);
            propertyRule2.AddValidator(_propertyValidatorStub3);
            propertyRule2.AddValidator(_propertyValidatorStub4);
            propertyRule3.AddValidator(_propertyValidatorStub5);

            var systemMetaValidationRuleMock1 = MockRepository.GenerateStrictMock <IMetaValidationRule>();
            var systemMetaValidationRuleMock2 = MockRepository.GenerateStrictMock <IMetaValidationRule>();

            _systemMetaRulesProviderFactoryStub.Stub(stub => stub.Create(Arg <IPropertyInformation> .Is.Anything)).Return(_systemMetaRulesProviderStub);
            _systemMetaRulesProviderStub.Stub(stub => stub.GetSystemMetaValidationRules())
            .Return(new[] { systemMetaValidationRuleMock1, systemMetaValidationRuleMock2 });

            var metaValidationRuleMock1 = MockRepository.GenerateStrictMock <IMetaValidationRule>();
            var metaValidationRuleMock2 = MockRepository.GenerateStrictMock <IMetaValidationRule>();
            var metaValidationRuleMock3 = MockRepository.GenerateStrictMock <IMetaValidationRule>();

            _propertyMetaValidationRuleStub1.Stub(stub => stub.Property).Return(PropertyInfoAdapter.Create((PropertyInfo)userNameExpression.GetMember()));
            _propertyMetaValidationRuleStub1.Stub(stub => stub.MetaValidationRules).Return(new[] { metaValidationRuleMock1, metaValidationRuleMock2 });

            _propertyMetaValidationRuleStub2.Stub(stub => stub.Property).Return(PropertyInfoAdapter.Create((PropertyInfo)lastNameExpression.GetMember()));
            _propertyMetaValidationRuleStub2.Stub(stub => stub.MetaValidationRules).Return(new[] { metaValidationRuleMock3 });

            _propertyMetaValidationRuleStub3.Stub(stub => stub.Property).Return(PropertyInfoAdapter.Create((PropertyInfo)lastNameExpression.GetMember()));
            _propertyMetaValidationRuleStub3.Stub(stub => stub.MetaValidationRules).Return(new IMetaValidationRule[0]);

            systemMetaValidationRuleMock1
            .Expect(
                mock => mock.Validate(Arg <IEnumerable <IPropertyValidator> > .List.Equal(new[] { _propertyValidatorStub1, _propertyValidatorStub2 })))
            .Return(new[] { MetaValidationRuleValidationResult.CreateInvalidResult("Error System Mock 1") });
            systemMetaValidationRuleMock2
            .Expect(
                mock => mock.Validate(Arg <IEnumerable <IPropertyValidator> > .List.Equal(new[] { _propertyValidatorStub1, _propertyValidatorStub2 })))
            .Return(new[] { MetaValidationRuleValidationResult.CreateValidResult() });
            metaValidationRuleMock1
            .Expect(
                mock => mock.Validate(Arg <IEnumerable <IPropertyValidator> > .List.Equal(new[] { _propertyValidatorStub1, _propertyValidatorStub2 })))
            .Return(new[] { MetaValidationRuleValidationResult.CreateValidResult() });
            metaValidationRuleMock2
            .Expect(
                mock => mock.Validate(Arg <IEnumerable <IPropertyValidator> > .List.Equal(new[] { _propertyValidatorStub1, _propertyValidatorStub2 })))
            .Return(
                new[]
                { MetaValidationRuleValidationResult.CreateValidResult(), MetaValidationRuleValidationResult.CreateInvalidResult("Error Mock 2") });

            systemMetaValidationRuleMock1
            .Expect(
                mock =>
                mock.Validate(
                    Arg <IEnumerable <IPropertyValidator> > .List.Equal(
                        new[] { _propertyValidatorStub3, _propertyValidatorStub4, _propertyValidatorStub5 })))
            .Return(new[] { MetaValidationRuleValidationResult.CreateValidResult() });
            systemMetaValidationRuleMock2
            .Expect(
                mock =>
                mock.Validate(
                    Arg <IEnumerable <IPropertyValidator> > .List.Equal(
                        new[] { _propertyValidatorStub3, _propertyValidatorStub4, _propertyValidatorStub5 })))
            .Return(new[] { MetaValidationRuleValidationResult.CreateValidResult() });
            metaValidationRuleMock3
            .Expect(
                mock =>
                mock.Validate(
                    Arg <IEnumerable <IPropertyValidator> > .List.Equal(
                        new[] { _propertyValidatorStub3, _propertyValidatorStub4, _propertyValidatorStub5 })))
            .Return(new[] { MetaValidationRuleValidationResult.CreateValidResult() });

            var result = _validator.Validate(new[] { propertyRule1, propertyRule2, filteredPropertyRule, propertyRule3 }).ToArray();

            systemMetaValidationRuleMock1.VerifyAllExpectations();
            systemMetaValidationRuleMock2.VerifyAllExpectations();
            metaValidationRuleMock1.VerifyAllExpectations();
            metaValidationRuleMock2.VerifyAllExpectations();
            metaValidationRuleMock3.VerifyAllExpectations();
            Assert.That(result.Count(), Is.EqualTo(8));
            Assert.That(result[0].IsValid, Is.False);
            Assert.That(result[1].IsValid, Is.True);
            Assert.That(result[2].IsValid, Is.True);
            Assert.That(result[3].IsValid, Is.True);
            Assert.That(result[4].IsValid, Is.False);
            Assert.That(result[5].IsValid, Is.True);
            Assert.That(result[6].IsValid, Is.True);
            Assert.That(result[7].IsValid, Is.True);
        }