Validation by matching 2 json and the route vi if

HI

I don’t know if this is possible to achieve or even if it understandable, I have tried a great number of different solutions, but I fell entirely lost in this case. I have tried with if nodes etc which i have attached but most of it is faulty

I am trying to make a quite complex routing by having an incoming that should get validated against another json and then be routed via the if node

“Example1”

I want the incoming json that contains an array of a number of subitems get validated as following

Example 1
“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“euqipment"id”=" “[policynode].[equipment.id]”
and
“count” “contextData”.“euqipment"id” >= “[policynode].[instances]”

If all conditions are met then it is “true”

“Example2”

I want to validate if a number value in the inEvent is eqaul or grater than the “policyData”

“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“enviroment”.“winds”.“speed”>= “[policynode].[wind"speed]”

“Example3”
Same as exmaple 2 but with other proporties
“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“enviroment”.“current”.“speed”>= "[policynode].(value2)

“exmaple” 4
“inEvent”.“topic”.“Id”= “[policynode].[topic.id]”
and
“inEvent”.“location”.“Id”= “[policynode].[location.id]”
and
“contextData”.“euqipment”.“id”=" “[policynode].[equipment.id]”
and
[
where instance with 1 id 1 is eual to the instance with id 1 in the policy

]

Sadly do your nodes not work. So I tried to repair it and get them working. Please make always first sure that your example workflows really work, that we do not have to spend time on debugging them first. Thanks!

Also does your example not seem to be complete as I can not find any “euqipment” or “equipment” anyway so only post here now an example workflow with IF for “Example 1” and only the first two conditions as that are the only ones that exist. Hope it is helpful anyway as also the other ones would work exactly the same:

Hi Jan

Thanks for you answer, i did my self get that far with the topic id and the ship id but the challenge comes with the calculating policy

The thing is that I need more complex validation by having a formula added to the policy node.

In this I example I would like to add a formula that gets calculated where it formula counts how many instances there active and match them against a value and if they are eg = 2 or more then it is true.

input data

.

trail of policy

{
"topic":{
"id": "exampleEvent1"
},
"locationdata":{
  "id": "Location1"
},
"validationdata":{
  "id": "",
  "policy": "and'$['inEvent'].json['contextData'].['eqipment'].['id']'equipment1','$['inEvent'].json['contextData'].['eqipment'].['intances']count???.....[state]=active > 2",
  "messege":{
  "true": "'Accepted'.concat($[inEvent].json[contextData].[equipment].[instances])",
  "false": "'Not accepted'.concat($[inEvent].json[contextData].[equipment].[instances])"
  }
}
},

I know my nodes are not right but I am a bit lost and don’t even know if it is possible that why I added some count ??? in the policy node.

I also added 4 different examples of calculations I would need to make.

I will try to explain it a bit better.

I want just like you make it with the criteria’s topic.id and location.id is matched in the IF node and that is not the challenge. The challenge comes with the 3rd criteria that is stored in the policy node and which is based on a formula

Here is the example

In exmaple 1 shall the If formula be true if $inEvent.topic.id = $policy.topic.id
and $inEvent.location.id = $policy.location.id then i would like a 3rd cirteria based
on a fomrula in in $policy.policy that contains a formula that calculates
if $inEvenet.contextdata.equipment.id = “a statement stored in the policy node”
and that calculates if there are $inData.contextData.equipment.instances where stete=Active
is 2 or more.

{
"topic":{
"id": "exampleEvent1"
 },
"locationdata":{
  "id": "Location1"
},
"contextData":{
  "equipment": 
  {
    "id": "equipment1",
    "instances":[
    {
      "id": "i1",
      "state": "active"
    },
    {
      "id": "i2",
      "state": "inactive"
    },
    {
      "id": "i3",
      "state": "active"
    }
    ]
  }

  
}
}

In exmaple 2 shall the If formula be true if $inEvent.topic.id = $policy.topic.id
and $inEvent.location.id = $policy.location.id then i would like a 3rd cirteria based
on a fomrula in in $policy.policy that contains a formula that calculates
if $inEvenet.contextdata.enviroment.wind.speed > 10

{
"topic":{
"id": "exampleEvent2"
},
"locationdata":{
  "id": "Location1"
},
"contextData":{
  "enviroment": 
  {
    "wind":{
      "speed": "10",
      "format": "m/s"
    }
  }
}
},

Exmaple 3 is the same as exmaple 2 i just wanted to have another property of
current.speed instead of wind.speed

{
"topic":{
"id": "exampleEvent3"
},
"contextData":{
  "enviroment": 
  {
    "current":{
      "speed": "10",
      "format": "m/s"
    }
  }
}
},

In exmaple 4 shall the If formula be true if $inEvent.topic.id = $policy.topic.id
and $inEvent.location.id = $policy.location.id then i would like a 3rd cirteria based
on a fomrula in in $policy.policy that contains a formula that calculates
if $inEvenet.contextdata.equipment.id = “a statement stored in the policy node”
and that calculates if there are $inData.contextData.equipment.instances

id = i1
state=Active

and

id = i2
State = Active

and

id = i2
State = inActive

{
"topic":{
"id": "exampleEvent4"
},
"locationdata":{
  "id": "Location1"
},
"contextData":{
  "equipment": 
  {
    "id": "equipment1",
    "instances":[
    {
      "id": "i1",
      "state": "active"
    },
    {
      "id": "i2",
      "state": "active"
    },
    {
      "id": "i3",
      "state": "inactive"
    }
    ]
  }
}
}

The idea was to have a formula in the property policy that calculated if it was true or false and then add it as 3rd criteria in if formula. However I struggle with formulas that gets stored

Hi

I managed to solve it by a workaround. The thing i wanted to achieve was to store to calculation of true or false in the policy json so based on the convert rule it should return true of false.

But i solved it with this workaround.

  "nodes": [
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"inEvent\"].json[\"topic\"][\"id\"]}}",
              "value2": "={{$node[\"policy\"].json[\"topic\"][\"id\"]}}"
            },
            {
              "value1": "={{$node[\"inEvent\"].json[\"locationdata\"][\"id\"]}}",
              "value2": "={{$node[\"policy\"].json[\"locationdata\"][\"id\"]}}"
            }
          ],
          "number": [
            {
              "value1": "={{$node[\"Function\"].json[\"activeValue\"]}}",
              "operation": "={{$node[\"Convert\"].json[\"validationdata\"][\"policy\"][\"rule\"]}}",
              "value2": "={{$node[\"Convert\"].json[\"validationdata\"][\"policy\"][\"policyValue\"]}}"
            }
          ]
        }
      },
      "name": "IF2",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1680,
        340
      ]
    },
    {
      "parameters": {
        "functionCode": "var policyValues = items[0].json.validationdata.policy.setValue;\n\nvar activePolicyStates =policyValues.filter(value => value.state === \"active\");\n\nreturn [{json:{\"activeValue\": activePolicyStates.length}}];\n\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1530,
        340
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "IF2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}