-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMin.ts
27 lines (24 loc) · 897 Bytes
/
Min.ts
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
26
27
import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
import { Validator } from 'class-validator';
@Injectable()
export class Min implements PipeTransform<any> {
private readonly min: number;
private readonly message: string;
private readonly validator: Validator;
constructor(min: number, message?: string) {
this.message = message || '';
this.validator = new Validator();
this.min = min;
}
async transform(value: any, metadata: ArgumentMetadata) {
if (value === undefined) {
return value;
}
if (!this.validator.min(value, this.min)) {
const { data } = metadata;
const defaults = data ? `${data} is invalid` : 'Validation failed';
throw new BadRequestException(this.message || defaults);
}
return value;
}
}