# All files / src/options ValidationErrorsFormatter.ts

7.69% Statements 1/13
0% Branches 0/2
100% Functions 0/0
8.33% Lines 1/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
    32
    33
    34
    35
    36
    37
    38

    1x

     import { ValidationError } from 'class-validator';

    export class ValidationErrorsFormatter {
        /**
         * @param validationErrors
         * @returns {string}
         */
        public static format (validationErrors: ValidationError[]): string {
            const errorsArray: string[] = [];

            for (const error of validationErrors) {
                errorsArray.push(ValidationErrorsFormatter.formatError(error));
            }

            return errorsArray.join('\n');
        }

        /**
         * @param validationError
         * @returns {string}
         */
        private static formatError (validationError: ValidationError): string {
            const constraints: {[type: string]: string} = validationError.constraints;

            let errorString: string = `\`${validationError.property}\` errors:\n`;

            for (const constraint in constraints) {
                if (!constraints.hasOwnProperty(constraint)) {
                    continue;
                }

                errorString += `    - ${constraints[constraint]}\n`;
            }

            return errorString;
        }
    }