Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit a65ad62

Browse files
committed
Major refactor and new GraphQL filter
1 parent 1281f72 commit a65ad62

File tree

8 files changed

+186
-78
lines changed

8 files changed

+186
-78
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"homepage": "https://github.com/exonest",
1414
"repository": {
1515
"type": "git",
16-
"url": "https://github.com/exonest/better-validation-pipe"
16+
"url": "https://github.com/exonest/better-validation"
1717
},
1818
"scripts": {
1919
"start": "tsdx watch",
@@ -36,16 +36,16 @@
3636
"singleQuote": true,
3737
"trailingComma": "es5"
3838
},
39-
"name": "@exonest/better-validation-pipe",
39+
"name": "@exonest/better-validation",
4040
"author": "Alireza Zamani",
41-
"module": "dist/better-validation-pipe.esm.js",
41+
"module": "dist/better-validation.esm.js",
4242
"size-limit": [
4343
{
44-
"path": "dist/better-validation-pipe.cjs.production.min.js",
44+
"path": "dist/better-validation.cjs.production.min.js",
4545
"limit": "10 KB"
4646
},
4747
{
48-
"path": "dist/better-validation-pipe.esm.js",
48+
"path": "dist/better-validation.esm.js",
4949
"limit": "10 KB"
5050
}
5151
],
@@ -72,6 +72,7 @@
7272
},
7373
"dependencies": {
7474
"@nestjs/common": "^8.2.0",
75+
"@nestjs/graphql": "^9.1.1",
7576
"class-transformer": "^0.4.0",
7677
"class-validator": "^0.13.1",
7778
"reflect-metadata": "^0.1.13"

src/error-transformer.spec.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/error-transformer.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/gql-validation.filter.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ArgumentsHost, Catch, BadRequestException } from '@nestjs/common';
2+
import { GqlContextType, GqlExceptionFilter } from '@nestjs/graphql';
3+
4+
@Catch(BadRequestException)
5+
export class GraphqlValidationFilter implements GqlExceptionFilter {
6+
catch(exception: BadRequestException, host: ArgumentsHost) {
7+
if (host.getType<GqlContextType>() === 'graphql') {
8+
// const gqlHost = GqlArgumentsHost.create(host);
9+
const response = exception.getResponse() as any;
10+
11+
if (
12+
Array.isArray(response.message) &&
13+
Array.isArray(response.message[0].errors) &&
14+
typeof response.message[0].field === 'string'
15+
) {
16+
return {
17+
userErrors: response.message.map((error: any) => ({
18+
field: error.path.split('.'),
19+
message: error.errors[0],
20+
})),
21+
};
22+
}
23+
}
24+
return exception;
25+
}
26+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './validation.pipe';
2+
export * from './gql-validation.filter';

src/validation.pipe.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@ import {
22
ValidationError,
33
ValidationPipe as OriginalValidationPipe,
44
} from '@nestjs/common';
5-
import { HttpErrorByCode } from '@nestjs/common/utils/http-error-by-code.util';
6-
import { transformErrors } from './error-transformer';
5+
import { iterate } from 'iterare';
76

87
export class ValidationPipe extends OriginalValidationPipe {
9-
public createExceptionFactory() {
10-
return (validationErrors: ValidationError[] = []) => {
11-
if (this.isDetailedOutputDisabled) {
12-
return new HttpErrorByCode[this.errorHttpStatusCode]();
13-
}
14-
const errors = transformErrors(validationErrors);
15-
return new HttpErrorByCode[this.errorHttpStatusCode](errors);
16-
};
8+
protected flattenValidationErrors(validationErrors: ValidationError[]) {
9+
return (iterate(validationErrors)
10+
.map(error => this.mapChildrenToValidationErrors(error))
11+
.flatten()
12+
.filter(item => !!item.constraints)
13+
.map(item => ({
14+
errors: Object.values(item.constraints || {}),
15+
path: (item as any).path || item.property,
16+
}))
17+
.filter(errorObject => errorObject.errors.length > 0)
18+
.flatten()
19+
.toArray() as unknown) as string[];
20+
}
21+
22+
protected prependConstraintsWithParentProp(
23+
parentPath: string,
24+
error: ValidationError
25+
): ValidationError {
26+
return ({
27+
path: `${parentPath}.${error.property}`,
28+
...super.prependConstraintsWithParentProp(parentPath, error),
29+
} as unknown) as ValidationError;
1730
}
1831
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
33
"include": ["src", "types"],
44
"compilerOptions": {
5+
"experimentalDecorators": true,
56
"target": "ES6",
67
"module": "esnext",
78
"lib": ["dom", "esnext"],

0 commit comments

Comments
 (0)