2

Request Json Looks like the below :-

{ 
   "eNumber": 8506493,   
   "details": [
     {
      "id":12345,
      "name": xyz123
     }
  ]
}

As part of requirement, I need to check the "details" array that either "id" or "name" field must present. if "id" field is present, "name" is non-mandatory. if "name" field is present, "id" is non-mandatory. Throws error if not met.

I tried few options using filter the details array and check the size of the filtered array in the validation component. It does not seems to be working well. if anyone has got better solutions.please share it here.

Infinity
  • 484
  • 2
  • 8
  • 21

2 Answers2

7

This example code will return true or false if it passes your condition

%dw 2.0
import * from dw::core::Arrays
output application/json
---
payload.details every ((item) -> item.id? or item.name?)

The function I'm using is every that checks that all elements in an array passes the given criteria.

Later you can use a choice and a use the raise error or you can use the fail dw function with an if else.

machaval
  • 4,969
  • 14
  • 20
0

You can restrict it at RAML level. Sample RAML -

#%RAML 1.0
title: api

types:
  test1:
      type: object
      properties:
        id: 
         type: string
         example: 123a
  test2:
    type: object
    properties:
      name: 
        type: string
        example: xyz124 
      
  test3:
      type: object
      properties:
        enumber: 
          type: string
          example: 8506493a
        details : 
         type: [test1 | test2]
/test:
  post:
    body:
      application/json:
        type: test3
NewBie
  • 23
  • 1
  • 5