Пример #1
0
 /// <summary>
 /// Add a <see cref="ValidatableElement"/>.
 /// </summary>
 /// <param name="subValidatableElement">The sub element</param>
 /// <seealso cref="ValidatableElement(System.Type, IClassValidator, IGetter)"/>
 /// <seealso cref="SubElements"/>
 public void AddSubElement(ValidatableElement subValidatableElement)
 {
     if (subValidatableElement.Getter == null)
     {
         throw new ArgumentException("The sub element of ValidatableElement must have a Getter.", "subValidatableElement");
     }
     subElements.Add(subValidatableElement);
 }
Пример #2
0
        /// <summary>
        /// Assert a valid entity.
        /// </summary>
        /// <param name="entity">The entity instance to validate</param>
        /// <exception cref="InvalidStateException">when <paramref name="entity"/> have an invalid state.</exception>
        /// <remarks>
        /// If the <see cref="System.Type"/> of the <paramref name="entity"/> was never inspected, or
        /// it was not configured, the <see cref="IClassValidator"/> will be automatic added to the engine.
        /// </remarks>
        public virtual void AssertValid(object entity)
        {
            if (entity == null)
            {
                return;
            }
            ValidatableElement element = GetElementOrNew(GuessEntityType(entity));

            element.Validator.AssertValid(entity);
        }
Пример #3
0
 internal void AddValidatableElement(ValidatableElement element)
 {
     if (element.HasSubElements || element.Validator.HasValidationRules)
     {
         validators[element.EntityType] = element;
     }
     else
     {
         validators[element.EntityType] = AlwaysValidPlaceHolder;
     }
 }
Пример #4
0
        internal void AddValidator(System.Type entityType, IValidatableSubElementsInspector inspector)
        {
            IClassValidator cv      = GetClassValidator(entityType);
            var             element = new ValidatableElement(entityType, cv);

            if (inspector != null)
            {
                inspector.Inspect(element);
            }
            AddValidatableElement(element);
        }
Пример #5
0
 private static IEnumerable <InvalidValue> ValidateSubElements(ValidatableElement element, object entity, params object[] activeTags)
 {
     if (element != null)
     {
         return(from subElement in element.SubElements
                let component = subElement.Getter.Get(entity)
                                from invalidValue in subElement.Validator.GetInvalidValues(component, activeTags).Concat(ValidateSubElements(subElement, component, activeTags))
                                select invalidValue);
     }
     return(ClassValidator.EmptyInvalidValueArray);
 }
Пример #6
0
        private ValidatableElement GetElementOrNew(System.Type entityType)
        {
            ValidatableElement element;

            if (!validators.TryGetValue(entityType, out element))
            {
                IClassValidator cv = GetClassValidator(entityType);
                element = new ValidatableElement(entityType, cv);
                AddValidatableElement(element);
            }
            return(element);
        }
 public void ValidatableElementTest()
 {
     ClassValidator cv = new ClassValidator(typeof(Address));
     ValidatableElement ve = new ValidatableElement(typeof(Address), cv);
     Assert.AreEqual(ve.EntityType, typeof(Address));
     Assert.IsTrue(ReferenceEquals(cv, ve.Validator));
     Assert.IsNull(ve.Getter);
     Assert.IsFalse(ve.SubElements.GetEnumerator().MoveNext());
     Assert.IsTrue(ve.Equals(new ValidatableElement(typeof(Address), new ClassValidator(typeof(Address)))));
     Assert.IsFalse(ve.Equals(5)); // any other obj
     Assert.AreEqual(ve.GetHashCode(),
                                     (new ValidatableElement(typeof(Address), new ClassValidator(typeof(Address)))).GetHashCode());
 }
 public void SubElements()
 {
     IGetter getter = new BasicPropertyAccessor().GetGetter(typeof(AClass), "Address");
     ClassValidator cvadd = new ClassValidator(typeof(Address));
     ClassValidator cv = new ClassValidator(typeof(AClass));
     ValidatableElement ve = new ValidatableElement(typeof(AClass), cv);
     try
     {
         ve.AddSubElement(new ValidatableElement(typeof(Address), cvadd));
         Assert.Fail("No exception adding a subelement without getter");
     }
     catch (ArgumentException)
     {
         //ok
     }
     Assert.IsFalse(ve.HasSubElements);
     ve.AddSubElement(new ValidatableElement(typeof(Address), cvadd, getter));
     Assert.IsTrue(ve.HasSubElements);
 }
Пример #9
0
        /// <summary>
        /// Use the <see cref="ClassValidator.GetPotentialInvalidValues(string, object)"/> for a given entity instance.
        /// </summary>
        /// <param name="entity">The entity instance to validate</param>
        /// <param name="propertyName">The name of a property</param>
        /// <param name="activeTags">Tags included in the validation.</param>
        /// <returns>All the invalid values.</returns>
        /// <remarks>
        /// If the <see cref="System.Type"/> of the <paramref name="entity"/> was never inspected, or
        /// it was not configured, the <see cref="IClassValidator"/> will be automatic added to the engine.
        /// </remarks>
        public virtual InvalidValue[] ValidatePropertyValue(object entity, string propertyName, params object[] activeTags)
        {
            if (entity == null)
            {
                return(ClassValidator.EmptyInvalidValueArray);
            }

            System.Type entityType = GuessEntityType(entity);

            if (!entityType.ShouldNeedValidation())
            {
                return(ClassValidator.EmptyInvalidValueArray);
            }

            ValidatableElement element = GetElementOrNew(entityType);

            if (activeTags != null && activeTags.Length == 0)
            {
                activeTags = null;
            }
            return(element.Validator.GetInvalidValues(entity, propertyName, activeTags).ToArray());
        }
Пример #10
0
        private IEnumerable <InvalidValue> InternalValidate(object entity, params object[] activeTags)
        {
            if (entity == null)
            {
                return(ClassValidator.EmptyInvalidValueArray);
            }

            System.Type entityType = GuessEntityType(entity);

            if (!entityType.ShouldNeedValidation())
            {
                return(ClassValidator.EmptyInvalidValueArray);
            }

            ValidatableElement element = GetElementOrNew(entityType);

            if (activeTags != null && activeTags.Length == 0)
            {
                activeTags = null;
            }
            return(ValidateSubElements(element, entity, activeTags).Concat(element.Validator.GetInvalidValues(entity, activeTags)));
        }
 /// <summary>
 /// Add a <see cref="ValidatableElement"/>.
 /// </summary>
 /// <param name="subValidatableElement">The sub element</param>
 /// <seealso cref="ValidatableElement(System.Type, IClassValidator, IGetter)"/>
 /// <seealso cref="SubElements"/>
 public void AddSubElement(ValidatableElement subValidatableElement)
 {
     if (subValidatableElement.Getter == null)
         throw new ArgumentException("The sub element of ValidatableElement must have a Getter.", "subValidatableElement");
     subElements.Add(subValidatableElement);
 }
            public void Inspect(ValidatableElement element)
            {
                AddSubElement(clazz.IdentifierProperty, element);

                foreach (Property property in clazz.PropertyIterator)
                {
                    AddSubElement(property, element);
                }
            }
            private static void AddSubElement(Property property, ValidatableElement element)
            {
                if (property != null && property.IsComposite && !property.BackRef)
                {
                    Component component = (Component) property.Value;
                    if (component.IsEmbedded)
                    {
                        return;
                    }

                    if (property.PersistentClass != null)
                    {
                        var cv = Engine.GetClassValidator(property.PersistentClass.MappedClass);

                        if (cv != null)
                        {
                            if (cv.GetMemberConstraints(property.Name).OfType<ValidAttribute>().Any())
                            {
                                // the components is already marked as Valid
                                return;
                            }
                        }
                    }

                    IPropertyAccessor accesor = PropertyAccessorFactory.GetPropertyAccessor(property, EntityMode.Poco);

                    IGetter getter = accesor.GetGetter(element.EntityType, property.Name);

                    IClassValidator validator = Engine.GetClassValidator(getter.ReturnType);
                    if (validator != null)
                    {
                        ValidatableElement subElement = new ValidatableElement(getter.ReturnType, validator, getter);

                        foreach (Property currentProperty in component.PropertyIterator)
                        {
                            AddSubElement(currentProperty, subElement);
                        }

                        if (subElement.HasSubElements || subElement.Validator.HasValidationRules)
                        {
                            element.AddSubElement(subElement);
                        }
                    }
                }
            }
            private static void AddSubElement(Property property, ValidatableElement element)
            {
                if (property != null && property.IsComposite && !property.BackRef)
                {
                    Component component = (Component) property.Value;
                    if (component.IsEmbedded)
                    {
                        return;
                    }

                    IPropertyAccessor accesor = PropertyAccessorFactory.GetPropertyAccessor(property, EntityMode.Poco);

                    IGetter getter = accesor.GetGetter(element.EntityType, property.Name);

                    IClassValidator validator = Engine.GetClassValidator(getter.ReturnType);
                    if (validator != null)
                    {
                        ValidatableElement subElement = new ValidatableElement(getter.ReturnType, validator, getter);

                        foreach (Property currentProperty in component.PropertyIterator)
                        {
                            AddSubElement(currentProperty, subElement);
                        }

                        if (subElement.HasSubElements || subElement.Validator.HasValidationRules)
                        {
                            element.AddSubElement(subElement);
                        }
                    }
                }
            }