Overview
Rules are boolean expressions associated with collection operations (list, view, create, update, delete).
When to Use Rules
Rule Syntax
Basic Expressions
Logical Operators
Operator Precedence:
() > ! > comparisons > && > ||
Evaluation Context
Rules have access to the following variables:1. @request.auth (Standard)
2. @request.data (Action-specific)
Available during create and update operations.
@request.data.fieldname: Accesses the value being sent in the request body.
[email protected]_admin (Don’t allow setting admin flag via this route).
3. Record Fields (Direct Access)
You can reference any field in the record directly by name.created_bystatusyour_custom_field
Built-in Macros
Rule Examples
Example 1: Edit Own Drafts
Only allow editing if the user is the creator AND the status is “draft”.Example 2: Admin or Owner
Example 3: String Pattern Matching
Allow viewing only if the record type starts with “public”.Example 4: Complex Multi-Condition
Testing Rules
Manual Testing in Admin UI
SnackBase provides a built-in Rule Tester in the Collections settings:- Navigate to Collections -> Edit Collection -> Rules.
- Write your rule.
- Use the “Test Rule” button to simulate evaluation against a mock user and record.
API Testing
You can also test rules via the API:Best Practices
- Keep Rules Simple: Complex logic is hard to debug. Use SQL Macros for complex multi-table checks.
- Use Parentheses: Always group conditions in complex expressions to ensure correct evaluation order.
- Optimistic Rules: In SnackBase, rules are compiled to
WHEREclauses, so they are extremely fast. - Deny by Default: If no rule is defined for an operation, access is denied.