Beispiel #1
0
        public void CanDeserializeSerializedSectionWithTypeWithValidatorsForNamedRule()
        {
            ValidationSettings     rwSettings   = new ValidationSettings();
            ValidatedTypeReference rwStringType = new ValidatedTypeReference(typeof(string));

            rwSettings.Types.Add(rwStringType);
            ValidationRulesetData rwValidationRule = new ValidationRulesetData("ruleset");

            rwStringType.Rulesets.Add(rwValidationRule);
            ValidatorData rwValidatorData = new MockValidatorData("validator1", true);

            rwValidationRule.Validators.Add(rwValidatorData);

            IDictionary <string, ConfigurationSection> sections = new Dictionary <string, ConfigurationSection>();

            sections[ValidationSettings.SectionName] = rwSettings;

            using (ConfigurationFileHelper configurationFileHelper = new ConfigurationFileHelper(sections))
            {
                IConfigurationSource configurationSource = configurationFileHelper.ConfigurationSource;

                ValidationSettings roSettings = configurationSource.GetSection(ValidationSettings.SectionName) as ValidationSettings;

                Assert.IsNotNull(roSettings);
                Assert.AreEqual(1, roSettings.Types.Count);
                Assert.AreEqual(typeof(string).FullName, roSettings.Types.Get(0).Name);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Count);
                Assert.AreEqual("ruleset", roSettings.Types.Get(0).Rulesets.Get(0).Name);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Get(0).Validators.Count);
                Assert.AreEqual("validator1", roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0).Name);
                Assert.AreSame(typeof(MockValidatorData), roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0).GetType());
                Assert.AreEqual(true, ((MockValidatorData)roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0)).ReturnFailure);
            }
        }
Beispiel #2
0
        public void CanDeserializeSerializedSectionWithTypeWithEmptyNamedRule()
        {
            ValidationSettings     rwSettings   = new ValidationSettings();
            ValidatedTypeReference rwStringType = new ValidatedTypeReference(typeof(string));

            rwSettings.Types.Add(rwStringType);
            rwStringType.Rulesets.Add(new ValidationRulesetData("ruleset"));

            IDictionary <string, ConfigurationSection> sections = new Dictionary <string, ConfigurationSection>();

            sections[ValidationSettings.SectionName] = rwSettings;

            using (ConfigurationFileHelper configurationFileHelper = new ConfigurationFileHelper(sections))
            {
                IConfigurationSource configurationSource = configurationFileHelper.ConfigurationSource;

                ValidationSettings roSettings = configurationSource.GetSection(ValidationSettings.SectionName) as ValidationSettings;

                Assert.IsNotNull(roSettings);
                Assert.AreEqual(1, roSettings.Types.Count);
                Assert.AreEqual(typeof(string).FullName, roSettings.Types.Get(0).Name);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Count);
                Assert.AreEqual("ruleset", roSettings.Types.Get(0).Rulesets.Get(0).Name);
            }
        }
Beispiel #3
0
        public ValidationSettings Build()
        {
            blockSettings = new ValidationSettings();
            if (!blockSettingsNode.RequirePermission)                   // don't set if false
            {
                blockSettings.SectionInformation.RequirePermission = blockSettingsNode.RequirePermission;
            }

            foreach (TypeNode typeNode in blockSettingsNode.Hierarchy.FindNodesByType(typeof(TypeNode)))
            {
                ValidatedTypeReference typeReference = new ValidatedTypeReference();

                typeReference.Name         = typeNode.TypeName;
                typeReference.AssemblyName = typeNode.AssemblyName;

                if (typeNode.DefaultRule != null)
                {
                    typeReference.DefaultRuleset = typeNode.DefaultRule.Name;
                }
                BuildRules(typeReference, typeNode);

                blockSettings.Types.Add(typeReference);
            }
            return(blockSettings);
        }
Beispiel #4
0
        public void CreateValidatorForDefaultRuleForExistingTypeNonExistingDefaultRuleSetReturnsEmptyValidator()
        {
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(TestClass));

            typeReference.DefaultRuleset = "ruleset2";
            ValidationRulesetData ruleData = new ValidationRulesetData("ruleset1");

            typeReference.Rulesets.Add(ruleData);
            ValidatedMethodReference methodReference1 = new ValidatedMethodReference("PublicMethod");

            ruleData.Methods.Add(methodReference1);
            ValidatedMethodReference methodReference2 = new ValidatedMethodReference("NonPublicMethod");

            ruleData.Methods.Add(methodReference2);
            methodReference1.Validators.Add(new MockValidatorData("validator1", false));
            methodReference1.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            methodReference2.Validators.Add(new MockValidatorData("validator2", false));
            methodReference2.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = builder.CreateValidator(typeof(TestClass), typeReference, string.Empty);

            Assert.IsNotNull(validator);
            Assert.AreSame(typeof(AndCompositeValidator), validator.GetType());
            IList <Validator> validators = ValidationTestHelper.CreateListFromEnumerable(((AndCompositeValidator)validator).Validators);

            Assert.AreEqual(0, validators.Count);
            Assert.AreEqual(0, mockFactory.requestedMembers.Count);
        }
        public void Given()
        {
            container = new UnityContainer();
            var configurationSource     = new DictionaryConfigurationSource();
            ValidationSettings settings = new ValidationSettings();

            configurationSource.Add(ValidationSettings.SectionName, settings);
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(BaseTestDomainObject));

            settings.Types.Add(typeReference);
            typeReference.DefaultRuleset = "RuleA";
            ValidationRulesetData ruleData = new ValidationRulesetData("RuleA");

            typeReference.Rulesets.Add(ruleData);
            ValidatedPropertyReference propertyReference1 = new ValidatedPropertyReference("Property1");

            ruleData.Properties.Add(propertyReference1);
            MockValidatorData validator11 = new MockValidatorData("validator1", true);

            propertyReference1.Validators.Add(validator11);
            validator11.MessageTemplate = "message-from-config1-RuleA";
            MockValidatorData validator12 = new MockValidatorData("validator2", true);

            propertyReference1.Validators.Add(validator12);
            validator12.MessageTemplate = "message-from-config2-RuleA";
            MockValidatorData validator13 = new MockValidatorData("validator3", false);

            propertyReference1.Validators.Add(validator13);
            validator13.MessageTemplate = "message-from-config3-RuleA";

            var typeRegistrationProvider = new ValidationTypeRegistrationProvider();
            var configurator             = new UnityContainerConfigurator(container);

            configurator.RegisterAll(configurationSource, typeRegistrationProvider);
        }
Beispiel #6
0
    public ValidationConfigurationBuilderType <T> ForType <T>()
    {
        ValidatedTypeReference typeReference;

        if (this.settings.Types.Contains(typeof(T).FullName))
        {
            typeReference = this.settings.Types.Get(typeof(T).FullName);
        }
        else
        {
            typeReference = new ValidatedTypeReference(typeof(T))
            {
                AssemblyName = typeof(T).Assembly.GetName().FullName,
            };
            if (this.defaultRulesetName != null)
            {
                typeReference.Rulesets.Add(new ValidationRulesetData(this.defaultRulesetName));
                typeReference.DefaultRuleset = this.defaultRulesetName;
            }
            foreach (var alternativeRulesetName in this.alternativeRulesetNames)
            {
                typeReference.Rulesets.Add(new ValidationRulesetData(alternativeRulesetName));
            }
            this.settings.Types.Add(typeReference);
        }
        return(new ValidationConfigurationBuilderType <T>(this, typeReference));
    }
        private static IConfigurationSource GetNotNullValidationConfig()
        {
            var validatorData = new NotNullValidatorData()
            {
                Name            = "Not Null",
                MessageTemplate = "City cannot be null"
            };

            var fieldRef = new ValidatedFieldReference()
            {
                Name = "City"
            };

            fieldRef.Validators.Add(validatorData);

            var rulesetData = new ValidationRulesetData("default");

            rulesetData.Fields.Add(fieldRef);

            var typeData = new ValidatedTypeReference(typeof(TargetAddress));

            typeData.Rulesets.Add(rulesetData);
            typeData.DefaultRuleset = rulesetData.Name;

            var section = new ValidationSettings();

            section.Types.Add(typeData);

            var configSource = new DictionaryConfigurationSource();

            configSource.Add(BlockSectionNames.Validation, section);
            return(configSource);
        }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeNode"/> class for a <see cref="ValidatedTypeReference"/>.
 /// </summary>
 /// <param name="typeReferenceData">The <see cref="ValidatedTypeReference"/> to be represented.</param>
 public TypeNode(ValidatedTypeReference typeReferenceData)
     : base(nameFormatter.CreateName(typeReferenceData))
 {
     defaultRuleNodeRemoved = new EventHandler<ConfigurationNodeChangedEventArgs>(OnDefaultRuleNodeRemoved);
     typeName = typeReferenceData.Name;
     assemblyName = typeReferenceData.AssemblyName;
 }
Beispiel #9
0
        public void CreateValidatorForExistingTypeReturnsNonEmptyValidatorBasedOnRuleDefinition()
        {
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(TestClass));
            ValidationRulesetData  ruleData      = new ValidationRulesetData("ruleset1");

            typeReference.Rulesets.Add(ruleData);
            ValidatedMethodReference methodReference1 = new ValidatedMethodReference("PublicMethod");

            ruleData.Methods.Add(methodReference1);
            ValidatedMethodReference methodReference2 = new ValidatedMethodReference("NonPublicMethod");

            ruleData.Methods.Add(methodReference2);
            methodReference1.Validators.Add(new MockValidatorData("validator1", false));
            methodReference1.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            methodReference2.Validators.Add(new MockValidatorData("validator2", false));
            methodReference2.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = this.builder.CreateValidator(typeof(TestClass), typeReference, "ruleset1");

            Assert.IsNotNull(validator);
            Assert.AreSame(typeof(AndCompositeValidator), validator.GetType());
            IList <Validator> validators = ValidationTestHelper.CreateListFromEnumerable(((AndCompositeValidator)validator).Validators);

            Assert.AreEqual(1, validators.Count);
            Assert.AreEqual(1, this.mockFactory.requestedMembers.Count);
            ValueAccessValidatorBuilder valueAccessValidatorBuilder1 = this.mockFactory.requestedMembers["TestClass.PublicMethod"];

            Assert.AreSame(valueAccessValidatorBuilder1.BuiltValidator, validators[0]);
            Assert.AreEqual(1, valueAccessValidatorBuilder1.ValueValidators.Count);
            Assert.AreEqual("validator 1 message", ((MockValidator <object>)valueAccessValidatorBuilder1.ValueValidators[0]).MessageTemplate);
        }
Beispiel #10
0
        public void CreateValidatorForDefaultRuleForExistingTypeReturnsNonEmptyValidatorBasedOnDefaultRuleDefinition()
        {
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(TestClass));

            typeReference.DefaultRuleset = "ruleset1";
            ValidationRulesetData ruleData = new ValidationRulesetData("ruleset1");

            typeReference.Rulesets.Add(ruleData);
            ValidatedMethodReference methodReference1 = new ValidatedMethodReference("PublicMethod");

            ruleData.Methods.Add(methodReference1);
            ValidatedMethodReference methodReference2 = new ValidatedMethodReference("NonPublicMethod");

            ruleData.Methods.Add(methodReference2);
            methodReference1.Validators.Add(new MockValidatorData("validator1", false));
            methodReference1.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            methodReference2.Validators.Add(new MockValidatorData("validator2", false));
            methodReference2.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = builder.CreateValidator(typeof(TestClass), typeReference, string.Empty);

            Assert.IsNotNull(validator);
            Assert.AreSame(typeof(ValueAccessValidator), validator.GetType());
            ValueAccessValidatorBuilder valueAccessValidatorBuilder1 = mockFactory.requestedMembers["TestClass.PublicMethod"];

            Assert.AreSame(valueAccessValidatorBuilder1.BuiltValidator, validator);
            Assert.AreEqual("validator 1 message", ((MockValidator <object>)valueAccessValidatorBuilder1.ValueValidators[0]).MessageTemplate);
        }
        public void CanBuildValidationInstanceFactoryFromGivenConfiguration()
        {
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();
            ValidationSettings            settings            = new ValidationSettings();

            configurationSource.Add(ValidationSettings.SectionName, settings);
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(BaseTestDomainObject));

            settings.Types.Add(typeReference);
            typeReference.DefaultRuleset = "RuleA";
            ValidationRulesetData ruleData = new ValidationRulesetData("RuleA");

            typeReference.Rulesets.Add(ruleData);
            ValidatedPropertyReference propertyReference1 = new ValidatedPropertyReference("Property1");

            ruleData.Properties.Add(propertyReference1);
            MockValidatorData validator11 = new MockValidatorData("validator1", true);

            propertyReference1.Validators.Add(validator11);
            validator11.MessageTemplate = "message-from-config1-RuleA";

            ValidationFactory.SetDefaultConfigurationValidatorFactory(configurationSource);
            ValidatorFactory factory = ValidationFactory.DefaultCompositeValidatorFactory;

            var validator = factory.CreateValidator <BaseTestDomainObject>("RuleA");
            var results   = validator.Validate(new BaseTestDomainObject());

            Assert.IsNotNull(factory);
            Assert.IsFalse(results.IsValid);
            Assert.AreEqual(validator11.MessageTemplate, results.ElementAt(0).Message);
        }
Beispiel #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeNode"/> class for a <see cref="ValidatedTypeReference"/>.
 /// </summary>
 /// <param name="typeReferenceData">The <see cref="ValidatedTypeReference"/> to be represented.</param>
 public TypeNode(ValidatedTypeReference typeReferenceData)
     : base(nameFormatter.CreateName(typeReferenceData))
 {
     defaultRuleNodeRemoved = new EventHandler <ConfigurationNodeChangedEventArgs>(OnDefaultRuleNodeRemoved);
     typeName     = typeReferenceData.Name;
     assemblyName = typeReferenceData.AssemblyName;
 }
Beispiel #13
0
        /// <summary>
        /// Retruns a friendly name based on a <see cref="ValidatedTypeReference"/> instance.
        /// </summary>
        /// <param name="typeConfiguration">The <see cref="ValidatedTypeReference"/> that should be used to create a name.</param>
        /// <returns>A friendly name that can be used as a display name.</returns>
        public string CreateName(ValidatedTypeReference typeConfiguration)
        {
            if (typeConfiguration == null)
            {
                throw new ArgumentNullException("typeConfiguration");
            }

            return(base.CreateName(typeConfiguration.Name));
        }
Beispiel #14
0
        private ValidatedTypeReference GetTypeReference(Type type)
        {
            if (null == validationSettings)
            {
                return(null);
            }

            ValidatedTypeReference typeReference = validationSettings.Types.Get(type.FullName);

            return(typeReference);
        }
Beispiel #15
0
        public void CreateValidatorForNonExistingRuleReturnsEmptyValidator()
        {
            ValidatedTypeReference typeReference = new ValidatedTypeReference();

            Validator validator = builder.CreateValidator(typeof(TestClass), typeReference, "ruleset1");

            Assert.IsNotNull(validator);
            Assert.AreSame(typeof(AndCompositeValidator), validator.GetType());
            IList <Validator> validators = ValidationTestHelper.CreateListFromEnumerable(((AndCompositeValidator)validator).Validators);

            Assert.AreEqual(0, validators.Count);
        }
Beispiel #16
0
        public void CanDeserializeSerializedSectionWithNamedRuleSpecifyingMultipleValidatorsForField()
        {
            ValidationSettings     rwSettings   = new ValidationSettings();
            ValidatedTypeReference rwStringType = new ValidatedTypeReference(typeof(string));

            rwSettings.Types.Add(rwStringType);
            ValidationRulesetData rwValidationRule = new ValidationRulesetData("ruleset");

            rwStringType.Rulesets.Add(rwValidationRule);
            ValidatorData rwValidatorData = new MockValidatorData("validator1", true);

            rwValidationRule.Validators.Add(rwValidatorData);
            ValidatedFieldReference rwValidationFieldReference = new ValidatedFieldReference("System.Object.GetHashCode");

            rwValidationRule.Fields.Add(rwValidationFieldReference);
            ValidatorData rwFieldValidatorData1 = new MockValidatorData("ruleset-validator1", false);

            rwValidationFieldReference.Validators.Add(rwFieldValidatorData1);
            ValidatorData rwFieldValidatorData2 = new MockValidatorData("ruleset-validator2", false);

            rwValidationFieldReference.Validators.Add(rwFieldValidatorData2);

            IDictionary <string, ConfigurationSection> sections = new Dictionary <string, ConfigurationSection>();

            sections[ValidationSettings.SectionName] = rwSettings;

            using (ConfigurationFileHelper configurationFileHelper = new ConfigurationFileHelper(sections))
            {
                IConfigurationSource configurationSource = configurationFileHelper.ConfigurationSource;

                ValidationSettings roSettings = configurationSource.GetSection(ValidationSettings.SectionName) as ValidationSettings;

                Assert.IsNotNull(roSettings);
                Assert.AreEqual(1, roSettings.Types.Count);
                Assert.AreEqual(typeof(string).FullName, roSettings.Types.Get(0).Name);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Count);
                Assert.AreEqual("ruleset", roSettings.Types.Get(0).Rulesets.Get(0).Name);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Get(0).Validators.Count);
                Assert.AreEqual("validator1", roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0).Name);
                Assert.AreSame(typeof(MockValidatorData), roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0).GetType());
                Assert.AreEqual(true, ((MockValidatorData)roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0)).ReturnFailure);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Get(0).Fields.Count);
                Assert.AreEqual("System.Object.GetHashCode", roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Name);
                Assert.AreEqual(2, roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Count);
                Assert.AreEqual("ruleset-validator1", roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(0).Name);
                Assert.AreSame(typeof(MockValidatorData), roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(0).GetType());
                Assert.AreEqual(false, ((MockValidatorData)roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(0)).ReturnFailure);
                Assert.AreEqual("ruleset-validator2", roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(1).Name);
                Assert.AreSame(typeof(MockValidatorData), roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(1).GetType());
                Assert.AreEqual(false, ((MockValidatorData)roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(1)).ReturnFailure);
            }
        }
Beispiel #17
0
        private void BuildTypeNode(TypeNode typeNode, ValidatedTypeReference typeReference)
        {
            foreach (ValidationRulesetData rule in typeReference.Rulesets)
            {
                RuleSetNode ruleNode = new RuleSetNode(rule);

                BuildRuleNode(ruleNode, rule);

                typeNode.AddNode(ruleNode);
                if (string.Compare(typeReference.DefaultRuleset, rule.Name) == 0)
                {
                    typeNode.DefaultRule = ruleNode;
                }
            }
        }
Beispiel #18
0
        public Validator CreateValidator(Type type, ValidatedTypeReference typeReference, string ruleset)
        {
            if (string.IsNullOrEmpty(ruleset))
            {
                ruleset = typeReference.DefaultRuleset;
            }

            ValidationRulesetData ruleData = typeReference.Rulesets.Get(ruleset);

            if (null == ruleData)
            {
                return(emptyValidator);
            }

            return(this.CreateValidator(new ConfigurationValidatedType(ruleData, type)));
        }
Beispiel #19
0
        public void CanCreateValidatorForTypeFromAttributesAndConfigurationWithDefaultRulesetName()
        {
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();
            ValidationSettings            settings            = new ValidationSettings();

            configurationSource.Add(ValidationSettings.SectionName, settings);
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(TestObjectWithFailingAttributesOnProperties));

            settings.Types.Add(typeReference);
            typeReference.DefaultRuleset = "RuleA";
            ValidationRulesetData ruleData = new ValidationRulesetData("RuleA");

            typeReference.Rulesets.Add(ruleData);
            ValidatedPropertyReference propertyReference1 = new ValidatedPropertyReference("FailingProperty1");

            ruleData.Properties.Add(propertyReference1);
            MockValidatorData validator11 = new MockValidatorData("validator1", true);

            propertyReference1.Validators.Add(validator11);
            validator11.MessageTemplate = "message-from-config1-RuleA";
            MockValidatorData validator12 = new MockValidatorData("validator2", true);

            propertyReference1.Validators.Add(validator12);
            validator12.MessageTemplate = "message-from-config2-RuleA";
            MockValidatorData validator13 = new MockValidatorData("validator3", false);

            propertyReference1.Validators.Add(validator13);
            validator13.MessageTemplate = "message-from-config3-RuleA";

            Validator <TestObjectWithFailingAttributesOnProperties> validator
                = ValidationFactory.CreateValidator <TestObjectWithFailingAttributesOnProperties>((IConfigurationSource)configurationSource);

            ValidationResults validationResults = validator.Validate(new TestObjectWithFailingAttributesOnProperties());

            Assert.IsFalse(validationResults.IsValid);
            IDictionary <string, ValidationResult> resultsMapping = ValidationTestHelper.GetResultsMapping(validationResults);

            Assert.AreEqual(4, resultsMapping.Count);
            Assert.IsTrue(resultsMapping.ContainsKey("message-from-config1-RuleA"));
            Assert.IsTrue(resultsMapping.ContainsKey("message-from-config1-RuleA"));
            Assert.IsTrue(resultsMapping.ContainsKey("message1"));
            Assert.IsTrue(resultsMapping.ContainsKey("message2"));
        }
Beispiel #20
0
        private ValidatedTypeReference GetTypeReference(Type type)
        {
            try
            {
                ValidationSettings validationSettings
                    = this.configurationSource.GetSection(ValidationSettings.SectionName) as ValidationSettings;
                if (null == validationSettings)
                {
                    return(null);
                }

                ValidatedTypeReference typeReference = validationSettings.Types.Get(type.FullName);
                return(typeReference);
            }
            catch (ConfigurationErrorsException configurationErrors)
            {
                instrumentationListener.ConfigurationFailure(this, new ValidationConfigurationFailureEventArgs(configurationErrors));
                throw;
            }
        }
Beispiel #21
0
        public void CanCreateValidatorForTypeFromConfigurationWithDefaultRulesetName()
        {
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();
            ValidationSettings            settings            = new ValidationSettings();

            configurationSource.Add(ValidationSettings.SectionName, settings);
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(BaseTestDomainObject));

            settings.Types.Add(typeReference);
            typeReference.DefaultRuleset = "RuleA";
            ValidationRulesetData ruleData = new ValidationRulesetData("RuleA");

            typeReference.Rulesets.Add(ruleData);
            ValidatedPropertyReference propertyReference1 = new ValidatedPropertyReference("Property1");

            ruleData.Properties.Add(propertyReference1);
            MockValidatorData validator11 = new MockValidatorData("validator1", true);

            propertyReference1.Validators.Add(validator11);
            validator11.MessageTemplate = "message-from-config1-RuleA";
            MockValidatorData validator12 = new MockValidatorData("validator2", true);

            propertyReference1.Validators.Add(validator12);
            validator12.MessageTemplate = "message-from-config2-RuleA";
            MockValidatorData validator13 = new MockValidatorData("validator3", false);

            propertyReference1.Validators.Add(validator13);
            validator13.MessageTemplate = "message-from-config3-RuleA";

            Validator <BaseTestDomainObject> validator
                = ValidationFactory.CreateValidatorFromConfiguration <BaseTestDomainObject>((IConfigurationSource)configurationSource);

            ValidationResults validationResults = validator.Validate(new BaseTestDomainObject());

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(2, resultsList.Count);
            Assert.AreEqual("message-from-config1-RuleA", resultsList[0].Message);
            Assert.AreEqual("message-from-config2-RuleA", resultsList[1].Message);
        }
Beispiel #22
0
        private void BuildRules(ValidatedTypeReference typeReference, TypeNode typeNode)
        {
            foreach (RuleSetNode ruleNode in typeNode.Hierarchy.FindNodesByType(typeNode, typeof(RuleSetNode)))
            {
                ValidationRulesetData validationRule = new ValidationRulesetData(ruleNode.Name);

                BuildPropertyReferences(validationRule, ruleNode);
                BuildMethodReferences(validationRule, ruleNode);
                BuildFieldReferences(validationRule, ruleNode);

                SelfNode selfNode = ruleNode.Hierarchy.FindNodeByType(ruleNode, typeof(SelfNode)) as SelfNode;
                if (selfNode != null)
                {
                    foreach (ValidatorData validator in FindValidators(selfNode))
                    {
                        validationRule.Validators.Add(validator);
                    }
                }

                typeReference.Rulesets.Add(validationRule);
            }
        }
        public ValidationSettings Build()
        {
            blockSettings = new ValidationSettings();
            if (!blockSettingsNode.RequirePermission)	// don't set if false
                blockSettings.SectionInformation.RequirePermission = blockSettingsNode.RequirePermission;

            foreach (TypeNode typeNode in blockSettingsNode.Hierarchy.FindNodesByType(typeof(TypeNode)))
            {
                ValidatedTypeReference typeReference = new ValidatedTypeReference();

                typeReference.Name = typeNode.TypeName;
                typeReference.AssemblyName = typeNode.AssemblyName;

                if (typeNode.DefaultRule != null)
                {
                    typeReference.DefaultRuleset = typeNode.DefaultRule.Name;
                }
                BuildRules(typeReference, typeNode);

                blockSettings.Types.Add(typeReference);
            }
            return blockSettings;
        }
Beispiel #24
0
        public void CreateValidatorForDefaultPropertyForExistingTypeInConfigurationRetunsAppropriateValidatorBasedOnRule()
        {
            ValidationSettings settings = new ValidationSettings();

            this.configurationSource.Add(ValidationSettings.SectionName, settings);
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(TestClass));

            settings.Types.Add(typeReference);
            typeReference.DefaultRuleset = "ruleset1";
            ValidationRulesetData ruleData = new ValidationRulesetData("ruleset1");

            typeReference.Rulesets.Add(ruleData);
            ValidatedMethodReference methodReference1 = new ValidatedMethodReference("PublicMethod");

            ruleData.Methods.Add(methodReference1);
            ValidatedMethodReference methodReference2 = new ValidatedMethodReference("NonPublicMethod");

            ruleData.Methods.Add(methodReference2);
            methodReference1.Validators.Add(new MockValidatorData("validator1", false));
            methodReference1.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            methodReference2.Validators.Add(new MockValidatorData("validator2", false));
            methodReference2.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = this.builder.CreateValidator(typeof(TestClass), string.Empty);

            Assert.IsNotNull(validator);
            Assert.AreSame(typeof(AndCompositeValidator), validator.GetType());
            IList <Validator> validators = ValidationTestHelper.CreateListFromEnumerable(((AndCompositeValidator)validator).Validators);

            Assert.AreEqual(1, validators.Count);
            Assert.AreEqual(1, this.mockFactory.requestedMembers.Count);
            ValueAccessValidatorBuilder valueAccessValidatorBuilder1 = this.mockFactory.requestedMembers["TestClass.PublicMethod"];

            Assert.AreSame(valueAccessValidatorBuilder1.BuiltValidator, validators[0]);
            Assert.AreEqual(1, valueAccessValidatorBuilder1.ValueValidators.Count);
            Assert.AreEqual("validator 1 message", ((MockValidator <object>)valueAccessValidatorBuilder1.ValueValidators[0]).MessageTemplate);
        }
        /// <summary>
        /// Retruns a friendly name based on a <see cref="ValidatedTypeReference"/> instance.
        /// </summary>
        /// <param name="typeConfiguration">The <see cref="ValidatedTypeReference"/> that should be used to create a name.</param>
        /// <returns>A friendly name that can be used as a display name.</returns>
        public string CreateName(ValidatedTypeReference typeConfiguration)
        {
            if (typeConfiguration == null) throw new ArgumentNullException("typeConfiguration");

            return base.CreateName(typeConfiguration.Name);
        }
        private void BuildRules(ValidatedTypeReference typeReference, TypeNode typeNode)
        {
            foreach (RuleSetNode ruleNode in typeNode.Hierarchy.FindNodesByType(typeNode, typeof(RuleSetNode)))
            {
                ValidationRulesetData validationRule = new ValidationRulesetData(ruleNode.Name);

                BuildPropertyReferences(validationRule, ruleNode);
                BuildMethodReferences(validationRule, ruleNode);
                BuildFieldReferences(validationRule, ruleNode);

                SelfNode selfNode = ruleNode.Hierarchy.FindNodeByType(ruleNode, typeof(SelfNode)) as SelfNode;
                if (selfNode != null)
                {
                    foreach (ValidatorData validator in FindValidators(selfNode))
                    {
                        validationRule.Validators.Add(validator);
                    }
                }

                typeReference.Rulesets.Add(validationRule);
            }
        }