Skip to content

Commit

Permalink
Don't assume a single wildcard in custom-has-key-construct (#183)
Browse files Browse the repository at this point in the history
Fixes #182

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored Jun 27, 2023
1 parent 64c99c6 commit 6dcc15e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bundle/regal/rules/idiomatic/custom_has_key_construct.rego
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ report contains violation if {
# description: Normalize var to always always be on the left hand side
normalize_eq_terms(terms) := [terms[1], terms[2]] if {
terms[1].type == "var"
terms[1].value == "$0"
startswith(terms[1].value, "$")
terms[2].type == "ref"
}

normalize_eq_terms(terms) := [terms[2], terms[1]] if {
terms[1].type == "ref"
terms[2].type == "var"
terms[2].value == "$0"
startswith(terms[2].value, "$")
}
22 changes: 22 additions & 0 deletions bundle/regal/rules/idiomatic/custom_has_key_construct_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,25 @@ test_fail_custom_has_key_reversed if {
"title": "custom-has-key-construct",
}}
}

test_fail_custom_has_key_multiple_wildcards if {
r := rule.report with input as ast.policy(`
other_rule["foo"] {
wildcard := input[_]
}
has_key(name, coll) {
coll[name] = _
}`)
r == {{
"category": "idiomatic",
"description": "Custom function may be replaced by `in` and `object.keys`",
"level": "error",
"location": {"col": 2, "file": "policy.rego", "row": 8, "text": "\thas_key(name, coll) {"},
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/custom-has-key-construct", "idiomatic"),
}],
"title": "custom-has-key-construct",
}}
}

0 comments on commit 6dcc15e

Please sign in to comment.