Search code examples
open-policy-agentrego

OPA Rego function statement evaluation order


package play


exists(obj, a) {
   obj[a]
}


hello {
     exists(input, "department")
     contains(input["location"], "London")
}

world {        
    contains(input["location"], "London")
    exists(input, "department")    
}

input = { "department": "Eng", "location": "London" }

Above code matches only hello. Why world does not match even when the conditions are same, but order reversed ?


Solution

  • The order of the statements does not matter. You've actually found a bug!

    If you change the example slightly so that exists is not called with input as the first argument but instead something like exists(input.user, "department") and then you update the input document to reflect that:

    {"user": {"department": "Eng", "location": "London"}}
    

    You'll observe the correct behaviour (e.g., world { contains(input.user["location"], "London"); exists(input.user, "department") }).