-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/crud-dto-only
- Loading branch information
Showing
13 changed files
with
186 additions
and
153 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
51 changes: 0 additions & 51 deletions
51
src/infrastructure/applications/rest/filters/ValidationExceptionFilterCustom.ts
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,39 +1,19 @@ | ||
import {ExceptionFilter, Catch, ArgumentsHost, HttpStatus} from '@nestjs/common'; | ||
import { Response } from 'express'; | ||
import {ValidationError} from 'class-validator'; | ||
import {ValidationException} from '../../usecases/exceptions/ValidationException'; | ||
import {ValidationException} from '../../usecases/exceptions'; | ||
|
||
@Catch(ValidationException) | ||
export class ValidationExceptionFilter implements ExceptionFilter { | ||
parseErrors(errors) { | ||
if (Array.isArray(errors) && errors[0] instanceof ValidationError) { | ||
return errors.reduce( | ||
(result: any, item) => { | ||
if (item.constraints) { | ||
result[item.property] = [].concat(Object.values(item.constraints)); | ||
} | ||
if (item.children?.length > 0) { | ||
result[item.property] = this.parseErrors(item.children); | ||
} | ||
return result; | ||
}, | ||
{}, | ||
); | ||
} | ||
return errors; | ||
} | ||
|
||
catch(exception: ValidationException, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
const errors = this.parseErrors(exception.errors); | ||
|
||
response | ||
// TODO#Warning - ставим код 200, чтобы форма на фронте принимала массив ошибок | ||
.status(HttpStatus.OK) | ||
.json({ | ||
statusCode: HttpStatus.BAD_REQUEST, | ||
errors, | ||
errors: exception.errors, | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import {ValidationError} from 'class-validator'; | ||
import {IFormError} from '../interfaces/IFormError'; | ||
import {IErrorsCompositeObject} from '../interfaces/IErrorsCompositeObject'; | ||
|
||
export class ValidationException { | ||
public errors; | ||
public errors: IErrorsCompositeObject; | ||
|
||
constructor(errors: ValidationError[] | IFormError) { | ||
constructor(errors: IErrorsCompositeObject) { | ||
this.errors = errors; | ||
} | ||
} |
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
Oops, something went wrong.