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, record or newRecord (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's params (list and subscribe), an update's resolved candidate and its changedFields(), record lookups for get() and exists(), and the unlocks owns() 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): unauthenticated is a 401, categorical a 403, and recordDependent (the rule reads the record, newRecord or changedFields()) 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 with operation: 'create' evaluateRule never reports recordDependent.

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.ts creditsAmountMax).

Functions

celFieldsRead(CelProgram program, String variable) → CelFieldsRead
The top-level fields program reads under variable (record or newRecord; any other root name, such as params, reads its members the same way). size(newRecord.title) <= 80 reads {title} under newRecord and nothing under record (size is a function in FFX-CEL, never a method: newRecord.title.size() does not parse). A changedFields() call reads the whole of both record and newRecord.
celFieldType(Map<String, Object?> spec) → Map<String, Object?>?
The rules engine's type of one config field ({type, values?, model?}, rules-engine fieldToCelType), or null for a type rules cannot address (map, bytes, geopoint).
celFieldTypes(String model, Map<String, Map<String, Object?>> fields) → Map<String, Object?>
Every field type a rule over model can address (rules-engine modelFieldCelTypes): the system fields, then each declared field of fields (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's changedFieldNames and 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 (record and newRecord). A writeOnly or serverOnly field counts as changed whenever the patch names it (patched), whatever its value. System fields are never named.
evaluateCel(CelProgram program, CelEnvironment env) → bool
Evaluates program in env to 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 rule for principal against record, the record the operation binds (05 §2.2). rule is a manifest rule ({kind, ownerField?, expression?, message?}) or a config rule ({allow, …}); a null rule denies categorically. program is the already parsed expression, else it is parsed from the rule. request defaults 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 program in env, and what its spend("…", 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.