8

I want a conditional transformation where I need to add a property in output if the value of a specific field in input matches my condition. Below is my input and output required.


Input

{
  "attr": [
    {
      "name": "first",
      "validations": [
        {
          "type": "Required",
          "value": true
        }
      ]
    },
    {
      "name": "last",
      "validations": [
        {
          "type": "lenght",
          "value": "10"
        }
      ]
    },
    {
      "name": "email",
      "validations": [
        {
          "type": "min",
          "value": 10
        }
      ]
    }
  ]
}

Output

{
  "out": [
    {
      "name": "first",
      "required": "yes"
    },
    {
      "name": "last"
    },
    {
      "name": "email"
    }
  ]
}

So I am able to get till the condition, but inside condition, & and @ are being respective to the input rather than to the output. Can anybody help me out with the transformation? Below is the spec I have written so far.

[
  {
    "operation": "shift",
    "spec": {
      "attr": {
        "*": {
          "name": "out.&1.name",
          "validations": {
            "*": {
              "type": {
                "Required": {
                  "@(2,value)": "out.&1.req"
                }
              }
            }
          }
        }
      }
    }
  }
]
Mohammadreza Khedri
  • 2,523
  • 1
  • 11
  • 22
yeswanth
  • 1,543
  • 1
  • 11
  • 19

1 Answers1

3

This spec does the transform.

[
  {
    "operation": "shift",
    "spec": {
      "attr": {
        "*": {
          "name": "out[&1].name",
          "validations": {
            "*": {
              "type": {
                "Required": {
                  "#yes": "out[&5].required"
                }
              }
            }
          }
        }
      }
    }
  }
]

However, I think you meant to grab the "value" : true that is a sibling of the "Required" : true, rather than have the output be "yes".

If so swap in this bit.

"Required": {
 "@(2,value)": "out[&5].required"
}
mido
  • 24,198
  • 15
  • 92
  • 117
Milo S
  • 4,466
  • 1
  • 19
  • 22
  • Thanks a ton. It worked. But I am not able to understand the usage of &5. Can you please explain from where do we need to traverse the tree from? – yeswanth Apr 22 '16 at 10:56
  • @Milo S - Can you pls explain the usage of &5, I am still not able to get it. – Dwarrior Sep 15 '21 at 00:04