forked from toonvanstrijp/nestjs-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore (error-formatter): moved format methods into a separate file
- Loading branch information
1 parent
40b6f20
commit 9805d60
Showing
2 changed files
with
35 additions
and
39 deletions.
There are no files selected for viewing
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
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
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, | ||
}; | ||
}; |