Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove exists function #15

Merged
merged 1 commit into from
Feb 10, 2025
Merged

remove exists function #15

merged 1 commit into from
Feb 10, 2025

Conversation

lonegunmanb
Copy link
Member

An exists function lure users into thinking they can use a not exists() check, or a not_exists() function, which is not correct. When we pass a non-exist path as function's parameter, the whole rule check stops with result as undefined. None of not exists(non_exist_path) nor not_exists(non_exist_path)` works as expected.

Another classic approach is to check existence is like:

rule1 := true if {
    input.foo.bar
}

If we'd like to assert that the path is not existed, we might use:

rule1 := true if {
    not input.foo.bar
}

But this rule would return a false result if input.foo.bar's type is boolean and the value is false.

The correct way to check non-existence is:

rule1 := true if {
    not input.foo.bar == input.foo.bar
}

In rego, == function wouldn't be effected by a non-exist path. For symmetry reasons, we stipulate that in this library, we must check the existence by:

exist := true if {
    input.foo.bar == input.foo.bar
}
not_exist := true if {
    not input.foo.bar == input.foo.bar
}

@lonegunmanb lonegunmanb merged commit e99b175 into main Feb 10, 2025
2 checks passed
@lonegunmanb lonegunmanb deleted the better-exists branch February 10, 2025 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant