https://cerbos.dev logo
#help
Title
# help
f

Farzad Soltani

09/19/2023, 12:30 PM
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

Jesum Yip

09/19/2023, 12:32 PM
Cerbos uses Google cel language. This might help. https://github.com/google/cel-go#macros
f

Farzad Soltani

09/19/2023, 12:36 PM
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

Jesum Yip

09/19/2023, 12:36 PM
Expr: !has(.....) try that. I cannot remember off-hand but that might work.
f

Farzad Soltani

09/19/2023, 12:37 PM
Tried it. Didn't work
j

Jesum Yip

09/19/2023, 12:37 PM
Ok
c

Charith (Cerbos)

09/19/2023, 12:53 PM
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

Farzad Soltani

09/19/2023, 2:00 PM
image.png
Thanks for introducing the REPL by the way.
c

Charith (Cerbos)

09/19/2023, 2:15 PM
Hmm.. that's odd. Could you try writing it like this:
Copy code
- expr: >
      !has(request.resource.attr.roles)
m

Michael

09/19/2023, 2:26 PM
@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

Farzad Soltani

09/19/2023, 3:22 PM
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
4 Views