title: Which Design Pattern To Use For Validation
body: <p>I am sure I am not the only one who's facing this problem but I am unable to find a solution till now. The problem is as below. </p>
  
  <p>I have published a web service (on which I don't have any control and I cannot change it) from a legacy system and all the clients are using it. I receive requests from the web service and object which i get is a very complex one but for the sake of an example lets say I receive object A from the web service call which contains several other objects like Object B, Object C in it etc and in addition objects B &amp; C also have some primitive data types as well as some other objects in them. My problem is that I want to validate the whole Object A (all the including objects and sub objects), which design pattern is recommended here? Secondly, the main point here is that I can get different types of Object A from the web service. What i really mean by different types of object A is that the Object A depends upon the client i.e. some clients will send Object A without filling the data in the containing Object B or even they can fill Object B partially and then send it in Object A. So I have to validate the Object A based on the client (as some clients will require containing Object B some will not, some will require few elements in Object B etc). So in other words I will have a place where I will also store the validation rules for each client which will tell me something like this that client ABC wants field abc in Object B to be of type string and the maximum length is 25 chars and it is mandatory to have data in that field.</p>
  
  <p>Some validations which I would like to perform is</p>
  
  <p>Checking fields of some object (say Object B) for data types, length of a particular field, is the field required for this client or is it optional etc etc...</p>
  
  <p>Any concrete working example will be extremely useful.</p>
  
  <p>The structure of the Object A for this particular example is as follows.</p>
  
  <pre><code>    public class A
      {
          private B objectB;
          private C objectC;
          // and so on
      }
  
      public class B{
          private E objectE;
          private String name;
          private int age;
          // and so on
      } 
  
      public class C
      {
          private F objectF;
          private String address;
          private String country;
      }
  
      public class E
      {
          // Members here
      }
  
      public class F
      {
          // Members here
      }
  </code></pre>
  
  <p>P.S: I have given arbitrary names to the classes and the members just for the general understanding. O yes I forgot to mention that I am using java here but it doesn't really matter as the design principles or patterns can be applied to any language. Hoping to hear from you guys soon.. :)</p>