diff --git a/SpecExpress/src/SpecExpress.Test/Entities/Person.cs b/SpecExpress/src/SpecExpress.Test/Entities/Person.cs new file mode 100644 index 0000000..eb7d15f --- /dev/null +++ b/SpecExpress/src/SpecExpress.Test/Entities/Person.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SpecExpress.Test.Entities +{ + public class Person + { + public string Name { get; set; } + + public Address2 MailingAddress { get; set; } + + public Address2 ShippingAddress { get; set; } + } + + public class Address2 + { + public const string MailingType = "MAIL"; + + public const string ShippingType = "SHIP"; + + public string Type { get; set; } + public string AddressLine { get; set; } + public string City { get; set; } + public string State { get; set; } + } + + public class PersonSpecification : Validates + { + public PersonSpecification() + { + Check(_ => _.Name).Required(); + + Check(_ => _.MailingAddress).Optional() + .Specification(v => v.Check(x => x.Type == Address2.MailingType)) + .And + .Specification(); + + Check(_ => _.ShippingAddress).Optional() + .Specification(v => v.Check(x => x.Type == Address2.ShippingType)) + .And + .Specification(); + } + } + + public class Address2Specification : Validates + { + public Address2Specification() + { + Check(_ => _.AddressLine).Required(); + Check(_ => _.City).Required(); + Check(_ => _.State).Required(); + } + } +} diff --git a/SpecExpress/src/SpecExpress.Test/PersonSpecificationTests.cs b/SpecExpress/src/SpecExpress.Test/PersonSpecificationTests.cs new file mode 100644 index 0000000..3fd5d0a --- /dev/null +++ b/SpecExpress/src/SpecExpress.Test/PersonSpecificationTests.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using SpecExpress.Test.Entities; + +namespace SpecExpress.Test +{ + [TestFixture] + public class PersonSpecificationTests + { + [TestFixtureSetUp] + public void FixtureSetup() + { + ValidationCatalog.Reset(); + ValidationCatalog.Scan(x => x.AddAssembly(typeof(Person).Assembly)); + ValidationCatalog.AssertConfigurationIsValid(); + } + + [TestFixtureTearDown] + public void FixtureTeardown() + { + ValidationCatalog.Reset(); + } + + [Test] + public void Valid_Person() + { + var person = new Person() + { + Name = "Someone", + MailingAddress = + new Address2() + { + AddressLine = "Dallas Pkwy", + City = "Dallas", + State = "TX", + Type = Address2.MailingType + }, + ShippingAddress = + new Address2() + { + AddressLine = "Dallas Pkwy", + City = "Dallas", + State = "TX", + Type = Address2.ShippingType + }, + }; + + var results = ValidationCatalog.Validate(person); + + Assert.That(results.IsValid, Is.True); + + CollectionAssert.IsEmpty(results.All()); + } + + [Test] + + public void Invalid_Person() + { + var person = new Person() + { + Name = "Someone", + MailingAddress = + new Address2() + { + AddressLine = "Dallas Pkwy", + City = "Dallas", + State = "TX", + }, + ShippingAddress = + new Address2() + { + //AddressLine = "Dallas Pkwy", + //City = "Dallas", + //State = "TX", + Type = Address2.ShippingType + }, + }; + + var results = ValidationCatalog.Validate(person); + + foreach (var result in results.All()) + { + Console.WriteLine(result.Message); + } + + Assert.That(results.IsValid, Is.False); + + CollectionAssert.IsNotEmpty(results.All()); + + + + } + } +} diff --git a/SpecExpress/src/SpecExpress.Test/SpecExpress.Test.csproj b/SpecExpress/src/SpecExpress.Test/SpecExpress.Test.csproj index bb2058a..88043cb 100644 --- a/SpecExpress/src/SpecExpress.Test/SpecExpress.Test.csproj +++ b/SpecExpress/src/SpecExpress.Test/SpecExpress.Test.csproj @@ -91,8 +91,10 @@ + + diff --git a/SpecExpress/src/SpecExpress/Util/RuleValidatorContext.cs b/SpecExpress/src/SpecExpress/Util/RuleValidatorContext.cs index d0631d4..912ee7a 100644 --- a/SpecExpress/src/SpecExpress/Util/RuleValidatorContext.cs +++ b/SpecExpress/src/SpecExpress/Util/RuleValidatorContext.cs @@ -93,6 +93,12 @@ private static PropertyInfo GetValidationProperty(PropertyValidator validator, T _propertyBag.Add(instanceType, new Dictionary()); } var propertyBag = _propertyBag[instanceType]; + + if (string.IsNullOrEmpty(validator.PropertyName)) + { + return null; + } + if (!propertyBag.ContainsKey(validator.PropertyName)) { propertyBag.Add(validator.PropertyName, instanceType.GetProperty(validator.PropertyName)); @@ -176,6 +182,12 @@ private static PropertyInfo GetValidationProperty(PropertyValidator validator, T _propertyBag.Add(instanceType, new Dictionary()); } var propertyBag = _propertyBag[instanceType]; + + if (string.IsNullOrEmpty(validator.PropertyName)) + { + return null; + } + if (!propertyBag.ContainsKey(validator.PropertyName)) { propertyBag.Add(validator.PropertyName, instanceType.GetProperty(validator.PropertyName));