Business Logic
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):
| Purpose | Column |
|---|---|
| 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 (forinput_type_rule, and for a live preview ofcomputed_fieldswhile 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_actorandhas_consultationlook 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:
- Insert or update arrives at the API. The platform loads the entity’s
computed_fieldsand runs them in declared order. Each derived value is written into the incoming row before validation, so later rules can reference it. - Validation runs the entity’s
validation_rulesagainst the merged row. Any rule that evaluates falsy rejects the write with the rule’scodeandmessage. Athrow_errorin a rule rejects it with its own message and code instead, and a failingrequire_permissionwith a permission error (42501). - Persist. The row is written.
- 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_fieldsandvalidation_rules, so a rule such as “the order total stays under the credit limit” holds whichever record changed. - Delete runs the entity’s
validation_rulesagainst the row being removed, with$modeset to"delete"and$oldbound to that row. Any rule that evaluates falsy aborts the delete;computed_fieldsstill run but their output is discarded (there is no surviving row). This lets a rule guard against deletion, for example{"!=": [{"var": "$mode"}, "delete"]}. - 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’sview_permission. This is row-level security expressed as data. A rule that raises an error on a row hides that row. - UI render. When a form is built from the model, each field’s
input_type_ruleis evaluated client-side against the current form values. It returns the field’s input mode, which replaces the staticinput_type:hiddenhides the field,readonlyanddisabledlock it,requiredmakes it mandatory. A rule that throws, or returns anything else, keeps the static mode. The form also re-evaluatescomputed_fieldsas 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 makesdisabledis left out of the request, and a field with anyinput_type_rulegets no databaserequiredcheck.
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:
- 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 invalidation_rules. If it filters access to rows, it goes inselect_rule. If it changes how a field is edited, it goes ininput_type_rule. - Write the expression using standard JsonLogic plus the Semantius extensions. Reach for
varto read fields of the current row,$oldto compare against the previous state on update, andset_recordto pull in a related record by id. - 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_errorcode 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. - Test it with a representative row.
To evaluate a rule against sample data, call
/rpc/evaluate_json_logicwith the rule asruleand a sample record asdata: it returns the result, evaluated with the caller’s rights. It binds no$variables, so put$today,$now,$user_id,$oldor$modeintodatawhen the rule reads them.
Next
- JsonLogic overview: the upstream language, with links and small samples.
- JsonLogic extensions: the variables and operators Semantius adds on top, and the shape of each rule.
- Rollup fields: aggregates over child records, kept current by the database.