Я использую пакет class-validator для проверки ссылки в типе ввода GraphQL. Проблема в том, что проверка не проходит, если ссылка содержит пробелы в конце входной строки. Есть ли способ обрезать его перед проверкой?
import { InputType, Field, Int } from 'type-graphql';
import { IsUrl, IsOptional } from 'class-validator';
import { Project } from '../entities';
@InputType()
export default class UpdateProjectInput implements Partial<Project> {
@Field(type => Int)
id: number;
@Field({ nullable: true })
@IsUrl({}, { message: 'Link is not a valid url' })
@IsOptional()
link?: string;
}
Пользовательский декоратор
export default function Transform(
cb: (value: any) => any
): (target: Object, propertyKey: string | symbol) => void {
return function (target: Object, propertyKey: string | symbol) {
Object.defineProperty(target, propertyKey, {
set(value) {
this.value = cb(value);
},
enumerable: true,
configurable: true,
});
};
}
И его использование
@InputType()
export default class UpdateProjectInput implements Partial<Project> {
@Field(type => Int)
id: number;
@Field({ nullable: true })
@Transform(value => value?.trim())
@IsUrl({}, { message: 'Link is not a valid url' })
@IsOptional()
link?: string;
}