-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotEquals.ts
27 lines (24 loc) · 938 Bytes
/
NotEquals.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 NotEquals implements PipeTransform<any> {
private readonly message: string;
private readonly comparison: any;
private readonly validator: Validator;
constructor(comparison: any, message?: string) {
this.message = message || '';
this.comparison = comparison;
this.validator = new Validator();
}
async transform(value: any, metadata: ArgumentMetadata) {
if (value === undefined) {
return value;
}
if (!this.validator.notEquals(value, this.comparison)) {
const { data } = metadata;
const defaults = data ? `${data} is invalid` : 'Validation failed';
throw new BadRequestException(this.message || defaults);
}
return value;
}
}