| sidebar_position | 8 |
|---|
ZModel provides an expression system that allows you to construct complex computations from primitive constructs like literals and fields. Expressions are used extensively in defining access control policies and input validation rules.
String (e.g., "hello"), number (e.g., 42), and boolean (e.g., true, false) literals are supported. Strings can be enclosed in either single quotes (') or double quotes (").
Lists can be constructed using square brackets, e.g., [1, 2, 3] or ['a', 'b', 'c'].
Model's fields can be directly referenced by their names within the current model context.
You can use the dot notation to access members of an expression that contains fields. E.g., author.name if author is a relation field. Member access expressions can be chained.
The following operators are supported:
!(logical NOT): E.g.,!true
The following operators are supported:
==(equal to): E.g.,a == b!=(not equal to): E.g.,a != b>(greater than): E.g.,a > b>=(greater than or equal to): E.g.,a >= b<(less than): E.g.,a < b<=(less than or equal to): E.g.,a <= b&&(logical AND): E.g.,a && b||(logical OR): E.g.,a || b
ZModel follows JavaScript's operator precedence and associativity rules.
Collection predicate expressions are used to express a boolean condition over a to-many relation field. The expression has the following three variants:
-
relation?[CONDITION]Evaluates to true if at least one related record satisfies the condition. E.g.,
posts?[published == true]. -
relation![CONDITION]Evaluates to true if all related records satisfy the condition. E.g.,
posts![published == true]. -
relation^[CONDITION]Evaluates to true if no related records satisfy the condition. E.g.,
posts^[published == true].
The CONDITION is an expression under the context of the relation, meaning that fields referenced in the condition are resolved against the related model. You can use this keyword to "escape" and refer to the fields belonging to the model where the rule is defined.
The in keyword can be used to check if a value exists in a list. E.g., value in [1, 2, 3].
Functions can be called using the standard syntax: functionName(arg1, arg2, ...). See Functions for the list of available functions.
The null keyword represents a null value expression.
The this keyword refers to the current entity of the model.