简体   繁体   English

如何在 dataweave 的有效负载中为多个键的值添加错误是 null?

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

I have a payload where I need to check for a few specific validation and if validation doesnt succeed I want to add an error to a new array in the payload.我有一个有效负载,我需要检查一些特定的验证,如果验证不成功,我想向有效负载中的新数组添加一个错误。

Input:输入:

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

In above input I need to check if Invoice == 'Locked' and Product,= null.在上面的输入中,我需要检查 Invoice == 'Locked' 和 Product,= null。 order needs to be checked against a different json array to see if that value exist but I just want to get an idea on invoice and product o how to get different validation and add errors to error array..需要针对不同的 json 数组检查订单,以查看该值是否存在,但我只想了解发票和产品 o 如何获得不同的验证并将错误添加到错误数组。

Expected output should be:预期的 output 应该是:

[
  {
    "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" : ""
      }
    }
    ]

I want to be able to check for different validations for different keys.我希望能够检查不同键的不同验证。

I am able to achieve below output, where I am using a different function for each individual key and adding error for each keys but it is becoming very challenging to filter out which error has occurred?我能够实现低于 output,其中我为每个单独的键使用不同的 function 并为每个键添加错误,但是过滤出哪个错误变得非常具有挑战性?

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

Even if I can figure out from above output which one of the objects has errors in it, that will server the purpose.即使我可以从上面 output 中找出其中一个对象有错误,这将达到目的。

How do I get the desired output shown above with Errors array with {Error, error message} ?如何使用带有{Error, error message}Errors数组获得所需的 output ?

You can try to use the following DataWeave expression:您可以尝试使用以下 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))

It will produce the following output based on the provided example payload:它将根据提供的示例有效负载生成以下 output:

[
  {
    "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"
    }
  }
]

You can try with this script -您可以尝试使用此脚本 -

 %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($) }

It will produce output like below -它将产生如下所示的 output -

 [ { "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" } } ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM