Best practice for validating XML messages

validationxml

Two systems are communicating via XML messages on a message queue and any received message must be validated for structure, content and business logic before being processed.

An XML schema offers validation on structure and content with data type restriction, required fields, choice structures and the like.

But this of course only gets me half way there. In a system I am currently working on, we do the remaining validation by deserializing the message to an object structure and validating context with a dedicated code library.

The problem is that first of all the validation is in two different places and it overlaps a bit since some validation can be done in both ways, and secondly maintaining the validation code can become tedious.

Is there a better way to do this, any best practices or tools out there?

Best Answer

Data types and required fields in the schema define the structure of the XML document, and should in my opinion only validate that the document is in fact a "complete document of type X". If this first validation fails, an attempt to deserialize it could fail.

The second validation could be regarded application or even context specific, errors would mean "your valid document does not contain valid values for the application (at the moment)".

If you share that vision, the two validation steps would never have a functional overlap, perhaps just a technical one.