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.
|| requires at least one condition to be true.
! negates a condition.
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.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.
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#
- Logical operators - Operator precedence, short-circuit semantics, and error handling
- Conditional operator - Type requirements and nesting
- Comparison operators - Equality and ordering for all types
- At a glance - Every function, macro, and operator