JSON that runs.
Stof is JSON with functions. Put the logic next to the data, and your app runs the document in a sandbox that can only touch what you hand it.
A model can write it. A person can read it. Your app can run it.
cargo add stofpip install stof
{
"customer": "acme",
"plan": { "rate": 0.02, "included": 1000 },
fn price(units: int) -> float {
const billable = units - self.plan.included;
if (billable <= 0) return 0;
billable * self.plan.rate
}
}A model wrote it. Check it before it runs.
Models write Stof well, because it reads like JSON and JavaScript. Parse a draft, run it against the cases you care about, and ship it only if it passes. No deploy, and nothing runs outside your process.
// A model drafted this rule for one customer.
draft: r#"
fn discount(units: int) -> float {
units > 50_000 ? 0.9 : 0
}
"#
#[main]
fn main() {
using rule = new {};
parse(self.draft, rule);
// Before it ships, your app checks what it does.
for (const units in [100, 10_000, 60_000]) {
const off = rule.discount(units);
if (off < 0 || off > 0.5) {
pln(`rejected: ${(off * 100).round()}% off at ${units} units`);
return;
}
}
pln('approved');
}The config checks itself.
Declare the rule once on a type, point it at any object. No schema file, no validation library.
#[type]
Server: {
#[schema((v: int): bool => v > 0 && v < 65536)]
int port: 8080
}
config: { port: 70000 }
#[main]
fn main() {
pln(<Server>.schemafy(self.config)); // false
self.config.port = 443;
pln(<Server>.schemafy(self.config)); // true
}It can't touch what you didn't hand it.
No file system, network, or environment unless your app turns them on. The sandbox isn't a setting, it's how a document runs. Your app gives a document functions with doc.lib(...), and that's all it can call.
Same document. Your language.
Load the document, hand it the functions it may call, set a time limit, and call it. The same document runs in TypeScript, Python, and Rust, and gives the same answer in each.
import { StofDoc } from '@formata/stof';
// policy: Stof text a model, a customer, or your team wrote
const doc = await StofDoc.new();
// the only function it can call
doc.lib('App', 'plan', (id: string) => plans.get(id));
doc.setMaxExecutionTime(50); // ms
doc.parse(policy);
const price = await doc.call('price', 'acme', 1200);
Stof runs every customer's pricing at Limitr, in production, on every call.