Rules and types
The rules directory
Every .luau file in the rules directory, and in its subfolders, is one of:
| File | What it is |
|---|---|
exports Input and Output |
a rule |
ends in .test.luau |
tests (Tests) |
ends in .d.luau |
type definitions for your editor; Bree skips it |
| anything else | a module: shared code that rules require |
A rule's name is its path without .luau. rules/shipment_eligibility.luau is shipment_eligibility, and rules/billing/refunds.luau is billing/refunds. Name rule files and folders in snake_case; Bree refuses other names, because they become Rust modules and Go identifiers.
Files and folders whose names start with a dot are skipped.
A rule
--!strict
-- Decides whether a parcel can ship with the service the customer picked.
-- A delivery service level.
export type Service = "standard" | "express" | "overnight"
export type Input = {
-- Parcel weight in grams.
weight_grams: int,
service: Service,
}
export type Output = {
eligible: boolean,
reason: "eligible" | "overweight",
}
return function(input: Input): Output
if input.weight_grams < 1 then
invalid("weight_grams", "must be at least 1 gram")
end
-- ...
endA rule:
- exports an
Inputtype and anOutputtype. Exporting one without the other is an error; - returns a function that takes an
Inputand returns anOutput. Bree checks this; returning anything else fails the check; - is type checked in strict mode, whether or not it starts with
--!strict. Keep the line anyway so your editor checks in strict mode too.
The comment at the top of the file is the rule's description. A comment directly above a type or a field is that type's or field's description. Both are copied into the generated Rust and Go code as doc comments.
Types
Input and Output, and every type they use, must have an exact meaning in JSON, because that is how values cross into and out of the rule:
| Luau | JSON | Rust | Go |
|---|---|---|---|
boolean |
true or false |
bool |
bool |
number |
any number | f64 |
float64 |
int |
a whole number, at most 2^53 either way | i64 |
int64 |
string |
a string | String |
string |
"a" | "b" |
one of those strings | an enum | a string type with a constant per value |
T? on a field |
the field may be missing or null |
Option<T> |
*T (slices and maps stay as they are) |
{ T } |
an array | Vec<T> |
[]T |
{ [string]: T } |
an object with any keys | BTreeMap<String, T> |
map[string]T |
{ name: T, ... } |
an object with exactly those fields | a struct | a struct |
Anything else, such as a function type or any, is refused with a message that lists what is supported. ({ [number]: T } is Luau's long form of { T }, so it is an array too.) Type aliases are fine (type Cents = int), and so are types from a module (Money.Amount).
Named types keep their names in generated code. An inline table or string union is named after the field it appears on: reason: "eligible" | "overweight" becomes Reason, and the items of service_limits: { { ... } } become ServiceLimit.
int is Bree's: Luau has only number. Inside the rule it is a number; at the boundary Bree accepts only whole numbers for it, and Rust and Go get integer types.
Rejecting input
The types say what shape the input has. Anything they cannot say (a weight of at least one gram, a postal code's format) goes in the rule, with invalid:
if input.weight_grams < 1 then
invalid("weight_grams", "must be at least 1 gram")
endinvalid(field, message) stops the rule. The caller gets an invalid_input error naming the field, the same error it gets for input that does not match the types, so an application can show either beside the right form field:
{"error":{"code":"invalid_input","message":"the input was rejected","violations":[{"field":"weight_grams","message":"must be at least 1 gram"}]}}Tests check rejections with toBeInvalid("weight_grams").
Sharing code between rules
A module is a file that exports neither Input nor Output. Rules, tests and other modules load it with require and a path relative to the requiring file:
-- rules/shared/money.luau
--!strict
export type Cents = int
local money = {}
function money.percent_of(amount: Cents, percent: number): Cents
return math.floor(amount * percent / 100)
end
return money-- rules/billing/refunds.luau
local money = require("../shared/money")Paths must start with ./ or ../ and may not leave the rules directory. A rule's digest covers the source of every module it requires, so a change to a shared module shows up in the digest of every rule that uses it.
What a rule can use
A rule runs in a sandbox, so the same input always gives the same output. It has Luau's table, string, math, utf8, bit32 and buffer libraries, and the built-in functions such as pairs, tostring and error, with these removed:
math.randomandmath.randomseed;os(no clock),debug,print;loadstring,getfenv,setfenv,newproxy,collectgarbage,gcinfo.
There is no io, no network, and no require outside the rules directory. Everything a rule needs comes in through its Input: pass the date, the customer's tier or the carrier's limits in, rather than looking them up. That keeps rules easy to test, and every decision reproducible from its input and digest.
Every evaluation also runs under step, memory and time limits; see The runtime.