我需要使用rego来评估一个json数组{ "users": [ { "name": "Alice", "age": 25 }, { "name": "Bob", "age":…
- User
我需要使用rego来评估一个json数组{ "users": [ { "name": "Alice", "age": 25 }, { "name": "Bob", "age": 35 } ] } 我希望该rego可以使用独立的变量来定义单个允许的用户,并且最终给出两种评估结果1. 当使用单个用户来评估时,给出该用户是否满足规则, 2.当使用多个用户来评估时,给出这多个用户是否都在定义的允许用户内。
Assistant
要使用rego来评估JSON数组,首先我们需要设定一个定义的允许用户的规则。
例如,我们可以定义允许的用户如下:
codeusers_allowed := { "Alice": true, "Bob": true }接着,我们可以定义评估一个用户是否被允许的规则:
codeallowed_user := input.users[_] { allowed_user.name := key users_allowed[key] }现在,我们可以使用上面定义的规则来评估数组中的每个用户:
codeallowed_users := [ { name: input.users[_].name, allowed: allowed_user } ]最后,我们可以定义评估包含多个用户是否都被允许的规则:
codeall_users_allowed := [ { name: users.name, allowed: true } ] all_users_allowed_filtered := [ x | x.allowed ] allowed_all_users_count := len(all_users_allowed_filtered)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.