Skip to content

Commit 111d8d8

Browse files
author
Mateus Garcia
committed
feat: validate pipe uuid
1 parent baa25d0 commit 111d8d8

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

libs/json-api-nestjs/src/lib/mixin/pipes/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import { BodyInputPatchPipe } from './body-input-patch/body-input-patch.pipe';
1010
import { ParseRelationshipNamePipe } from './parse-relationship-name/parse-relationship-name.pipe';
1111
import { BodyRelationshipPipe } from './body-relationship/body-relationship.pipe';
1212
import { BodyRelationshipPatchPipe } from './body-relationship-patch/body-relationship-patch.pipe';
13-
import { ConfigParam, Entity, PipeMixin } from '../../types';
13+
import { Entity, PipeMixin } from '../../types';
1414
import { nameIt } from '../../helper';
15+
import { IdSchemaPipe } from './query-schema/id-schema.pipe';
1516

1617
function factoryMixin(entity: Entity, connectionName: string, pipe: PipeMixin) {
1718
const entityName =
@@ -95,10 +96,6 @@ export function bodyRelationshipPatchPipeMixin(): PipeMixin {
9596
return BodyRelationshipPatchPipe;
9697
}
9798

98-
export function idPipeMixin(
99-
entity: Entity,
100-
connectionName: string,
101-
config: ConfigParam
102-
): PipeMixin {
103-
return config.pipeForId;
99+
export function idPipeMixin(entity: Entity, connectionName: string): PipeMixin {
100+
return factoryMixin(entity, connectionName, IdSchemaPipe);
104101
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { BadRequestException, Inject, PipeTransform } from '@nestjs/common';
2+
import { Repository } from 'typeorm';
3+
import AjvCall, { ValidateFunction } from 'ajv';
4+
5+
import { getEntityName } from '../../../helper';
6+
import {
7+
Entity,
8+
PipeMixin,
9+
QuerySchemaTypes,
10+
ValidationError,
11+
} from '../../../types';
12+
import { isNumberString, isUUID } from 'class-validator';
13+
14+
export class IdSchemaPipe implements PipeTransform {
15+
protected validateFunction: ValidateFunction<QuerySchemaTypes>;
16+
static inject(pip: PipeMixin): void {
17+
Inject(AjvCall)(pip, 'ajvCall', 1);
18+
}
19+
20+
constructor(
21+
protected repository: Repository<Entity>,
22+
protected ajvCall: AjvCall
23+
) {
24+
const schemaName = getEntityName(this.repository.target);
25+
this.validateFunction = this.ajvCall.getSchema(
26+
`inputQuerySchema-${schemaName}`
27+
);
28+
}
29+
30+
async transform(value: unknown): Promise<QuerySchemaTypes> {
31+
const resource = getEntityName(this.repository.target);
32+
const columns = this.repository.metadata.columns;
33+
34+
for (const column of columns) {
35+
if (column.propertyName === 'id') {
36+
const isUuuidColumn = column.generationStrategy === 'uuid';
37+
38+
const valid = isUuuidColumn ? isUUID(value) : isNumberString(value);
39+
40+
if (!valid) {
41+
const message = isUuuidColumn
42+
? `Id of resource ${resource} should be uuid`
43+
: `Id of resource ${resource} should be numeric string`;
44+
throw new BadRequestException(message);
45+
}
46+
}
47+
}
48+
49+
return value as QuerySchemaTypes;
50+
}
51+
}

0 commit comments

Comments
 (0)