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
Copy file name to clipboardExpand all lines: README.md
+20-40Lines changed: 20 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,8 +88,8 @@ const actions = {
88
88
// validate using a JSON schema via AJV
89
89
constajv=newAjv();
90
90
constvalidator=async (subject, schema) => {
91
-
constvalidate=ajv.compile(schema);
92
-
constresult=awaitvalidate(subject);
91
+
constvalidate=awaitajv.compile(schema);
92
+
constresult=validate(subject);
93
93
return { result };
94
94
};
95
95
@@ -119,50 +119,30 @@ engine.run({
119
119
120
120
## Validator
121
121
122
-
The thing about `json-schema-rules-engine` is that you don't have to use JSON schema (but you are highly encouraged to!)
122
+
The validator is what makes `json-schema-rules-engine`so powerful. The validator is passed the resolved fact value and the schema (the value of the `is` property of an [`evaluator`]) and returns (optionally asynchronously) a `ValidatorResult`:
123
123
124
-
You **must** provide a validator when creating a rules engine. We haven't provided one in the interest of keeping this package unopinionated and small, but here's a great one to use:
124
+
```ts
125
+
typeValidatorResult= {
126
+
result:boolean;
127
+
};
128
+
```
129
+
130
+
If you want to use `json-schema-rules-engine` as was originally envisioned - to allow encoding of boolean logic by means of JSON Schema - then this is a great validator to use:
125
131
126
132
```js
127
133
importAjvfrom'Ajv';
128
134
constajv=newAjv();
129
135
constvalidator=async (subject, schema) => {
130
-
constvalidate=ajv.compile(schema);
131
-
constresult=awaitvalidate(subject);
136
+
constvalidate=awaitajv.compile(schema);
137
+
constresult=validate(subject);
132
138
return { result };
133
139
};
134
-
```
135
140
136
-
The validator must return an object with a `result` key that has a `boolean`. It can run async. It is used to evaluate a fact at runtime, and is passed the fact value and the schema (or otherwise serializable JSON) you have defined in your rules
137
-
138
-
```js
139
-
construle= {
140
-
myRule: {
141
-
when: [
142
-
{
143
-
firstName: {
144
-
is: {
145
-
type:'string',
146
-
pattern:'^Joe',
147
-
},
148
-
},
149
-
},
150
-
];
151
-
}
152
-
}
153
-
154
-
engine.run({firstName:'Bill'})
155
-
156
-
// the validator you provided is called like this:
157
-
const { result, ...rest } =validator(
158
-
'Bill',
159
-
{
160
-
type:'string',
161
-
pattern:'^Joe',
162
-
}
163
-
);
141
+
constengine=createRulesEngine(validator);
164
142
```
165
143
144
+
You can see by abstracting the JSON Schema part away from the core rules engine (by means of the `validator`) this engine can actually use **anything** to evaluate a property against. The validator is why `json-schema-rules-engine` is so small and so powerful.
145
+
166
146
### Context
167
147
168
148
`context` is the name of the object the rules engine evaluates during `run`. It can be used for interpolation or even as a source of facts
@@ -253,8 +233,8 @@ The `then` or `otherwise` property can consist of either `actions`, but it can a
253
233
constmyRule= {
254
234
when: [
255
235
{
236
+
id:'weatherCondition',
256
237
weather: {
257
-
name:'myWeatherFact',
258
238
params: {
259
239
query:'{{city}}',
260
240
appId:'{{apiKey}}',
@@ -274,7 +254,7 @@ const myRule = {
274
254
forecast: {
275
255
params: {
276
256
appId:'{{apiKey}}',
277
-
coord:'{{results.myWeatherFact.value.coord}}'// interpolate a value returned from the first fact
257
+
coord:'{{results.weatherCondition.weather.value.coord}}'// interpolate a value returned from the first fact
278
258
},
279
259
path:'daily',
280
260
is: {
@@ -320,7 +300,9 @@ const myRule = {
320
300
321
301
#### FactMap
322
302
323
-
A fact map is a plain object whose keys are facts (static or functional) and values are [`Evaluator`'s](#evaluator)
303
+
A fact map is a plain object whose keys are facts (static or functional) and values are [`Evaluator`'s](#evaluator).
304
+
305
+
NOTE: `id` is a reserved word in a `FactMap`. It is used internally to allow easy access to the results of a `FactMap` for interpolation in the `then` or `otherwise` clauses.
324
306
325
307
#### Evaluator
326
308
@@ -345,8 +327,6 @@ const myFactMap = {
345
327
};
346
328
```
347
329
348
-
You can also specify a `name` as a way to more easily interpolate the result from the
349
-
350
330
### Interpolation
351
331
352
332
Interpolation is configurable by passing the `pattern` option. By default, it uses [handlebars](https://handlebarsjs.com/)
0 commit comments