1
0
Fork 0

fix(rule): log and apply mutation when rule has > 0 diffs

This commit is contained in:
ssube 2019-11-01 05:46:31 -05:00
parent 3b2e421c0c
commit 63b6e48cc7
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
2 changed files with 14 additions and 15 deletions

View File

@ -6,7 +6,7 @@ import { LogLevel } from 'noicejs';
import { YamlParser } from './parser/YamlParser';
import { readFileSync } from './source';
import { ensureArray, isNilOrEmpty } from './utils';
import { ensureArray, hasItems } from './utils';
import { friendlyError } from './utils/ajv';
import { Visitor } from './visitor';
import { VisitorContext } from './visitor/VisitorContext';
@ -198,21 +198,20 @@ export async function visitRules(ctx: VisitorContext, rules: Array<Rule>, data:
}
const itemDiff = diff(item, itemCopy);
if (isNilOrEmpty(itemDiff)) {
if (hasItems(itemDiff)) {
ctx.logger.info({
diff: itemDiff,
item,
rule: rule.name,
}, 'rule passed with modifications');
if (ctx.innerOptions.mutate) {
applyDiff(item, itemCopy);
}
} else {
ctx.logger.info({ rule: rule.name }, 'rule passed');
continue;
}
ctx.logger.info({
diff: itemDiff,
item,
rule: rule.name,
}, 'rule passed with modifications');
if (ctx.innerOptions.mutate) {
applyDiff(item, itemCopy);
}
}
}
}
return ctx;

View File

@ -1,6 +1,6 @@
import { isNil } from 'lodash';
export function isNilOrEmpty(val: Array<unknown> | null | undefined): val is Array<unknown> {
export function hasItems(val: Array<unknown> | null | undefined): val is Array<unknown> {
return (Array.isArray(val) && val.length > 0);
}