Skip to content

Logic and conditions#

CEL combines checks with logical operators and selects values with the conditional operator. Examples on this page use this message:

{
    "id": "ord-123",
    "customer": "alice@example.com",
    "customer_age": 30,
    "tags": ["express", "gift-wrapped"],
    "items": [
        {"name": "Laptop", "category": "electronics", "price": 999, "quantity": 1},
        {"name": "Cable", "category": "accessories", "price": 29, "quantity": 3}
    ],
    "status": "pending"
}

Combining checks#

&& requires both conditions to be true.

order.customer_age >= 18 && "express" in order.tags
// result: true (bool)

|| requires at least one condition to be true.

order.customer.endsWith("@example.com") || "express" in order.tags
// result: true (bool)

! negates a condition.

!("overnight" in order.tags)
// result: true (bool)

Group conditions with parentheses to control evaluation order.

order.customer_age >= 18 && ("express" in order.tags || order.customer.endsWith("@example.com"))
// result: true (bool)

Conditional operator#

The conditional operator ? : selects between two values.

order.customer_age >= 18 ? "adult" : "minor"
// result: "adult" (string)
"express" in order.tags ? "expedited" : "standard"
// result: "expedited" (string)
order.status == "pending" ? "awaiting confirmation" : "processing"
// result: "awaiting confirmation" (string)

Short-circuit evaluation#

CEL evaluates && and || left to right and stops early when the result is determined. Use this to guard expressions that might otherwise error.

size(order.tags) > 0 && order.tags[0] == "express"
// result: true (bool)

If size(order.tags) is 0, the right side doesn't evaluate, avoiding an index-out-of-range error.

Complete validation rules#

Combine multiple checks into a single expression.

order.customer_age >= 18 && "express" in order.tags && order.customer.endsWith("@example.com")
// result: true (bool)
size(order.items) > 0 && order.items.all(i, i.quantity > 0) && order.status == "pending"
// result: true (bool)

See also#