import { Rule } from "json-rules-engine" ; import fs from "fs-extra" ; import { RuleCategory } from "./ruleCategory" ; /**
 * Holds rules
 */ export class RuleSet { constructor ( public rules ) { /**
         * Validate properties that are required for amf node diffs
         */ this . validateRuleProperties ( ) ; } param rulesPath static async init ( rulesPath ) { let ruleJsonArray ; try { ruleJsonArray = await fs . readJSON ( rulesPath ) ; } catch ( error ) { error . message = `Error parsing the rules file: ${ error . message }` ; throw error ; } if ( ! Array . isArray ( ruleJsonArray ) ) { throw new Error ( "Rules must be defined as a json array" ) ; } try { const rules = ruleJsonArray . map ( ( r ) => new Rule ( r ) ) ; return new RuleSet ( rules ) ; } catch ( error ) { error . message = `Error parsing the rule: ${ error . message }` ; throw error ; } } /**
     * Validate properties of the rule required for the Diff
     */ validateRuleProperties ( ) { this . rules . forEach ( ( r ) => { if ( ! r . name ) { throw new Error ( "Name is required for every rule" ) ; } if ( ! r . event . params ) { throw new Error ( `Params are required in rule: ${ r . name }` ) ; } if ( ! r . event . params . category ) { throw new Error ( `Category is required in rule: ${ r . name }` ) ; } if ( ! Object . values ( RuleCategory ) . includes ( r . event . params . category ) ) { throw new Error ( `Invalid category in rule: ${ r . name }` ) ; } if ( ( ! r . event . params . changedProperty || r . event . params . changedProperty . trim ( ) === "" ) && r . event . params . category !== RuleCategory . IGNORED ) { throw new Error ( `Changed property is required in rule: ${ r . name }` ) ; } } ) ; } /**
     * Returns true when there are rules
     */ hasRules ( ) { return this . rules != null && this . rules . length > 0 ; } }