rules library
Access rules over plain maps (05 §2): FFX-CEL and the four rule kinds' decisions (evaluateRule, Principal), shared by the app SDK's device checks (local data, 5.13) and the emulator.
Classes
- CelEnvironment
- The variable environment of one evaluation (05 §3.5).
- CelFieldsRead
-
What one expression reads under one variable root,
recordornewRecord(local data, 5.13): the top-level field names it reads, or all of it. A phone decides from this whether a rule's denial can be final (every field the denial read is known on the phone). - CelProgram
- Principal
-
The signed-in end user a rule reads (
user.id,user.accountType,user.claims, 05 §3.5). An anonymous caller has none. - RuleAllow
- RuleDecision
- The outcome of one rule: RuleAllow, RuleDeny or RuleUndecided.
- RuleDeny
- RuleRequest
-
What an expression rule reads besides the principal and the record (05
§3.5):
request.time, the query'sparams(list and subscribe), an update's resolved candidate and itschangedFields(), record lookups forget()andexists(), and the unlocksowns()reads. - RuleUndecided
- This host cannot decide the rule: a kind it does not know, an owner rule without its field, an expression rule without its expression or one its parser refuses (written for a newer FFX-CEL), or, when the operation is given, a rule that reads a record (the stored one, or a create's candidate) the host did not pass. FFX decides instead.
Enums
- RuleDenialReason
-
Why a rule refused, which decides the platform's answer (04 §3.2.1, local
data 3.3):
unauthenticatedis a 401,categoricala 403, andrecordDependent(the rule reads the record,newRecordorchangedFields()) a 404 on a record route unless the 403 rule of 3.3 applies. FFX answers every create denial with a 403 (or a 401), so withoperation: 'create'evaluateRule never reportsrecordDependent.
Constants
- maxRuleExpressionLength → const int
- The configuration schema and TypeScript parser enforce the same budgets.
- maxSpend → const int
-
The most credits one rule's
spend()takes: as many as a pack may hold, the largest integer every client reads exactly (packages/schema config/payments-limits.tscreditsAmountMax).
Functions
-
celFieldsRead(
CelProgram program, String variable) → CelFieldsRead -
The top-level fields
programreads undervariable(recordornewRecord; any other root name, such asparams, reads its members the same way).size(newRecord.title) <= 80reads{title}undernewRecordand nothing underrecord(sizeis a function in FFX-CEL, never a method:newRecord.title.size()does not parse). AchangedFields()call reads the whole of bothrecordandnewRecord. -
celFieldType(
Map< String, Object?> spec) → Map<String, Object?> ? -
The rules engine's type of one config field (
{type, values?, model?}, rules-enginefieldToCelType), or null for a type rules cannot address (map, bytes, geopoint). -
celFieldTypes(
String model, Map< String, Map< fields) → Map<String, Object?> >String, Object?> -
Every field type a rule over
modelcan address (rules-enginemodelFieldCelTypes): the system fields, then each declared field offields(config field specs, by name) that celFieldType types. -
changedFieldNames(
Iterable< String> fieldNames, Map<String, String> markers, Map<String, Object?> stored, Map<String, Object?> candidate, Iterable<String> patched) → List<String> -
What
changedFields()reports for one proposed update (05 §2.2), as the platform'schangedFieldNamesand the emulator's: the named top-level fields whose stored value differs from the candidate's, by deep JSON equality, absent and null alike. Pass both images as the rule reads them (recordandnewRecord). AwriteOnlyorserverOnlyfield counts as changed whenever the patch names it (patched), whatever its value. System fields are never named. -
evaluateCel(
CelProgram program, CelEnvironment env) → bool -
Evaluates
programinenvto its boolean. Throws CelEvaluationError on a deterministic evaluation error — callers DENY on it (05 §6.3). -
evaluateRule(
Map< String, Object?> ? rule, Principal? principal, Map<String, Object?> ? record, {RuleRequest? request, CelProgram? program, String? operation, Map<String, Object?> ? fieldTypes, Map<String, Object?> ? claimTypes}) → RuleDecision -
Evaluates one operation
ruleforprincipalagainstrecord, the record the operation binds (05 §2.2).ruleis a manifest rule ({kind, ownerField?, expression?, message?}) or a config rule ({allow, …}); a null rule denies categorically.programis the already parsed expression, else it is parsed from the rule.requestdefaults to the device clock and nothing else. -
parseCel(
String expression) → CelProgram - Parses one FFX-CEL expression (05 §3.2). Throws CelParseError (message
-
runCel(
CelProgram program, CelEnvironment env) → ({bool allowed, Map< String, int> spends}) -
Evaluates
programinenv, and what itsspend("…", n)calls spend when it allows: the credits on the path that allowed it, summed by name (05 §3.4); empty when it denies. Throws as evaluateCel does.
Typedefs
-
RecordLookup
= Map<
String, Object?> ? Function(String model, String id) - One parsed FFX-CEL expression, evaluated any number of times by evaluateCel.
Exceptions / Errors
- CelEvaluationError
- A deterministic evaluation error (05 §3.6): null in an ordering, arithmetic, membership, or method; a type mismatch the schema-free parse could not see; a record read with no record bound; or a non-boolean result. Callers deny on it — it is never a 500 (05 §6.3).
- CelParseError
-
A grammar, limit, or schema-free typing violation in an expression — what
the platform reports at publish time. Carries the platform's
fix.