fix(rule): log and apply mutation when rule has > 0 diffs
This commit is contained in:
parent
3b2e421c0c
commit
63b6e48cc7
27
src/rule.ts
27
src/rule.ts
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue