I went through the docs and couldn't find a way to...
# help
f
I went through the docs and couldn't find a way to check whether an attribute exists or not. Is this expression valid?
expr: request.resource.attr.roles == null
j
Cerbos uses Google cel language. This might help. https://github.com/google/cel-go#macros
f
Thanks. Something like this seems to work:
expr: has(request.resource.attr.roles) == false
I was thinking that there should be a better way to write it.
j
Expr: !has(.....) try that. I cannot remember off-hand but that might work.
f
Tried it. Didn't work
j
Ok
c
The
!
operator should work. You can try out expressions in our REPL
Copy code
-> :let x = {"foo": "bar"}
x = {
  "foo": "bar"
}

-> has(x.foo)
_ = true

-> !has(x.foo)
_ = false

-> !has(x.bar)
_ = true
f
image.png
Thanks for introducing the REPL by the way.
c
Hmm.. that's odd. Could you try writing it like this:
Copy code
- expr: >
      !has(request.resource.attr.roles)
m
@Farzad Soltani
!
has special meaning in YAML, so in your case
!has
gets interpreted as YAML tag, and not as CEL. Check out the YAML spec for more info: https://yaml.org/spec/1.2-old/spec.html#id2784064
f
Yes. You're completely right @Michael Your workaround works perfectly @Charith (Cerbos) I love learning new things when working with Cerbos. I will be writing lots of blog posts soon.
🙌 3