Rules
rules is a list of Expr expressions, which every write of an element of the type has to satisfy. Rules cover what the property can not express, e.g. relations between properties, values which must never decrease, or files of a certain kind. The guide writing schema shows how to create and debug them.
A rule has a name (letters, digits, _ and -), an expression and an optional description. At most 16 rules can be defined per schema, which is . Rules are not available for built-in and if expressions are .
{ "mode": "auto", "rules": [ { "name": "positive-year", "expression": "document.year == nil || document.year > 0 ? true : \"The year has to be positive.\"", "description": "The year of publication is positive, there is no year 0." }, { "name": "counter-never-decreases", "expression": "operation != \"update\" || document.counter >= previous.counter" } ]}Validation on save
If expressions are enabled, the rules are compiled when the schema is saved ( and ). A rule which does not compile, e.g. because of a syntax error, an unknown function or an operation on incompatible types, rejects the request with 400 Bad Request; the error message names the rule and contains the compile error, and the schema is not saved. Only compilation is checked: what a rule returns and whether it fails at runtime depends on the data of the writes.
Evaluation
Rules run synchronously after the properties were validated, in the order of the list, and stop at the first rule which is not satisfied. A rule is satisfied if it returns exactly true, everything else rejects the write with 400 Bad Request. If the rule returns a non-empty string, the string becomes the error message. Every other result which is not true (false, any other value or no value) uses the description of the rule as the error message, or a generic one if the rule has no description. A crash, a timeout or an expression which can not be evaluated, e.g. because of a typo, is reported as a rule which could not be evaluated. Such a rule blocks all writes of the type until the schema is fixed; schema changes are never blocked by rules. Rules are not applied to deletions.
Variables
Values have the shape of their : geo values are GeoJSON objects, whatever format the request used, datetime values are strings in the format in which they were written (e.g. 2026-01-02T03:04:05+01:00, use date() to compare them as dates), vectors are lists of numbers, objects and lists are maps and arrays.
The kind of a number follows the schema: values of integer properties are integers, so operators which only accept integers work, e.g. document.year % 4 == 0, and values of number properties are floats, also whole ones which were written as 2. Numbers of properties which the schema does not define keep the kind in which they were written. Expr’s builtin type() tells them apart: type(document.year) == "int".
Geo values
The geo read, measure and compare geo values, e.g. to keep a location inside of an area, to limit the size of an area or the length of a route:
{ "name": "location-in-area", "expression": "document.location == nil || document.area == nil || geoContains(document.area, document.location) ? true : \"The location has to be inside of the area.\""}Rules are stricter than the validation of geo values: a geometry which the functions consider invalid, e.g. a polygon whose ring intersects itself, fails the rule. Try rules with the , whose input type “Element” provides the variables of rules.
Files
Rules also run when a file is uploaded, with operation file: file describes the new file, document and previous are the properties of the element. Direct uploads are checked before the file is stored, resumable uploads when the request which completes them arrives; a rejected resumable upload is discarded. In all other writes, file describes the file which the element has at that moment, so a rule can, e.g., require a property for elements with large files.
| Field | Value |
|---|---|
size | The size in bytes. |
mimeType | The detected mime type, not the one the request states. |
extension | The extension, taken from the file name of the upload, without the dot, or nil. |
sha256 | The SHA-256 digest, hex encoded, or nil if it is not known yet. |
{ "name": "small-images", "expression": "file == nil || !(file.mimeType startsWith \"image/\") || file.size <= 1048576", "description": "Images are at most 1 MiB large."}The file name of an upload only determines the extension, it does not change the name of the element.
Performance and safety
Rules are evaluated by an Expr Lang engine which runs inside the API process: all rules of a write are evaluated with a single call, and the compiled expressions are cached, so rules add little time to a write. Running inside the process is what makes them fast, but an expensive expression also occupies a worker of the API until it finishes or exceeds the timeout of the expression . Defining rules therefore needs the capability schema.manage, which only administrators should hold. Expressions of users, e.g. in search queries, run in a separate sandboxed process instead, which is started for every expression and stopped when it exceeds the timeout.