如何验证嵌套对象中的空值

huangapple go评论75阅读模式
英文:

How to validate nested objects for Null values

问题

<br/>
我正在开发一个服务,该服务从上游系统接收请求。<br/>

  • 这个请求可能有大约 300 个字段。
  • 我的任务是处理订单,但在处理订单之前,我必须执行数据验证。
  • 数据验证主要涉及空值和空检查。我至少必须检查所有必填字段是否既不为空也不为 null。

订单数据传输对象(DTO):
<br/>

public class Order{
   private String orderId;
   private String sourceOrder;
   private String destinationSystem;
   private String orderingTimeStamp;
   private List&lt;Mapper&gt; properties;
          ....
}

我不想在每个步骤上都进行空值检查,比如
if(sourceOrder!=null &amp;&amp; !sourceOrder.isEmpty()),因为那样我会写很多样板代码。<br/>
因此,我想在处理订单之前,先验证订单的空值和空检查。如果所有必填值都存在,那么就处理订单,否则拒绝订单。

<br/>
我知道有@NotNull 注解,但对于嵌套对象(例如private List&lt;Mapper&gt; properties),它不起作用。在这些元素中,我只想验证所选元素,而不是全部元素。<br/>

<br/>
例如,我只想在List&lt;Mapper&gt; properties中验证"firstName"、"lastName"、"Address",而不是全部验证。
<br/>
有没有办法实现这一点,或者我们有没有设计模式/框架来做到这一点呢?<br/>
请帮忙提供一下建议。

英文:

<br/>
I am working on a service which receives a request from the upstream system.<br/>

  • 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:
<br/>

public class Order{
   private String orderId;
   private String sourceOrder;
   private String destinationSystem;
   private String orderingTimeStamp;
   private List&lt;Mapper&gt; properties;
          ....
}

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

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

<br/>
For example, I have to validate only &quot;firstName&quot;, &quot;lastName&quot;, &quot;Address&quot; in the List&lt;Mapper&gt; properties, not all.
<br/>
Is there any way to achieve it, or do we have any design pattern/framework to do this?<br/>
Please help.

答案1

得分: 1

以下是翻译好的内容:

我会做的是在所有嵌套属性上定义一个 validate 函数。

首先,您要定义一个单独的类,其中包含不同的标志以控制验证行为。

public class OrderValidationOptions {
  // 您的选项
}

然后针对我们的嵌套类型:

public class Mapper {
  // 其他代码

  public boolean validate(OrderValidationOptions options) {
    // 验证逻辑
  }
}

同样地,在 Order 类中也是如此:

public class Order {
  // 其他代码
  private List<Mapper> properties;

  public boolean validate(OrderValidationOptions options) {
    // 其他逻辑
    if (this.properties != null) {
      for (Mapper property : this.properties) {
        property.validate(options);
      }
    }
  }
}
英文:

What I would do is define a validate function on all nested properties.

First, you want to define a seperate class which has the different flags to control validation behaviour.

public class OrderValidationOptions {
  // Your options
}

Then or our nested type:

public class Mapper {
 // Other Code
 
 public boolean validate(OrderValidationOptions options) {
   // validation logic
 }
}

And similarly in the Order class:

public class Order {
 // Other Code
 private List&lt;Mapper&gt; properties;

 public boolean validate(OrderValidationOptions options) {
   // Other logic
   if (this.properties != null) {
     for(Mapper property: this.properties) {
       property.validate(options)
     }
   }
 }
}

答案2

得分: 0

所有的列表(Lists)和集合(Sets)都继承自接口 Collection。我会利用这一点。当您有任何类型的对象 example 时,您可以调用 validate(example); 并且会返回正确的结果。
如果 example 实现了 Collection 接口,将会调用第一个方法,否则会调用第二个方法。

public boolean validate(Collection c) {
   if(c == null) return false;
   if(c.isEmpty()) return false;
   return true;
}

public boolean validate(Object obj) {
   return obj != null;
}
英文:

All Lists and Sets extend the Interface Collection. I make use of that. When you have any Object example of any type, you can call validate(example); and the right result will return.
If example implements Collection, the first method will be called, otherwise the second.

public boolean validate(Collection c) {
   if(c == null) return false;
   if(c.isEmpty()) return false;
   return true;
}

public boolean validate(Object obj) {
   return obj != null;
}

huangapple
  • 本文由 发表于 2020年4月9日 17:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61117840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定