Skip to content

Commit

Permalink
chore (error-formatter): moved format methods into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchDorrestijn committed May 27, 2022
1 parent 40b6f20 commit 9805d60
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
41 changes: 2 additions & 39 deletions src/filters/i18n-validation-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
I18nValidationExceptionFilterErrorFormatterOption,
} from 'src/interfaces/i18n-validation-exception-filter.interface';
import { Either } from 'src/types/either.type';
import { mapChildrenToValidationErrors } from 'src/utils/format';
import { I18nContext } from '../i18n.context';
import {
I18nValidationError,
Expand Down Expand Up @@ -81,49 +82,11 @@ export class I18nValidationExceptionFilter implements ExceptionFilter {
validationErrors: ValidationError[],
): string[] {
return iterate(validationErrors)
.map((error) => this.mapChildrenToValidationErrors(error))
.map((error) => mapChildrenToValidationErrors(error))
.flatten()
.filter((item) => !!item.constraints)
.map((item) => Object.values(item.constraints))
.flatten()
.toArray();
}

protected mapChildrenToValidationErrors(
error: ValidationError,
parentPath?: string,
): ValidationError[] {
if (!(error.children && error.children.length)) {
return [error];
}
const validationErrors = [];
parentPath = parentPath
? `${parentPath}.${error.property}`
: error.property;
for (const item of error.children) {
if (item.children && item.children.length) {
validationErrors.push(
...this.mapChildrenToValidationErrors(item, parentPath),
);
}
validationErrors.push(
this.prependConstraintsWithParentProp(parentPath, item),
);
}
return validationErrors;
}

protected prependConstraintsWithParentProp(
parentPath: string,
error: ValidationError,
): ValidationError {
const constraints = {};
for (const key in error.constraints) {
constraints[key] = `${parentPath}.${error.constraints[key]}`;
}
return {
...error,
constraints,
};
}
}
33 changes: 33 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ValidationError } from '@nestjs/common';

export const mapChildrenToValidationErrors = (
error: ValidationError,
parentPath?: string,
): ValidationError[] => {
if (!(error.children && error.children.length)) {
return [error];
}
const validationErrors = [];
parentPath = parentPath ? `${parentPath}.${error.property}` : error.property;
for (const item of error.children) {
if (item.children && item.children.length) {
validationErrors.push(...mapChildrenToValidationErrors(item, parentPath));
}
validationErrors.push(prependConstraintsWithParentProp(parentPath, item));
}
return validationErrors;
};

const prependConstraintsWithParentProp = (
parentPath: string,
error: ValidationError,
): ValidationError => {
const constraints = {};
for (const key in error.constraints) {
constraints[key] = `${parentPath}.${error.constraints[key]}`;
}
return {
...error,
constraints,
};
};

0 comments on commit 9805d60

Please sign in to comment.