All files / app/errors invalid-request-error.ts

100% Statements 17/17
100% Branches 4/4
100% Functions 1/1
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 471x                                 61x 61x 60x   60x 60x   80x 2x   2x 7x     78x         60x     61x   61x       1x  
import BadRequestError from './bad-request-error';
import {Result} from 'express-validator';
 
/**
 * The error if a request has invalid attributes.
 */
class InvalidRequestError extends BadRequestError {
  /**
   * The result of the validation.
   */
  public validationResult: Result;
 
  /**
   * Creates an instance of this class.
   * @param validationResult - The result of the validation
   */
  constructor(validationResult: Result) {
    let message = '';
    if (!validationResult.isEmpty()) {
      message += 'The following fields are invalid: ';
      const resultArray: Array<Record<string, unknown>> =
          validationResult.array();
      for (let i = 0; i < resultArray.length; i++) {
        // Check if this item has nested errors, if it does, append them
        if (resultArray[i].nestedErrors) {
          const nested = resultArray[i].nestedErrors as
              Array<Record<string, unknown>>;
          for (let j = 0; j < nested.length; j++) {
            message += `${nested[j].param} -> ${nested[j].msg}, `;
          }
        } else {
          message += `${resultArray[i].param} -> ${resultArray[i].msg}, `;
        }
      }
 
      // Remove the last ', '
      message = message.substring(0, message.length - 2);
    }
 
    super(message, validationResult as unknown as Record<string, unknown>);
 
    this.validationResult = validationResult;
  }
}
 
export default InvalidRequestError;