jsonlogic-validator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJSONLogic Validator
JSONLogic 验证器
Implement and validate JSONLogic rules—portable, JSON-serializable business logic that works across JavaScript, Python, PHP, Ruby, Go, Java, .Net, and C++.
实现并验证JSONLogic规则——一种可移植、支持JSON序列化的业务逻辑,可在JavaScript、Python、PHP、Ruby、Go、Java、.Net和C++中运行。
Core Syntax
核心语法
Every rule:
{ "operator": [arguments] }json
{"==": [1, 1]} // comparison
{"var": "user.name"} // data access
{"and": [{">=": [{"var": "age"}, 18]}, {"var": "active"}]} // compound每条规则格式:
{ "operator": [arguments] }json
{"==": [1, 1]} // 比较
{"var": "user.name"} // 数据访问
{"and": [{">=": [{"var": "age"}, 18]}, {"var": "active"}]} // 复合逻辑Quick Reference
快速参考
| Category | Operators |
|---|---|
| Data | |
| Logic | |
| Numeric | |
| Array | |
| String | |
| 类别 | 运算符 |
|---|---|
| 数据 | |
| 逻辑 | |
| 数值 | |
| 数组 | |
| 字符串 | |
Workflow
工作流程
1. Gather Requirements
1. 收集需求
Clarify:
- What data fields are available?
- What conditions determine the outcome?
- What should the rule return (boolean, value, category)?
明确:
- 可用的数据字段有哪些?
- 哪些条件决定结果?
- 规则应返回什么(布尔值、具体值、分类)?
2. Write the Rule
2. 编写规则
Build incrementally from inner expressions outward:
json
// Requirement: "Premium users over 25 get 20% discount, others get 10%"
{
"if": [
{"and": [
{"==": [{"var": "tier"}, "premium"]},
{">": [{"var": "age"}, 25]}
]},
0.20,
0.10
]
}从内部表达式开始逐步构建:
json
// 需求:"Premium用户年满25岁可享受20%折扣,其他用户享受10%折扣"
{
"if": [
{"and": [
{"==": [{"var": "tier"}, "premium"]},
{">": [{"var": "age"}, 25]}
]},
0.20,
0.10
]
}3. Validate
3. 验证
Run the validation script (no dependencies required):
bash
node scripts/validate-jsonlogic.js '<rule>' '<data>'Example:
bash
node scripts/validate-jsonlogic.js '{">=": [{"var": "age"}, 18]}' '{"age": 25}'运行验证脚本(无需依赖):
bash
node scripts/validate-jsonlogic.js '<rule>' '<data>'示例:
bash
node scripts/validate-jsonlogic.js '{"<=": [{"var": "age"}, 18]}' '{"age": 25}'4. Test with Cases
4. 用测试用例测试
Requires:
npm install json-logic-jsbash
node scripts/test-jsonlogic.js '<rule>' --test-cases '[
{"data": {"age": 25}, "expected": true},
{"data": {"age": 15}, "expected": false}
]'需要先安装:
npm install json-logic-jsbash
node scripts/test-jsonlogic.js '<rule>' --test-cases '[
{"data": {"age": 25}, "expected": true},
{"data": {"age": 15}, "expected": false}
]'Common Patterns
常见模式
Null-safe access with default
带默认值的空安全访问
json
{"var": ["user.name", "Guest"]}json
{"var": ["user.name", "Guest"]}Range check (between)
范围检查(介于两者之间)
json
{"<=": [0, {"var": "value"}, 100]}json
{"<=": [0, {"var": "value"}, 100]}Multi-condition (all must pass)
多条件(全部必须满足)
json
{"and": [
{">=": [{"var": "age"}, 18]},
{"==": [{"var": "status"}, "active"]},
{"in": [{"var": "role"}, ["admin", "editor"]]}
]}json
{"and": [
{">=": [{"var": "age"}, 18]},
{"==": [{"var": "status"}, "active"]},
{"in": [{"var": "role"}, ["admin", "editor"]]}
]}Grade/tier classification
等级/层级分类
json
{"if": [
{">=": [{"var": "score"}, 90]}, "A",
{">=": [{"var": "score"}, 80]}, "B",
{">=": [{"var": "score"}, 70]}, "C",
"F"
]}json
{"if": [
{">=": [{"var": "score"}, 90]}, "A",
{">=": [{"var": "score"}, 80]}, "B",
{">=": [{"var": "score"}, 70]}, "C",
"F"
]}Array operations
数组操作
json
// Sum: reduce with +
{"reduce": [{"var": "items"}, {"+": [{"var": "accumulator"}, {"var": "current"}]}, 0]}
// Filter: keep matching
{"filter": [{"var": "items"}, {">": [{"var": ""}, 10]}]}
// Any match: some
{"some": [{"var": "items"}, {"==": [{"var": ".status"}, "error"]}]}json
// 求和:用reduce和+
{"reduce": [{"var": "items"}, {"+": [{"var": "accumulator"}, {"var": "current"}]}, 0]}
// 过滤:保留匹配项
{"filter": [{"var": "items"}, {">": [{"var": ""}, 10]}]}
// 任意匹配:some
{"some": [{"var": "items"}, {"==": [{"var": ".status"}, "error"]}]}Truthy/Falsy (differs from JavaScript)
真值/假值(与JavaScript不同)
Falsy: , , ,
Truthy: Everything else (including and )
0[]""null"0"[0]假值:, , ,
真值:其他所有值(包括和)
0[]""null"0"[0]Validation Checklist
验证检查清单
Before deploying a rule:
- Valid JSON syntax
- All operators spelled correctly
- Arguments in arrays where required
- Data paths match actual data structure
- Edge cases handled (null, empty, missing keys)
- Tested with representative data samples
部署规则前:
- 有效的JSON语法
- 所有运算符拼写正确
- 参数在要求的数组中
- 数据路径与实际数据结构匹配
- 处理了边缘情况(空值、空内容、缺失键)
- 用代表性数据样本测试过
References
参考资料
For complete operator documentation with all arguments and edge cases:
- See
references/jsonlogic-reference.md
如需包含所有参数和边缘情况的完整运算符文档:
- 查看
references/jsonlogic-reference.md