jeudi 9 avril 2020

How to validate nested objects for Null values


I am working on a service which receives a request from the upstream system.

  • The request can have about 300 fields.
  • My task is to process the order, but before processing the order, I have to perform data validation.
  • Data validation mainly involves null & empty checks. I have to check at-least whether all the mandatory fields are neither empty nor null.

Order DTO:

public class Order{
   private String orderId;
   private String sourceOrder;
   private String destinationSystem;
   private String orderingTimeStamp;
   private List<Mapper> properties;
          ....
}



I don't want to check null on each step, like if(sourceOrder!=null && !sourceOrder.isEmpty()), as I would end up writing a lot of boilerplate code.
So, I thought before processing the order, let's validate the order for null & empty checks. If all mandatory values are present, then process the order else reject the order.


I am aware of @NotNull annotation, but that won't work for nested objects for example private List<Mapper> properties, will have a list of objects. Out of those elements I want to validate only selected and not all.



Is there any way to achieve it, or do we have any design pattern/framework to do this?
Please help.

Aucun commentaire:

Enregistrer un commentaire