public void Inspect(ValidatableElement element) { AddSubElement(clazz.IdentifierProperty, element); foreach (Property property in clazz.PropertyIterator) { AddSubElement(property, 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()); }
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); } } } }
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); }