All files / common/utils validate-each.util.ts

92.31% Statements 12/13
100% Branches 6/6
75% Functions 3/4
91.67% Lines 11/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x       1x 1x   1x               1x             21x 5x   25x 16x 1x   15x    
export class InvalidDecoratorItemException extends Error {
  private readonly msg: string;
 
  constructor(decorator: string, item: string, context: string) {
    const message = `Invalid ${item} passed to ${decorator}() decorator (${context}).`;
    super(message);
 
    this.msg = message;
  }
 
  public what(): string {
    return this.msg;
  }
}
 
export function validateEach(
  context: { name: string },
  arr: any[],
  predicate: Function,
  decorator: string,
  item: string,
): boolean {
  if (!context || !context.name) {
    return true;
  }
  const errors = arr.some(str => !predicate(str));
  if (errors) {
    throw new InvalidDecoratorItemException(decorator, item, context.name);
  }
  return true;
}