Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

mule - 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, 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..

Expected output should be :

[
  {
    "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?

[
  {
    "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.

How do I get the desired output shown above with Errors array with {Error, error message}?

question from:https://stackoverflow.com/questions/65878229/how-to-add-error-to-payload-for-several-keys-value-is-null-in-payload-in-datawe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try to use the following DataWeave expression:

%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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...