PeaLang / Code Library / Blueprint
Production Blueprint · v3.1.36

FinTech risk decision rules

Domain: FinTech / Fraud & CompliancePeaLangPythonJava
Verified example. The code below was checked against the local PeaLang v3.1.36 parser, interpreter or async VM. It uses only syntax and built-ins present in that source tree.

Problem statement

A payment service needs one reviewable rule that is shared by a Python batch job and a Java API. A malformed transaction must never silently pass, and every rejection must carry a stable machine code plus minimal context for audit logs.

Complete code

const MAX_AMOUNT = 10000
const HIGH_RISK_COUNTRIES = ["XX", "YY"]

function evaluate(tx) {
  if tx == null {
    return error("ValueError", "missing transaction", "E_NULL")
  }
  if tx["amount"] == null {
    return error("ValueError", "missing amount", "E_AMOUNT", null, {"field": "amount"})
  }
  if tx["amount"] > MAX_AMOUNT {
    return error("ValueError", "amount above limit", "E_LIMIT", null,
                 {"amount": tx["amount"], "limit": MAX_AMOUNT})
  }
  if tx["country"] in HIGH_RISK_COUNTRIES {
    return {"decision": "REVIEW", "score": 60, "reasons": ["country"]}
  }
  let score = std.math.clamp(tx["amount"] / 100, 0, 50)
  return {"decision": "ALLOW", "score": score, "reasons": []}
}

let approved = evaluate({"amount": 250, "country": "IN"})
print std.json.stringify(approved)
let denied = evaluate({"amount": 25000, "country": "IN"})
print denied["code"]
Save as fintech-risk-rules.pea

Run & verify

pea fmt --check fintech-risk-rules.pea
pea check fintech-risk-rules.pea
pea run fintech-risk-rules.pea
Always run the formatter and static checker before execution: pea fmt --check fintech-risk-rules.pea && pea check fintech-risk-rules.pea, then pea run fintech-risk-rules.pea (or the host command shown above for async modules).

Why & How

  1. Input shape first. The rule accepts a plain map, so callers from Python or Java can pass JSON-derived data without host objects. No secret or live connection is embedded in the script.
  2. Total behaviour. Each invalid branch returns a structured error(kind, message, code, cause, context) value instead of throwing. The stable codes E_NULL, E_AMOUNT and E_LIMIT become log keys and test assertions.
  3. Why five arguments. The fourth argument is the cause (omitted here with null); the fifth is a small context map. Context carries only the offending amount and limit — never PII or tokens.
  4. Decision logic. in checks the country against a constant allow-review list, and std.math.clamp bounds the numeric score. Both are ordinary v3.1.36 runtime features, so the function works identically in the interpreter and when embedded.
  5. Cross-language use. Embed the function with PeaRuntime from Python or expose it over the generated RPC client from Java. The rule itself remains the single source of truth; neither host reimplements the policy.
  6. Verification. The printed output is a JSON decision map for the allowed case and E_LIMIT for the blocked case, confirming both branches execute.