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

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

How to validate nested objects for Null values

问题

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

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

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

  1. public class Order{
  2. private String orderId;
  3. private String sourceOrder;
  4. private String destinationSystem;
  5. private String orderingTimeStamp;
  6. private List&lt;Mapper&gt; properties;
  7. ....
  8. }

我不想在每个步骤上都进行空值检查,比如
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/>

  1. public class Order{
  2. private String orderId;
  3. private String sourceOrder;
  4. private String destinationSystem;
  5. private String orderingTimeStamp;
  6. private List&lt;Mapper&gt; properties;
  7. ....
  8. }

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 函数。

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

  1. public class OrderValidationOptions {
  2. // 您的选项
  3. }

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

  1. public class Mapper {
  2. // 其他代码
  3. public boolean validate(OrderValidationOptions options) {
  4. // 验证逻辑
  5. }
  6. }

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

  1. public class Order {
  2. // 其他代码
  3. private List<Mapper> properties;
  4. public boolean validate(OrderValidationOptions options) {
  5. // 其他逻辑
  6. if (this.properties != null) {
  7. for (Mapper property : this.properties) {
  8. property.validate(options);
  9. }
  10. }
  11. }
  12. }
英文:

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.

  1. public class OrderValidationOptions {
  2. // Your options
  3. }

Then or our nested type:

  1. public class Mapper {
  2. // Other Code
  3. public boolean validate(OrderValidationOptions options) {
  4. // validation logic
  5. }
  6. }

And similarly in the Order class:

  1. public class Order {
  2. // Other Code
  3. private List&lt;Mapper&gt; properties;
  4. public boolean validate(OrderValidationOptions options) {
  5. // Other logic
  6. if (this.properties != null) {
  7. for(Mapper property: this.properties) {
  8. property.validate(options)
  9. }
  10. }
  11. }
  12. }

答案2

得分: 0

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

  1. public boolean validate(Collection c) {
  2. if(c == null) return false;
  3. if(c.isEmpty()) return false;
  4. return true;
  5. }
  6. public boolean validate(Object obj) {
  7. return obj != null;
  8. }
英文:

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.

  1. public boolean validate(Collection c) {
  2. if(c == null) return false;
  3. if(c.isEmpty()) return false;
  4. return true;
  5. }
  6. public boolean validate(Object obj) {
  7. return obj != null;
  8. }

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:

确定