简体   繁体   English

用于检查列表中员工姓名的 Rego 规则

[英]Rego rule to check for employee name in a list

I am new to rego code and writing a rule to check for employee names if they present in the approved employee list.我是 rego 代码的新手,并编写了一条规则来检查员工姓名是否出现在批准的员工列表中。 If not, it should print out the employees who are not part of the list.如果没有,它应该打印出不在列表中的员工。 Here is the input I am giving:这是我给出的输入:

{ "valid_employee_names": { "first_name.2113690404": "emp_Maria", "first_name.2641279496": "emp_Rosie", "first_name.3921413181": "emp_Shaun", "first_name.588579514": "emp_John" }, "destroy": false } {“valid_employee_names”:{“first_name.2113690404”:“emp_Maria”、“first_name.2641279496”:“emp_Rosie”、“first_name.3921413181”:“emp_Shaun”、“first_name.588579514”:“emp_John”}、“destroy” : 错误的 }

Here is the rego rule:这是rego规则:

valid_name := {i: Reason |
    check_list := {["emp_John","emp_Monika","emp_Cindy","emp_Katie","emp_Kein"]}
    doc = input[i]; i="valid_employee_names"
    key_ids := [k | doc[k]; startswith(k, "first_name.")]
    resource := {
        doc[k] : doc[replace(k, ".key", ".value")] | key_ids[_] == k
    }
    emp_name := [m | resource[m]; startswith(m, "emp_")]
    list := {x| x:=check_list[_]}
    not(list[emp_name])
    Reason := sprintf("Employees not on record found - %v . :: ", emp_name)
}

I always get the same output that employees not on record are found.我总是得到相同的 output 发现没有记录在案的员工。 Can anyone point me to see what needs to be corrected/updated?谁能指出我需要更正/更新的内容?

Thanks!谢谢!

The rule you posted is quite complicated.您发布的规则非常复杂。 If you only need to compute the set of employee names NOT in the approved list (which I assume is input.valid_employee_names ) start by computing a set of valid names and then take the difference.如果您只需要计算一组不在批准列表中的员工姓名(我假设是input.valid_employee_names ),则首先计算一组有效姓名,然后取差值。

# valid_employee_names generates a set of valid employee (first) names from the input.
valid_employee_names := {v | 
    some k
    v := input.valid_employee_names[k]
    startswith(k, "first_name.")}

I'm assuming the list of names to check against is check_list .我假设要检查的名称列表是check_list Note, you have check_list defined as a set containing an array of names (represented as strings).请注意,您已将check_list定义为包含名称数组(表示为字符串)的集合。 There is no obvious need for the extra array.没有明显需要额外的数组。 Just define the check_list as a set of names:只需将check_list定义为一组名称:

check_list := {"emp_John", "emp_Monika", "emp_Cindy", "emp_Katie", "emp_Kein"}

Now we have two sets we can easily find the invalid names using set difference:现在我们有两个集合,我们可以使用集合差异轻松找到无效名称:

check_list - valid_employee_names

Putting it all together:把它们放在一起:

valid_employee_names := {v | 
    some k
    v := input.valid_employee_names[k]
    startswith(k, "first_name.")}

check_list := {"emp_John", "emp_Monika", "emp_Cindy", "emp_Katie", "emp_Kein"}

invalid_names := check_list - valid_employee_names

(playground link: https://play.openpolicyagent.org/p/5KfXnSIkHa ) (游乐场链接: https://play.openpolicyagent.org/p/5KfXnSIkHa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM