1
0
Fork 0

fix(visitor): handle missing errors gracefully

This commit is contained in:
ssube 2019-11-02 11:33:40 -05:00 committed by Sean Sube
parent a3b8341b75
commit 97256855ac
2 changed files with 11 additions and 2 deletions

View File

@ -189,7 +189,7 @@ export async function visitRules(ctx: VisitorContext, rules: Array<SchemaRule>,
const itemResult = cloneDeep(item);
const ruleResult = await rule.visit(ctx, itemResult);
if (ruleResult.errors.length > 0) {
if (hasItems(ruleResult.errors)) {
ctx.logger.warn({ count: ruleResult.errors.length, rule }, 'rule failed');
ctx.mergeResult(ruleResult);
continue;

View File

@ -4,7 +4,16 @@ export function doesExist<T>(val: T | null | undefined): val is T {
return !isNil(val);
}
export function hasItems<T>(val: Array<T> | null | undefined): val is Array<T> {
/**
* Test if a value is an array with some items (length > 0).
*
* This is not a general replacement for `.length > 0`, since it is also a typeguard:
* `if (hasItems(val)) else { val }` will complain that `val` is `never` in the `else`
* branch, since it was proven not to be an array by this function.
*/
export function hasItems<T>(val: Array<T> | null | undefined): val is Array<T>;
export function hasItems<T>(val: ReadonlyArray<T> | null | undefined): val is ReadonlyArray<T>;
export function hasItems<T>(val: ReadonlyArray<T> | null | undefined): val is ReadonlyArray<T> {
return (Array.isArray(val) && val.length > 0);
}