* Applies the plugin by registering its hooks on the compiler. * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler * @returns {void}
(ruleSetCompiler)
| 45 | * @returns {void} |
| 46 | */ |
| 47 | apply(ruleSetCompiler) { |
| 48 | const { ruleProperty, dataProperty } = this; |
| 49 | ruleSetCompiler.hooks.rule.tap( |
| 50 | PLUGIN_NAME, |
| 51 | (path, rule, unhandledProperties, result) => { |
| 52 | if (unhandledProperties.has(ruleProperty)) { |
| 53 | unhandledProperties.delete(ruleProperty); |
| 54 | const value = |
| 55 | /** @type {Record<string, RuleSetConditionOrConditions>} */ |
| 56 | (rule[ruleProperty]); |
| 57 | for (const property of Object.keys(value)) { |
| 58 | const nestedDataProperties = property.split("."); |
| 59 | const condition = ruleSetCompiler.compileCondition( |
| 60 | `${path}.${ruleProperty}.${property}`, |
| 61 | value[property] |
| 62 | ); |
| 63 | if (this.additionalConditionFunction) { |
| 64 | result.conditions.push({ |
| 65 | property: [dataProperty], |
| 66 | matchWhenEmpty: condition.matchWhenEmpty, |
| 67 | fn: this.additionalConditionFunction |
| 68 | }); |
| 69 | } |
| 70 | result.conditions.push({ |
| 71 | property: [dataProperty, ...nestedDataProperties], |
| 72 | matchWhenEmpty: condition.matchWhenEmpty, |
| 73 | fn: condition.fn |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | module.exports = ObjectMatcherRulePlugin; |