1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | class ObjectMatcherRulePlugin {
|
12 | constructor(ruleProperty, dataProperty) {
|
13 | this.ruleProperty = ruleProperty;
|
14 | this.dataProperty = dataProperty || ruleProperty;
|
15 | }
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 | apply(ruleSetCompiler) {
|
22 | const { ruleProperty, dataProperty } = this;
|
23 | ruleSetCompiler.hooks.rule.tap(
|
24 | "ObjectMatcherRulePlugin",
|
25 | (path, rule, unhandledProperties, result) => {
|
26 | if (unhandledProperties.has(ruleProperty)) {
|
27 | unhandledProperties.delete(ruleProperty);
|
28 | const value = rule[ruleProperty];
|
29 | for (const property of Object.keys(value)) {
|
30 | const nestedDataProperties = property.split(".");
|
31 | const condition = ruleSetCompiler.compileCondition(
|
32 | `${path}.${ruleProperty}.${property}`,
|
33 | value[property]
|
34 | );
|
35 | result.conditions.push({
|
36 | property: [dataProperty, ...nestedDataProperties],
|
37 | matchWhenEmpty: condition.matchWhenEmpty,
|
38 | fn: condition.fn
|
39 | });
|
40 | }
|
41 | }
|
42 | }
|
43 | );
|
44 | }
|
45 | }
|
46 |
|
47 | module.exports = ObjectMatcherRulePlugin;
|