public void CanCreateValidatorForValidatorAndValidationAttributes()
        {
            var validator =
                PropertyValidationFactory.GetPropertyValidator(
                    typeof(TestObjectWithMultipleSourceValidationAttributesOnProperties),
                    StaticReflection.GetPropertyInfo((TestObjectWithMultipleSourceValidationAttributesOnProperties t)
                                                     => t.PropertyWithMixedAttributes),
                    "",
                    ValidationSpecificationSource.DataAnnotations | ValidationSpecificationSource.Attributes,
                    new MemberAccessValidatorBuilderFactory());

            var invalidResults =
                validator.Validate(
                    new TestObjectWithMultipleSourceValidationAttributesOnProperties
            {
                PropertyWithMixedAttributes = "invalid"
            });

            Assert.IsFalse(invalidResults.IsValid);
            Assert.AreEqual(2, invalidResults.Count);
            Assert.IsTrue(
                invalidResults.Any(vr =>
                                   vr.Key == "PropertyWithMixedAttributes" && vr.Message == "data annotations-mixed"));
            Assert.IsTrue(
                invalidResults.Any(vr =>
                                   vr.Key == "PropertyWithMixedAttributes" && vr.Message == "vab-mixed"));
        }
 public void InstanceConfiguredWithNullPropertyNameThrows()
 {
     PropertyValidationFactory.GetPropertyValidator(typeof(TestObjectWithFailingAttributesOnProperties),
                                                    null,
                                                    "",
                                                    ValidationSpecificationSource.Attributes,
                                                    new MemberAccessValidatorBuilderFactory());
 }
Exemplo n.º 3
0
 /// <summary>
 /// Returns the <see cref="Validator"/> represented by the configuration in the <see cref="IValidationIntegrationProxy"/>, linked
 /// with the integration scenario as necessary.
 /// </summary>
 public Validator GetValidator()
 {
     return(PropertyValidationFactory.GetPropertyValidator(validatedType,
                                                           validatedProperty,
                                                           integrationProxy.Ruleset,
                                                           integrationProxy.SpecificationSource,
                                                           integrationProxy.GetMemberValueAccessBuilder()));
 }
Exemplo n.º 4
0
        private Validator GetPropertyValidator(Type entityType, PropertyInfo property)
        {
            string ruleset = string.Empty;
            var    source  = ValidationSpecificationSource.All;
            var    builder = new ReflectionMemberValueAccessBuilder();

            return(PropertyValidationFactory.GetPropertyValidator(entityType, property, ruleset, source, builder));
        }
        public void RequestForValidatorBasedOnAttributesReturnsNullForNonExistingRuleName()
        {
            Validator validator
                = PropertyValidationFactory.GetPropertyValidatorFromAttributes(typeof(TestObjectWithFailingAttributesOnProperties),
                                                                               typeof(TestObjectWithFailingAttributesOnProperties).GetProperty("FailingProperty1"), "InvalidRule",
                                                                               new MemberAccessValidatorBuilderFactory());

            Assert.IsNull(validator);
        }
        public void RequestForValidatorBasedOnAttributesAndConfigurationWithRulesetThroughStaticFacadeReturnsNullIfNoValidationInfomationExistsForProperty()
        {
            Validator validator
                = PropertyValidationFactory.GetPropertyValidator(typeof(TestObjectWithFailingAttributesOnProperties),
                                                                 typeof(TestObjectWithFailingAttributesOnProperties).GetProperty("PropertyWithoutAttributes"),
                                                                 "RuleA",
                                                                 ValidationSpecificationSource.Both,
                                                                 new MemberAccessValidatorBuilderFactory());

            Assert.IsNull(validator);
        }
Exemplo n.º 7
0
        private Validator CreateValidator(PropertyInfo validatedProperty)
        {
            var validator =
                PropertyValidationFactory.GetPropertyValidator(
                    validatedProperty.ReflectedType,
                    validatedProperty,
                    this.RulesetName ?? string.Empty,
                    this.ValidationSpecificationSource,
                    new FixedPropertyMemberValueAccessBuilder(validatedProperty));

            return(validator);
        }
        public void RequestForValidatorBasedOnAttributesWithRulesetReturnsAppropriateValidator()
        {
            Validator validator
                = PropertyValidationFactory.GetPropertyValidatorFromAttributes(typeof(TestObjectWithFailingAttributesOnProperties),
                                                                               typeof(TestObjectWithFailingAttributesOnProperties).GetProperty("FailingProperty1"), "RuleA",
                                                                               new MemberAccessValidatorBuilderFactory());

            TestObjectWithFailingAttributesOnProperties objectToTest = new TestObjectWithFailingAttributesOnProperties();

            objectToTest.FailingProperty1 = "property value";
            ValidationResults validationResults = validator.Validate(objectToTest);

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

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("message1-RuleA", resultsList[0].Message);
        }
        public void RequestForValidatorBasedOnAttributesAndConfigurationWithRulesetThroughStaticFacadeReturnsAppropriateValidator()
        {
            Validator validator
                = PropertyValidationFactory.GetPropertyValidator(typeof(TestObjectWithFailingAttributesOnProperties),
                                                                 typeof(TestObjectWithFailingAttributesOnProperties).GetProperty("FailingProperty1"),
                                                                 "RuleA",
                                                                 ValidationSpecificationSource.Both,
                                                                 new MemberAccessValidatorBuilderFactory());

            TestObjectWithFailingAttributesOnProperties objectToTest = new TestObjectWithFailingAttributesOnProperties();

            objectToTest.FailingProperty1 = "property value";
            ValidationResults validationResults = validator.Validate(objectToTest);

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

            Assert.AreEqual(3, resultsMapping.Count);
            Assert.IsTrue(resultsMapping.ContainsKey("message-from-config1-RuleA"));
            Assert.IsTrue(resultsMapping.ContainsKey("message-from-config2-RuleA"));
            Assert.IsTrue(resultsMapping.ContainsKey("message1-RuleA"));
        }
Exemplo n.º 10
0
    private void PopulateValidators()
    {
        var properties = GetType().GetProperties(
            BindingFlags.Instance |
            BindingFlags.Public);

        foreach (var property in properties)
        {
            var attributes = property.GetCustomAttributes(
                typeof(ValueValidatorAttribute),
                false);
            if (attributes.Length == 0 || _properties.Contains(property.Name))
            {
                continue;
            }
            _properties.Add(property);
            _propertyValidators[property.Name] =
                PropertyValidationFactory.GetPropertyValidatorFromAttributes(
                    property.PropertyType,
                    property,
                    string.Empty,
                    new MemberAccessValidatorBuilderFactory());
        }
    }
 public static void ResetCaches()
 {
     CreatedValidators.Clear();
     ValidationFactory.ResetCaches();
     PropertyValidationFactory.ResetCaches();
 }