如何将错误添加到有效载荷中,因为几个键的值在数据编织的有效载荷中为空?

How to add Error to payload for several key's value is null in payload in dataweave?

我有一个有效载荷,我需要检查一些特定的验证,如果验证不成功,我想向有效载荷中的新数组添加一个错误。

输入:

[
  {
    "order": "123",
    "product": "",
    "invoice": "Lock"
    },
   {
    "order": "123",
    "product": "456",
    "invoice": ""
    }
    ]

在上面的输入中,我需要检查 Invoice == 'Locked' 和 Product != null,需要根据不同的 json 数组检查订单以查看该值是否存在,但我只是想了解发票和产品如何获得不同的验证并将错误添加到错误数组..

预期输出应为:

[
  {
    "order": "123",
    "product": "",
    "invoice": "Lock",
    "Errors" : {
      "Error" : true,
      "Error Message" : "Invoice is not "Locked", product is null"
      }
    },
   {
    "order": "123",
    "product": "456",
    "invoice": "123",
    "Errors" : {
      "Error" : false,
      "Error Message" : ""
      }
    }
    ]

我希望能够检查不同密钥的不同验证。

我能够实现以下输出,其中我为每个单独的键使用不同的函数并为每个键添加错误但是过滤出发生的错误变得非常具有挑战性?

[
  {
    "order": "123",
    "product": "",
    "invoice": "",
    "Errors": {
      "Order ": "Order is NULL,",
      "Product": "",
      "Invoice": ""
      }
    },
   {
    "order": "123",
    "product": "456",
    "invoice": "123",
    "Errors": {
      "Order ": "",
      "Product": "",
      "Invoice": ""
      }
    }
    ]

即使我能从上面的输出中找出其中一个对象有错误,那也能达到目的。

如何使用 Errors 数组和 {Error, error message} 获得上面显示的所需输出?

你可以试试这个脚本 -

%dw 2.0
output application/json
import someEntry from dw::core::Objects
fun maperror(item) = do {
    var error =  item someEntry (value, key) -> isEmpty(value)
    ---
    {
    "Error" : error,
    "Error Message" : if (error) keysOf((item filterObject ((value, key, index) -> isEmpty(value)))) map ($ ++ " is null") joinBy ", " else ""
    }
}
---
payload map {
    ($),
    "Errors": maperror($)
}

它将产生如下输出 -

[
  {
    "order": "123",
    "product": "456",
    "invoice": "",
    "Errors": {
      "Error": true,
      "Error Message": "invoice is null"
    }
  },
  {
    "order": "123",
    "product": "",
    "invoice": "",
    "Errors": {
      "Error": true,
      "Error Message": "product is null, invoice is null"
    }
  }
]

您可以尝试使用如下DataWeave表达式:

%dw 2.0
output application/json

fun addMessage(condition, message) = if (condition) message else null

fun validate(item) = 
    []
    << addMessage(isEmpty(item.order), "Order is null or empty")
    << addMessage(isEmpty(item.product), "Product is null or empty")
    << addMessage(isEmpty(item.invoice), "Invoice is null or empty")
    << addMessage(item.invoice ~= "Lock", "Invoice is locked")
    // keep adding other validation here

fun addError(messages) =
   {
       "Errors" : {
            "Error": !isEmpty(messages),
            "Error Message": ((messages filter (item) -> (item != null)) as Array) joinBy ", "
       }
   }

---
payload map (item, index) -> 
    item 
    ++  addError(validate(item))

它将根据提供的示例负载生成以下输出:

[
  {
    "order": "123",
    "product": "",
    "invoice": "Lock",
    "Errors": {
      "Error": true,
      "Error Message": "Product is null or empty, Invoice is locked"
    }
  },
  {
    "order": "123",
    "product": "456",
    "invoice": "",
    "Errors": {
      "Error": true,
      "Error Message": "Invoice is null or empty"
    }
  }
]