diff --git a/src/rule/SchemaRule.ts b/src/rule/SchemaRule.ts index 64b82cf..d636dfa 100644 --- a/src/rule/SchemaRule.ts +++ b/src/rule/SchemaRule.ts @@ -2,7 +2,7 @@ import { ValidateFunction } from 'ajv'; import { cloneDeep, defaultTo, isNil } from 'lodash'; import { LogLevel } from 'noicejs'; -import { RuleData } from '.'; +import { Rule, RuleData } from '.'; import { hasItems } from '../utils'; import { friendlyError } from '../utils/ajv'; import { Visitor } from '../visitor'; @@ -10,7 +10,7 @@ import { VisitorContext } from '../visitor/VisitorContext'; import { VisitorError } from '../visitor/VisitorError'; import { VisitorResult } from '../visitor/VisitorResult'; -export class SchemaRule implements RuleData, Visitor { +export class SchemaRule implements Rule, RuleData, Visitor { public readonly check: ValidateFunction; public readonly desc: string; public readonly filter?: ValidateFunction; diff --git a/src/rule/index.ts b/src/rule/index.ts index ab5f7ec..14c30e4 100644 --- a/src/rule/index.ts +++ b/src/rule/index.ts @@ -1,3 +1,4 @@ +import { ValidateFunction } from 'ajv'; import { applyDiff, diff } from 'deep-diff'; import { cloneDeep, Dictionary, intersection, isNil } from 'lodash'; import { LogLevel } from 'noicejs'; @@ -7,10 +8,10 @@ import { YamlParser } from '../parser/YamlParser'; import { readDir, readFile } from '../source'; import { ensureArray, hasItems } from '../utils'; import { VisitorContext } from '../visitor/VisitorContext'; +import { VisitorResult } from '../visitor/VisitorResult'; import { SchemaRule } from './SchemaRule'; /* tslint:disable:no-any */ - export interface RuleData { // metadata desc: string; @@ -22,6 +23,21 @@ export interface RuleData { filter?: any; select: string; } +/* tslint:enable:no-any */ + +export type Validator = ValidateFunction; +export interface Rule { + check: Validator; + desc?: string; + filter?: Validator; + level: LogLevel; + name: string; + select: string; + tags: Array; + + pick(ctx: VisitorContext, root: any): Promise>; + visit(ctx: VisitorContext, item: any): Promise; +} /** * Rule selector derived from arguments. @@ -57,7 +73,7 @@ export interface RuleSourceData { export interface RuleSourceModule { definitions?: Dictionary; name: string; - rules: Array; + rules: Array; } export function createRuleSelector(options: Partial): RuleSelector { @@ -79,7 +95,7 @@ export function createRuleSources(options: Partial): RuleSources { }; } -export async function loadRuleFiles(paths: Array, ctx: VisitorContext): Promise> { +export async function loadRuleFiles(paths: Array, ctx: VisitorContext): Promise> { const parser = new YamlParser(); const rules = []; @@ -102,7 +118,7 @@ export async function loadRuleFiles(paths: Array, ctx: VisitorContext): return rules; } -export async function loadRulePaths(paths: Array, ctx: VisitorContext): Promise> { +export async function loadRulePaths(paths: Array, ctx: VisitorContext): Promise> { const rules = []; for (const path of paths) { @@ -119,7 +135,7 @@ export async function loadRulePaths(paths: Array, ctx: VisitorContext): return rules; } -export async function loadRuleModules(modules: Array, ctx: VisitorContext): Promise> { +export async function loadRuleModules(modules: Array, ctx: VisitorContext): Promise> { const rules = []; for (const name of modules) { @@ -140,7 +156,7 @@ export async function loadRuleModules(modules: Array, ctx: VisitorContex return rules; } -export async function loadRules(sources: RuleSources, ctx: VisitorContext): Promise> { +export async function loadRules(sources: RuleSources, ctx: VisitorContext): Promise> { return [ ...await loadRuleFiles(sources.ruleFile, ctx), ...await loadRulePaths(sources.rulePath, ctx), @@ -148,8 +164,8 @@ export async function loadRules(sources: RuleSources, ctx: VisitorContext): Prom ]; } -export async function resolveRules(rules: Array, selector: RuleSelector): Promise> { - const activeRules = new Set(); +export async function resolveRules(rules: Array, selector: RuleSelector): Promise> { + const activeRules = new Set(); for (const r of rules) { if (selector.excludeLevel.includes(r.level)) { @@ -182,7 +198,7 @@ export async function resolveRules(rules: Array, selector: RuleSelec return Array.from(activeRules); } -export async function visitRules(ctx: VisitorContext, rules: Array, data: any): Promise { +export async function visitRules(ctx: VisitorContext, rules: Array, data: any): Promise { for (const rule of rules) { const items = await rule.pick(ctx, data); for (const item of items) {