mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 20:59:26 +00:00
add auth
This commit is contained in:
43
src/settings/pipes-setup.ts
Normal file
43
src/settings/pipes-setup.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
INestApplication,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { ValidationError } from 'class-validator';
|
||||
|
||||
export const validationErrorsMapper = {
|
||||
mapValidationErrorArrayToValidationPipeErrorTypeArray(
|
||||
errors: ValidationError[],
|
||||
): ValidationPipeErrorType[] {
|
||||
return errors.flatMap((error) => {
|
||||
const constraints = error.constraints ?? [];
|
||||
return Object.entries(constraints).map(([_, value]) => ({
|
||||
field: error.property,
|
||||
message: value,
|
||||
}));
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export function pipesSetup(app: INestApplication) {
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
transform: true,
|
||||
|
||||
stopAtFirstError: true,
|
||||
exceptionFactory: (errors: ValidationError[]) => {
|
||||
const err =
|
||||
validationErrorsMapper.mapValidationErrorArrayToValidationPipeErrorTypeArray(
|
||||
errors,
|
||||
);
|
||||
throw new BadRequestException(err);
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export type ValidationPipeErrorType = {
|
||||
field: string;
|
||||
message: string;
|
||||
};
|
||||
Reference in New Issue
Block a user