Beispiel #1
0
        public void Should_not_be_able_to_add_duplicate_validation_rules_to_default_property_convention()
        {
            DefaultPropertyConvention defaultPropertyConvention = new DefaultPropertyConvention(x => x.Name.Contains(""));

            defaultPropertyConvention.AddValidationRule <IsRequired <CanBeAnyViewModel> >();
            defaultPropertyConvention.AddValidationRule <IsRequired <CanBeAnyViewModel> >();

            defaultPropertyConvention.GetValidationRules().Count().ShouldEqual(1);
        }
        public void Should_add_the_default_property_conventions_to_discovered_types_when_they_are_added()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));
            defaultPropertyConvention1.AddValidationRule<IsUrl<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            DefaultPropertyConvention defaultPropertyConvention2 = new DefaultPropertyConvention(x => x.Name.Contains("Email"));
            defaultPropertyConvention2.AddValidationRule<IsEmail<CanBeAnyViewModel>>();
            defaultPropertyConvention2.AddValidationRule<IsRequired<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention2);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType<TestViewModel>();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(9);
        }
        public void TestFixtureSetUp()
        {
            _testViewModel           = new TestViewModel();
            _validationConfiguration = new ValidationConfiguration();

            var convention0 = new DefaultPropertyConvention(x => !x.Name.StartsWith("Optional"));

            convention0.AddValidationRule <IsRequired <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(convention0);

            var convention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));

            convention1.AddValidationRule <IsUrl <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(convention1);

            var convention2 = new DefaultPropertyConvention(x => x.Name.Contains("Email"));

            convention2.AddValidationRule <IsEmail <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(convention2);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType <TestViewModel>();

            IValidationResults <TestViewModel> validationResults = _validationConfiguration.Validate(_testViewModel);

            _testViewModel.ValidationResults.CloneFrom(validationResults);

            _testView = new TestView();
            _testView.SetModel(_testViewModel);
        }
Beispiel #4
0
        public void Should_remove_a_validation_rule_from_a_scanned_ICanBeValidated_implementation()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));

            defaultPropertyConvention1.AddValidationRule <IsUrl <CanBeAnyViewModel> >();
            defaultPropertyConvention1.AddValidationRule <IsEmail <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType <TestViewModel>();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(6);

            _validationConfiguration.DiscoveredTypes.RemoveRuleFrom <TestViewModel, IsUrl <TestViewModel> >();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(3);
        }
Beispiel #5
0
        public void Should_be_able_to_actually_validate_an_object_that_implements_ICanBeValidated_using_the_default_conventions()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));

            defaultPropertyConvention1.AddValidationRule <IsUrl <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            DefaultPropertyConvention defaultPropertyConvention2 = new DefaultPropertyConvention(x => x.Name.Contains("Email"));

            defaultPropertyConvention2.AddValidationRule <IsEmail <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention2);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType <TestViewModel>();

            IValidationResults <TestViewModel> validationResults = _validationConfiguration.Validate(_testViewModel);

            validationResults.ShouldNotBeNull();
            validationResults.GetInvalidFields().Count().ShouldEqual(3);
            validationResults.GetInvalidFields().First().ShouldEqual("property => property.False_Email_1");
            validationResults.GetInvalidFields().Last().ShouldEqual("property => property.False_Url");
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().First()).Count().ShouldEqual(1);
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().First()).First().ShouldBeOfType <IsEmail <TestViewModel> >();
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().Last()).Count().ShouldEqual(1);
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().Last()).First().ShouldBeOfType <IsUrl <TestViewModel> >();
        }
Beispiel #6
0
        public void Should_add_the_default_property_conventions_to_discovered_types_when_they_are_added()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));

            defaultPropertyConvention1.AddValidationRule <IsUrl <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            DefaultPropertyConvention defaultPropertyConvention2 = new DefaultPropertyConvention(x => x.Name.Contains("Email"));

            defaultPropertyConvention2.AddValidationRule <IsEmail <CanBeAnyViewModel> >();
            defaultPropertyConvention2.AddValidationRule <IsRequired <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention2);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType <TestViewModel>();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(9);
        }
        public void Should_add_a_validation_rule_to_a_scanned_ICanBeValidated_implementation_on_a_specific_property()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));
            defaultPropertyConvention1.AddValidationRule<IsUrl<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType<TestViewModel>();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(3);

            _validationConfiguration.DiscoveredTypes.AddRuleFor<TestViewModel>(x => x.Valid_Email, new ValidationRuleSetup(typeof(IsRequired<>), new AdditionalProperties()));

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(4);
        }
Beispiel #8
0
        public void Should_be_able_to_add_aditional_required_properties_to_validation_rule()
        {
            DefaultPropertyConvention defaultPropertyConvention = new DefaultPropertyConvention(x => x.Name.Contains("question"));

            var properties = new AdditionalProperties();
            var expression = new AdditionalPropertyExpression(properties);

            expression.NeedsAdditionalPropertyMatching(p => p.Name.Contains("answer"));
            expression.NeedsAdditionalPropertyMatching(p => !p.Name.Contains("question"));

            defaultPropertyConvention.AddValidationRule <IsRequired <CanBeAnyViewModel> >(properties);

            defaultPropertyConvention.GetValidationRules().Count().ShouldEqual(1);
            defaultPropertyConvention.GetAdditionalPropertiesForRule(defaultPropertyConvention.GetValidationRules().First()).Count().ShouldEqual(2);
        }
Beispiel #9
0
        public void Should_not_add_duplicate_validation_rules_to_a_scanned_ICanBeValidated_implementation_on_a_specific_property()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));

            defaultPropertyConvention1.AddValidationRule <IsUrl <CanBeAnyViewModel> >();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType <TestViewModel>();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(3);

            _validationConfiguration.DiscoveredTypes.AddRuleFor <TestViewModel>(x => x.Valid_Url_1, new ValidationRuleSetup(typeof(IsUrl <>), new AdditionalProperties()));

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(3);
        }
        public void Should_be_able_to_actually_validate_an_object_that_implements_ICanBeValidated_using_the_default_conventions()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));
            defaultPropertyConvention1.AddValidationRule<IsUrl<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            DefaultPropertyConvention defaultPropertyConvention2 = new DefaultPropertyConvention(x => x.Name.Contains("Email"));
            defaultPropertyConvention2.AddValidationRule<IsEmail<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention2);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType<TestViewModel>();

            IValidationResults<TestViewModel> validationResults = _validationConfiguration.Validate(_testViewModel);

            validationResults.ShouldNotBeNull();
            validationResults.GetInvalidFields().Count().ShouldEqual(3);
            validationResults.GetInvalidFields().First().ShouldEqual("property => property.False_Email_1");
            validationResults.GetInvalidFields().Last().ShouldEqual("property => property.False_Url");
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().First()).Count().ShouldEqual(1);
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().First()).First().ShouldBeOfType<IsEmail<TestViewModel>>();
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().Last()).Count().ShouldEqual(1);
            validationResults.GetBrokenRulesFor(validationResults.GetInvalidFields().Last()).First().ShouldBeOfType<IsUrl<TestViewModel>>();
        }
 public RuleExpression WillBeValidatedBy <TValidationRule>() where TValidationRule : IValidationRule <CanBeAnyViewModel>
 {
     _defaultPropertyConvention.AddValidationRule <TValidationRule>();
     return(this);
 }
        public void TestFixtureSetUp()
        {
            _testViewModel = new TestViewModel();
            _validationConfiguration = new ValidationConfiguration();

            var convention0 = new DefaultPropertyConvention(x => !x.Name.StartsWith("Optional"));
            convention0.AddValidationRule<IsRequired<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(convention0);

            var convention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));
            convention1.AddValidationRule<IsUrl<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(convention1);

            var convention2 = new DefaultPropertyConvention(x => x.Name.Contains("Email"));
            convention2.AddValidationRule<IsEmail<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(convention2);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType<TestViewModel>();

            IValidationResults<TestViewModel> validationResults = _validationConfiguration.Validate(_testViewModel);
            _testViewModel.ValidationResults.CloneFrom(validationResults);

            _testView = new TestView();
            _testView.SetModel(_testViewModel);
        }
        public void Should_be_able_to_add_aditional_required_properties_to_validation_rule()
        {
            DefaultPropertyConvention defaultPropertyConvention = new DefaultPropertyConvention(x => x.Name.Contains("question"));

            var properties = new AdditionalProperties();
            var expression = new AdditionalPropertyExpression(properties);
            expression.NeedsAdditionalPropertyMatching(p => p.Name.Contains("answer"));
            expression.NeedsAdditionalPropertyMatching(p => !p.Name.Contains("question"));

            defaultPropertyConvention.AddValidationRule<IsRequired<CanBeAnyViewModel>>(properties);

            defaultPropertyConvention.GetValidationRules().Count().ShouldEqual(1);
            defaultPropertyConvention.GetAdditionalPropertiesForRule(defaultPropertyConvention.GetValidationRules().First()).Count().ShouldEqual(2);
        }
        public void Should_remove_a_validation_rule_from_a_scanned_ICanBeValidated_implementation()
        {
            DefaultPropertyConvention defaultPropertyConvention1 = new DefaultPropertyConvention(x => x.Name.Contains("Url"));
            defaultPropertyConvention1.AddValidationRule<IsUrl<CanBeAnyViewModel>>();
            defaultPropertyConvention1.AddValidationRule<IsEmail<CanBeAnyViewModel>>();
            _validationConfiguration.DefaultPropertyConventions.AddDefaultPropertyConvention(defaultPropertyConvention1);

            _validationConfiguration.DiscoveredTypes.AddDiscoveredType<TestViewModel>();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(6);

            _validationConfiguration.DiscoveredTypes.RemoveRuleFrom<TestViewModel, IsUrl<TestViewModel>>();

            _validationConfiguration.DiscoveredTypes.GetRulesFor(new TestViewModel()).Count().ShouldEqual(3);
        }
        public void Should_not_be_able_to_add_duplicate_validation_rules_to_default_property_convention()
        {
            DefaultPropertyConvention defaultPropertyConvention = new DefaultPropertyConvention(x => x.Name.Contains(""));

            defaultPropertyConvention.AddValidationRule<IsRequired<CanBeAnyViewModel>>();
            defaultPropertyConvention.AddValidationRule<IsRequired<CanBeAnyViewModel>>();

            defaultPropertyConvention.GetValidationRules().Count().ShouldEqual(1);
        }