Last active
February 26, 2025 17:19
-
-
Save ivanzigoni/efda2e32b560df92773080b6d980f0bc to your computer and use it in GitHub Desktop.
function for extracting validation error messages recursively from the class-validator library ValidationError array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formatClassValidatorError( | |
validationErrors: ValidationError[], | |
accumulator: string[] = [], | |
): string[] { | |
for (const error of validationErrors) { | |
if (!error.children.length && !Object.keys(error.constraints).length) { | |
return accumulator; | |
} | |
if (error.children.length) { | |
return this.formatClassValidatorError(error.children, accumulator); | |
} | |
accumulator.push(...Object.values(error.constraints)); | |
} | |
return accumulator; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment