Skip to content

Commit 2edfb6c

Browse files
authored
fix(DEV-123): Improve error thrown by failed validations (#395)
* fix(DEV-123): Improve error thrown by failed validations * fix(DEV-123): Replace map with forEach in MapAndValidatePipe
1 parent d39d382 commit 2edfb6c

6 files changed

Lines changed: 60 additions & 30 deletions

File tree

package-lock.json

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lenne.tech/nest-server",
3-
"version": "10.8.8",
3+
"version": "10.8.9",
44
"description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
55
"keywords": [
66
"node",

spectaql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ servers:
1111
info:
1212
title: lT Nest Server
1313
description: Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).
14-
version: 10.8.8
14+
version: 10.8.9
1515
contact:
1616
name: lenne.Tech GmbH
1717
url: https://lenne.tech

src/core/common/decorators/common-error.decorator.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,32 @@ export const commonErrorSchema = {
1919
type: 'object',
2020
};
2121

22+
export const badRequestSchema = {
23+
properties: {
24+
message: { type: 'string' },
25+
name: { type: 'string' },
26+
options: {
27+
additionalProperties: true,
28+
type: 'object',
29+
},
30+
response: {
31+
additionalProperties: {
32+
additionalProperties: {
33+
type: 'string',
34+
},
35+
type: 'object',
36+
},
37+
type: 'object',
38+
},
39+
status: { type: 'number' },
40+
},
41+
type: 'object',
42+
};
43+
2244
export function ApiCommonErrorResponses() {
2345
return applyDecorators(
2446
ApiUnauthorizedResponse({ schema: commonErrorSchema }),
2547
ApiNotFoundResponse({ schema: commonErrorSchema }),
26-
ApiBadRequestResponse({ schema: commonErrorSchema }),
48+
ApiBadRequestResponse({ schema: badRequestSchema }),
2749
);
2850
}

src/core/common/pipes/map-and-validate.pipe.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export class MapAndValidatePipe implements PipeTransform {
2525
// Validate
2626
const errors = await validate(value, { forbidUnknownValues: false });
2727
if (errors.length > 0) {
28-
throw new BadRequestException(`Input validation failed:${errors.join('; ')}`);
28+
const result = {};
29+
errors.forEach((e) => {
30+
result[e.property] = e.constraints;
31+
});
32+
throw new BadRequestException(result);
2933
}
3034

3135
return value;

tests/project.e2e-spec.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,28 +239,32 @@ describe('Project (e2e)', () => {
239239
name: expect.any(String),
240240
options: expect.any(Object),
241241
response: {
242-
error: expect.any(String),
243-
message: expect.any(String),
244-
statusCode: expect.any(Number),
242+
email: {
243+
isEmail: expect.any(String),
244+
isNotEmpty: expect.any(String),
245+
},
246+
password: {
247+
isNotEmpty: expect.any(String),
248+
isString: expect.any(String),
249+
},
245250
},
246251
status: expect.any(Number),
247252
});
248253

249254
// Test for concrete values
250255
expect(res).toMatchObject({
251-
message: 'Input validation failed:An instance of CoreAuthSignInInput has failed the validation:\n'
252-
+ ' - property email has failed the following constraints: isNotEmpty, isEmail \n'
253-
+ '; An instance of CoreAuthSignInInput has failed the validation:\n'
254-
+ ' - property password has failed the following constraints: isString, isNotEmpty \n',
256+
message: 'Bad Request Exception',
255257
name: 'BadRequestException',
256258
options: {},
257259
response: {
258-
error: 'Bad Request',
259-
message: 'Input validation failed:An instance of CoreAuthSignInInput has failed the validation:\n'
260-
+ ' - property email has failed the following constraints: isNotEmpty, isEmail \n'
261-
+ '; An instance of CoreAuthSignInInput has failed the validation:\n'
262-
+ ' - property password has failed the following constraints: isString, isNotEmpty \n',
263-
statusCode: 400,
260+
email: {
261+
isEmail: 'email must be an email',
262+
isNotEmpty: 'email should not be empty',
263+
},
264+
password: {
265+
isNotEmpty: 'password should not be empty',
266+
isString: 'password must be a string',
267+
},
264268
},
265269
status: 400,
266270
});

0 commit comments

Comments
 (0)