I have used @Transform
decorator from class-transformer
to sanitize specific fields in user input.
import { IsString } from "class-validator";
import { Transform } from "class-transformer";
export class SearchRequestDto {
@Transform((data) => data.value && sanitizeRegex(data.value))
@IsString()
readonly term: string = '';
}
How can I create a new decorator to add this transformer to DTO more straight forward. Like this:
export class SearchRequestDto {
@Regex()
@IsString()
readonly term: string = '';
}
You can create a Custom Decorator that looks like this:
export const Regex = () => Transform((data) => data.value && sanitizeRegex(data.value))
And then use it the way you wanted:
export class SearchRequestDto {
@Regex()
@IsString()
readonly term: string = '';
}