Semantius Logo
DocsData CatalogBusiness Logic

Business Logic

On this page

Business logic in Semantius is data, not code. Rules live on the same entities and fields as your schema, expressed in JsonLogic, and evaluated by a PL/pgSQL engine inside the database itself. There are no hooks to deploy, no functions to host, and no out-of-band scripts to keep in sync with the model. Because enforcement happens at the storage layer, no agent, misbehaving client, or alternate API surface can bypass a rule: every write and every read passes through the same engine. Three cases fall outside it. A cascade delete deletes a parent’s cascade children without checking the user’s permissions on them or their select_rule (see Delete modes). Deleting an entity or a module, which takes admin, drops its tables with every record in them. And the rules of an unmanaged entity are stored but not enforced.

The platform exposes five extension points. Each one is a column on either an entity or a field, and each one holds a JsonLogic expression (or an array of them):

PurposeColumn
Derives values for the named fields on every insert/update, before the row is written.entities.computed_fields
Each rule must evaluate truthy or the write (or delete) is rejected with the given message and code.entities.validation_rules
Per-row access filter. Only rows where the rule evaluates truthy can be read, updated or deleted by the caller.entities.select_rule
Returns the field’s input mode (default, required, readonly, disabled or hidden), evaluated client-side against the current form values.fields.input_type_rule
Restricts which child records a rollup field aggregates. Compiled to SQL from a subset of JsonLogic.fields.rollup (its filter)

Why JsonLogic

JsonLogic gives Semantius a single, portable expression language that:

  • Serializes cleanly as JSON, so rules sit alongside the rest of the model in the same markdown spec, the same database row, and the same diff.
  • Runs in the database (for computed_fields, validation_rules, select_rule) via a PL/pgSQL interpreter, and in the browser (for input_type_rule, and for a live preview of computed_fields while a form is edited), with no compiler or runtime to ship. A rollup filter is the one exception: it is compiled to SQL.
  • Cannot be bypassed. Because the engine lives in the database, every insert, update, delete and select is evaluated against the rules regardless of which client, agent, or service issued the request, apart from the three cases described above.
  • Is safe to evaluate against untrusted input, since it makes no writes and has no host access. Beyond the variables the platform binds for it, a rule reads only what set_record, has_permission, is_raci_actor and has_consultation look up in the database.
  • Is readable enough that agents can author, audit, and refactor rules without touching application code.

For the language itself, see the JsonLogic overview. For the Semantius-specific variables and operators (such as $user_id, $old, set_record, has_permission, value_changed, throw_error) and the shape of each rule, see JsonLogic extensions. For aggregates over child records, see Rollup fields.

Where the rules run

The five extension points map onto distinct stages of the request lifecycle:

  1. Insert or update arrives at the API. The platform loads the entity’s computed_fields and runs them in declared order. Each derived value is written into the incoming row before validation, so later rules can reference it.
  2. Validation runs the entity’s validation_rules against the merged row. Any rule that evaluates falsy rejects the write with the rule’s code and message. A throw_error in a rule rejects it with its own message and code instead, and a failing require_permission with a permission error (42501).
  3. Persist. The row is written.
  4. Rollups. When the written row is a child that rollup fields aggregate over, the parents it touched are recomputed in the same transaction. A parent whose total moved runs its own computed_fields and validation_rules, so a rule such as “the order total stays under the credit limit” holds whichever record changed.
  5. Delete runs the entity’s validation_rules against the row being removed, with $mode set to "delete" and $old bound to that row. Any rule that evaluates falsy aborts the delete; computed_fields still run but their output is discarded (there is no surviving row). This lets a rule guard against deletion, for example {"!=": [{"var": "$mode"}, "delete"]}.
  6. Read requests pass through the entity’s select_rule. The platform evaluates the rule per row, with the row bound as the root context, and returns only rows where it evaluates truthy. The same rule decides which rows an update or delete can reach, and it takes the place of the entity’s view_permission. This is row-level security expressed as data. A rule that raises an error on a row hides that row.
  7. UI render. When a form is built from the model, each field’s input_type_rule is evaluated client-side against the current form values. It returns the field’s input mode, which replaces the static input_type: hidden hides the field, readonly and disabled lock it, required makes it mandatory. A rule that throws, or returns anything else, keeps the static mode. The form also re-evaluates computed_fields as the user types, so a computed field shows its new value before the save. Neither is the authoritative gate: the server recomputes and validates on every write, and its result wins. Two effects reach the stored record all the same: a field a rule makes disabled is left out of the request, and a field with any input_type_rule gets no database required check.

Authoring rules

Rules are stored in the model alongside the entities and fields they govern. In day-to-day work you let an agent with the use-semantius skill write and edit them on your instance. When you do need to write one by hand, the workflow is:

  1. Decide which extension point the rule belongs to. If the rule derives a value from the record itself, it goes in computed_fields. If it aggregates child records, it is a rollup field. If it rejects a write, it goes in validation_rules. If it filters access to rows, it goes in select_rule. If it changes how a field is edited, it goes in input_type_rule.
  2. Write the expression using standard JsonLogic plus the Semantius extensions. Reach for var to read fields of the current row, $old to compare against the previous state on update, and set_record to pull in a related record by id.
  3. Save it. The platform checks every rule when the entity or field is saved, and refuses an unknown operator, a wrong number of arguments, a misspelled date unit, part or pattern, a throw_error code outside class 99 and a $ name it does not bind, among others. A misspelled field or entity name still passes, and finds nothing when the rule runs. See Checks when a rule is saved.
  4. Test it with a representative row. To evaluate a rule against sample data, call /rpc/evaluate_json_logic with the rule as rule and a sample record as data: it returns the result, evaluated with the caller’s rights. It binds no $ variables, so put $today, $now, $user_id, $old or $mode into data when the rule reads them.

Next