-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIsUUID.ts
More file actions
25 lines (22 loc) · 822 Bytes
/
IsUUID.ts
File metadata and controls
25 lines (22 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
import { Validator } from 'class-validator';
@Injectable()
export class IsUUID implements PipeTransform<any> {
private readonly message: string;
private readonly validator: Validator;
constructor(message?: string) {
this.message = message || '';
this.validator = new Validator();
}
async transform(value: any, metadata: ArgumentMetadata) {
if (value === undefined) {
return value;
}
if (!this.validator.isUUID(value)) {
const { data } = metadata;
const defaults = data ? `${data} is invalid` : 'Validation failed';
throw new BadRequestException(this.message || defaults);
}
return value;
}
}