@amirmarmul/waba-common
Version:

48 lines (47 loc) • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestValidationError = void 0;
const AppError_1 = require("../../core/errors/AppError");
class RequestValidationError extends AppError_1.AppError {
errors;
constructor(errors) {
super('Unprocessable Content', 422);
this.errors = errors;
}
serializeErrors() {
return this.errors
.flatMap((error) => this.mapChildrenToValidationErrors(error))
.filter((error) => !!error.constraints && !!Object.entries(error.constraints).length)
.flatMap((error) => {
return { message: Object.values(error.constraints).join(', '), field: error.property };
});
}
mapChildrenToValidationErrors(error, parentPath) {
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(item, parentPath));
}
return validationErrors;
}
prependConstraintsWithParentProp(error, parentPath) {
const constraints = {};
for (const key in error.constraints) {
constraints[key] = `${parentPath}.${error.constraints[key]}`;
}
return {
...error,
constraints,
};
}
}
exports.RequestValidationError = RequestValidationError;
exports.default = RequestValidationError;