Explainable and budget-limited JSON business rules for MoonBit
{
“id”: “student-coupon”,
“description”: “满 100 元的成年学生可以使用优惠券”,
“condition”: {
“and”: [
{“>=”: [{“var”: “user.age”}, 18]},
{“==”: [{“var”: “user.role”}, “student”]},
{“>=”: [{“var”: “order.total”}, 100]}
]
}
}{
“user”: { “age”: 20, “role”: “guest” },
“order”: { “total”: 128 }
}FAIL student-coupon
├─ PASS user.age >= 18
│ ├─ VALUE var(user.age) = 20
│ └─ VALUE literal = 18
├─ FAIL user.role == “student”
│ ├─ VALUE var(user.role) = “guest”
│ └─ VALUE literal = “student”
└─ SKIPPED order.total >= 100
└─ reason: and short-circuitedmoon add z2823253773-p/moonrules@0.2.0moon run --target native cmd/main check examples/coupon.rule.json --json
moon run --target native cmd/main eval examples/coupon.rule.json --data examples/coupon.data.json --json
cat rule.json | moon run --target native cmd/main check - --jsonpub(all) struct Diagnostic {
code : String
severity : DiagnosticSeverity
rule_path : String
message : String
suggestion : String
} derive(Eq, Debug)pub(all) struct EvaluationReport {
decision : Decision
trace : TraceNode
stats : ExecutionStats
} derive(Eq, Debug)pub(all) struct TraceNode {
rule_path : String
operator : String
status : TraceStatus
resolved_inputs : Array[ResolvedInput]
result : Json?
message : String
children : Array[TraceNode]
} derive(Eq, Debug)fn evaluate(rule : RuleDocument, data : Json, budget : Budget, trace_mode? : TraceMode) -> EvaluationReportfn evaluate_condition(condition : Expr, data : Json, budget : Budget, trace_mode? : TraceMode) -> EvaluationReportfn evaluate_json(rule_source : String, data_source : String, budget? : Budget, trace_mode? : TraceMode) -> Result[EvaluationReport, String]Install
Download zipExplainable and budget-limited JSON business rules for MoonBit