Skip to main content
SnackBase includes a powerful rule engine that compiles expression-based rules into efficient SQL WHERE clauses. This guide explains the syntax, variables, and best practices for writing rules.

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.
Example: [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_by
  • status
  • your_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:
  1. Navigate to Collections -> Edit Collection -> Rules.
  2. Write your rule.
  3. 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

  1. Keep Rules Simple: Complex logic is hard to debug. Use SQL Macros for complex multi-table checks.
  2. Use Parentheses: Always group conditions in complex expressions to ensure correct evaluation order.
  3. Optimistic Rules: In SnackBase, rules are compiled to WHERE clauses, so they are extremely fast.
  4. Deny by Default: If no rule is defined for an operation, access is denied.