Skip to content

Instantly share code, notes, and snippets.

@ivanzigoni
Last active February 26, 2025 17:19
Show Gist options
  • Save ivanzigoni/efda2e32b560df92773080b6d980f0bc to your computer and use it in GitHub Desktop.
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
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