Validating a single property within a heirarchy

Oct 5, 2013 at 6:37 AM
Hi,

I've been working on some in-depth integration between Fluent Validation and Silverlight for my team at work. We're pretty keen on completely decoupling validation from our models, which has lead to some interesting challenges.

The approach we've taken would ideally validate a single property nested however far deep into the object graph. At this point I'm not even tackling collections.

Based on your test suite from ComplexValidationTester, I've put this together:
[Test]
        public void Explicitly_included_properties_should_be_propogated_to_nested_validators() {
            var results = validator.Validate(person, x => x.Address.Country);
            results.Errors.Count.ShouldEqual(1);
            results.Errors.Single().PropertyName.ShouldEqual("Address.Country.Name");
        }
I've tried a few fixes, but my lack of understanding of the internals have lead me to break more than fix anything.

Could you offer any insight on this?

Regards,
Steven
Coordinator
Oct 7, 2013 at 4:12 PM
Hi Steven

This happens because the default MemberNameValidatorSelector only knows about 'top level' property names (for example, x => x.Address).

If you specify x.Address.Name then it looks for a property on Person called "Address.Name" rather than one called "Address" and then a sub-property called "Name".

The MemberNameValidatorSelector's CanExecute method would need to be modified to perform a better check, which can take into account a full 'path' of a property, rather than just treating it as a name.

If you want to create an issue for this on the Issues page, then I'll try and have a look at doing this when I get some time (but it might not be for a while I'm afraid). If you want to have a go at fixing it yourself, then please feel free - I think the MemberNameValidatorSelector would be the only class you'd need to change.

Jeremy
Oct 8, 2013 at 1:47 AM
Thanks for the advice. I'll see what I can throw together.