ObjectValidator which object failed when root has multiple of same type

Topics: Validation Application Block
Jul 7, 2008 at 5:05 PM
Here's my situation:
I have a root object, "ProtectionOrder" with multiple properties of type "Person" (Respondent, Petitioner).  Person is a complex type which contains simple properties like Age (int) and complex ones like Name (PersonName), Address, etc.  You can argue with me on why these are so nested but that is not changing :)  This system is for law enforcement so the data requirements are stringent and very granular.  Attempts to change them will result in confinement!

I configured the validation block to validate my root object ProtectionOrder and on the Respondent and Petitioner properties I used object validators to point to the person rule set.  On the person object Name property I used an object validator to point to the person name rule set.  PersonName contains First, Last, Middle - all string properties that can be validated.

This is all working perfectly (and it was so easy to set up...great job on the API!).  I can pass in my protection order and all the nested types & properties will validate.  The issue I'm having is figuring out which one failed.  Because I'm reusing rule sets, if the Name of the Respondent fails there is no intuitive way for me to figure out it was the respondent's name and not the petitioners name.

Here's a sample of the structure in a tree like format:
ProtectionOrder
    Respondent (Person)
        Name (PersonName)
            First: Chris
            Last: This one is too long and will fail validation
    Petitioner (Person)
        Name (PersonName)
            First: Christy
            Last: Deweese

As you can see, Respondent.Name.Last will fail.  But using the EL 4 validation block there is not an easy way to figure out it was the respondent last name.  I tried using the tags/messages but per the documentation those are overridden.  I do not want to duplicate rule sets because that would defeat the purpose of having my validation centralized and shared via config.

I did set the message to display the value that failed.  However due to data security/privacy requirements I may not be able to do that long term.  This particular validation is used in the context of a service so there is no UI.  The service is also queued so there is no client to notify directly.  The only recourse is to log the error which may present an issue with the security requirements.

All in all this is a small price to pay for using this helpful utility.

If you have any suggestions on what I might to do help me identify which nested property triggered the failure that would be great.

Thanks,
Chris
Jul 7, 2008 at 6:32 PM

Hi Chris,

You could benefit from the ValidationResult.Target property, although it's no replacement for some kind of "validation path" that does not exist today and it requires some object traversing to be useful. In your example, the value for the Target property would be the Respondent's Name (a instance of the PersonName type).

Creating a new ObjectValidator that sets the tag property could also help, although it wouldn't support more than one level unless to make it so the tag somehow encodes level information.

Regards
Fernando


cdeweese wrote:
Here's my situation:
I have a root object, "ProtectionOrder" with multiple properties of type "Person" (Respondent, Petitioner).  Person is a complex type which contains simple properties like Age (int) and complex ones like Name (PersonName), Address, etc.  You can argue with me on why these are so nested but that is not changing :)  This system is for law enforcement so the data requirements are stringent and very granular.  Attempts to change them will result in confinement!

I configured the validation block to validate my root object ProtectionOrder and on the Respondent and Petitioner properties I used object validators to point to the person rule set.  On the person object Name property I used an object validator to point to the person name rule set.  PersonName contains First, Last, Middle - all string properties that can be validated.

This is all working perfectly (and it was so easy to set up...great job on the API!).  I can pass in my protection order and all the nested types & properties will validate.  The issue I'm having is figuring out which one failed.  Because I'm reusing rule sets, if the Name of the Respondent fails there is no intuitive way for me to figure out it was the respondent's name and not the petitioners name.

Here's a sample of the structure in a tree like format:
ProtectionOrder
    Respondent (Person)
        Name (PersonName)
            First: Chris
            Last: This one is too long and will fail validation
    Petitioner (Person)
        Name (PersonName)
            First: Christy
            Last: Deweese

As you can see, Respondent.Name.Last will fail.  But using the EL 4 validation block there is not an easy way to figure out it was the respondent last name.  I tried using the tags/messages but per the documentation those are overridden.  I do not want to duplicate rule sets because that would defeat the purpose of having my validation centralized and shared via config.

I did set the message to display the value that failed.  However due to data security/privacy requirements I may not be able to do that long term.  This particular validation is used in the context of a service so there is no UI.  The service is also queued so there is no client to notify directly.  The only recourse is to log the error which may present an issue with the security requirements.

All in all this is a small price to pay for using this helpful utility.

If you have any suggestions on what I might to do help me identify which nested property triggered the failure that would be great.

Thanks,
Chris


Jul 7, 2008 at 7:31 PM
Thanks Fernando.  I think I'll try and push logging the validation failures before I dig too much further.