You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a rule is evaluated, it returns a `Result` struct containing:
78
72
79
73
-`Value`: The evaluated value, usually a boolean
80
-
- `Error`: Any fields required by the rule but not present in the input
81
-
- `EvaluatedRule`: The sub-rule that determined the returned value
74
+
-`Error`: Any evaluation errors such as fields missing from the KV map
75
+
-`EvaluatedRule`: The sub-rule that determined the returned value. Useful for debugging and understanding which part of a complex rule caused the result.
82
76
83
77
The Result also provides additional helper methods:
84
78
-`Pass()`: Returns true if the rule returns true/a non-zero value with no errors
85
79
-`Fail()`: Returns true if the rule returns false/a zero value with no errors
86
80
-`Ok()`: Returns true if the rule executed with no error
87
81
88
-
## Context
89
-
90
-
Rules are evaluated with a context input containing field values. `rulekit.KV` is an alias for `map[string]any`.
91
-
92
-
```go
93
-
ctx:= &rulekit.Ctx{
94
-
KV: rulekit.KV{
95
-
"domain": "example.com",
96
-
"port": 8080,
97
-
},
98
-
}
99
-
100
-
result:= rule.Eval(ctx)
101
-
```
102
-
103
82
## Supported Operators
104
83
105
84
| Operator | Alias | Description |
@@ -120,6 +99,8 @@ result := rule.Eval(ctx)
120
99
121
100
## Supported Types
122
101
102
+
### Basic values
103
+
123
104
| Type | Used As | Example | Description |
124
105
|------|---------|---------|-------------|
125
106
|**bool**| VALUE, FIELD |`true`| Valid values: `true`, `false`|
@@ -130,7 +111,97 @@ result := rule.Eval(ctx)
130
111
|**Hexadecimal string**| VALUE, FIELD |`12:34:56:78:ab` (MAC address), `504f5354` (hex string "POST") | A hexadecimal string, optionally separated by colons. |
131
112
|**Regex**| VALUE |`/example\.com$/`| A Go-style regular expression. Must be surrounded by forward slashes. May not be quoted with double quotes (otherwise it will be parsed as a string). Maps to Go type: `*regexp.Regexp`|
132
113
133
-
Arrays are also supported using a square bracket notiation. An array may contain mixed value types. For example: `field in [1.2.3.4, "domain.com"]`.
114
+
### Constructs
115
+
116
+
| Type | Used As | Example | Description |
117
+
|------|---------|---------|-------------|
118
+
|**Array**| VALUE |`[1, "string", true]`| An array of mixed value types. Can be used with most operators including `in` and `contains`. |
119
+
|**Function**| VALUE |`starts_with(url, "https://")`| A function call with optional arguments. Can be built-in or custom. |
120
+
|**Macro**| VALUE |`isValidRequest()`| A zero-argument function that encapsulates a predefined rule. |
121
+
122
+
## Macros
123
+
124
+
Macros can be used for complex or commonly-used rules. They are defined in the evaluation context:
125
+
126
+
```go
127
+
// create a macro
128
+
isInternalAPI, err:= rulekit.Parse(`domain matches /\.internal\.example\.com$/ or ip in 10.0.0.0/8`)
129
+
if err != nil { /* ... */ }
130
+
131
+
// create a rule that uses the macro
132
+
rule, err:= rulekit.Parse(`isInternalAPI() && user != "root"`)
133
+
if err != nil { /* ... */ }
134
+
135
+
// evaluate the rule, making sure to pass the macro in the eval context
136
+
result:= rule.Eval(&rulekit.Ctx{
137
+
Macros: map[string]rulekit.Rule{
138
+
"isInternalAPI": isInternalAPI,
139
+
},
140
+
KV: rulekit.KV{
141
+
"user": user,
142
+
// ...
143
+
},
144
+
})
145
+
```
146
+
147
+
## Functions
148
+
149
+
Functions can be called inside rules and used as value objects. Functions may accept zero or more arguments.
150
+
151
+
### Standard library
152
+
153
+
Rulekit comes with a built-in standard library of functions:
154
+
155
+
| Function | Description | Example |
156
+
|----------|-------------|---------|
157
+
|`starts_with(value, prefix)`| Checks if a value starts with the given prefix. Works with strings, numbers, and other types by converting them to strings. |`starts_with(url, "https://")`|
158
+
159
+
### Custom Functions
160
+
161
+
Custom functions may be used to extend Rulekit with additional functionality. Note that functions only have access to their arguments and do not have access to the context KV map. Rulekit will validate the function's arguments per the provided spec before executing the handler.
162
+
163
+
```go
164
+
// define a custom function
165
+
customFuncs:=map[string]*rulekit.Function{
166
+
"randomInt": {
167
+
Args: []rulekit.FunctionArg{
168
+
{Name: "min"},
169
+
{Name: "max"},
170
+
},
171
+
Eval: func(args map[string]any) rulekit.Result {
172
+
// use the rulekit.IndexFuncArg helper to retrieve args and validate types.
173
+
// rulekit.IndexFuncArg[any] will skip type validation.
0 commit comments