如何使一组装饰器在Typescript中易于重用



TL;DR

如何将装饰器(来自库(组合为一个可重用的装饰器

问题

每当我的REST API收到请求时,它都会验证所提供的主体属性(使用class-validator库(。每条路由都有自己的专用验证类(在代码中称为Dtos((参见示例(

每个提供的主体属性都有几个验证规则,这些规则有时会变得非常复杂,其他工程师应该能够轻松地重用这些验证规则

示例

路线1:公司创建

POST - /api/company
>> Parameters: name, domain, size, contact
class CreateCompanyDto implements Dto {
@IsString({message: 'Must be text format'})
@MinLength(2, { message: "Must have at least 2 characters" })
@MaxLength(20, { message: "Can't be longer than 20 characters" })
@IsDefined({ message: 'Must specify a receiver' })
public name!: string;
@MaxLength(253, { message: "Can't be longer than 253 characters" })
@IsFQDN({}, {message: 'Must be a valid domain name'})
@IsDefined({ message: 'Must specify a domain' })
public domain!: string;
@MaxLength(30, { message: "Can't be longer than 30 characters" })
@IsString({message: 'Must be text format'})
@IsDefined({ message: 'Must specify a company size' })
public size!: string;
@IsPhoneNumber(null, {message: 'Must be a valid phone number'})
@IsDefined({ message: 'Must specify a phone number' })
public contact!: string;
}

路线2:公司更新

PUT - /api/company
>> Parameters: id, name, domain, size, contact
class UpdateCompanyDto implements Dto {
@IsUUID()
@IsDefined({ message: 'Must be defined' })
public id!: string;
@IsString({ message: 'Must be text format' })
@MinLength(2, { message: "Must have at least 2 characters" })
@MaxLength(20, { message: "Can't be longer than 20 characters" })
@IsOptional()
public name!: string;
@MaxLength(253, { message: "Can't be longer than 253 characters" })
@IsFQDN({}, { message: 'Must be a valid domain name' })
@IsOptional()
public domain!: string;
@MaxLength(30, { message: "Can't be longer than 30 characters" })
@IsString({ message: 'Must be text format' })
@IsOptional()
public size!: string;
@IsPhoneNumber(null, { message: 'Must be a valid phone number' })
@IsOptional()
public contact!: string;
}

我在寻找什么

正如您在示例中看到的,一个验证类需要使用另一个验证类别的属性并不罕见。

问题是,如果工程师在随机验证类内的属性中添加1个验证规则,其他验证类将不会动态更新。

问题:确保装饰器更改/添加后,其他验证类知道更新的最佳方法是什么。

有没有办法将它们组合成一个变量/装饰器?感谢任何打字大师的帮助!

可接受的结果:

class CreateCompanyDto implements Dto {
@IsCompanyName({required: true})
public name!: string;
@IsCompanyDomain({required: true})
public domain!: string;
@isCompanySize({required: true})
public size!: string;
@isCompanyContact({required: true})
public contact!: string;
}
class UpdateCompanyDto implements Dto {
@IsCompanyId({required: true})
public id!: string;
@IsCompanyName({required: false})
public name!: string;
@IsCompanyDomain({required: false})
public domain!: string;
@isCompanySize({required: false})
public size!: string;
@isCompanyContact({required: false})
public contact!: string;
}

由于装饰器的功能性质,您可以轻松地定义自己的装饰器工厂,只需调用所有必需的验证器:

export function IsCompanyName({ required }: { required: boolean }): PropertyDecorator {
return function (target: any,
propertyKey: string | symbol): void {
IsString({ message: 'Must be text format' })(target, propertyKey);
MinLength(2, { message: "Must have at least 2 characters" })(target, propertyKey);
MaxLength(20, { message: "Can't be longer than 20 characters" })(target, propertyKey);
if (required)
IsDefined({ message: 'Must specify a receiver' })(target, propertyKey);
else
IsOptional()(target, propertyKey);
}
}

游乐场

一家小型装饰工厂

export function ValidatorComposer(validators: PropertyDecorator[], name: string): (options: { required: boolean }) => PropertyDecorator {
return function ({ required }: { required: boolean }) {
return function (target: any,
propertyKey: string | symbol): void {
validators.forEach((validator) => validator(target, propertyKey));
if (required)
IsDefined({ message: 'Must specify a ' + name })(target, propertyKey);
else
IsOptional()(target, propertyKey);
}
}
}

游乐场

您可以通过定义自己的装饰器来链接装饰器。请注意,我以前没有使用class-validator库,所以您需要对此进行测试。但这就是它的样子:

在这里,我定义了一个名为IsCompanyName的Decorator,并让它调用您的示例中的验证Decorator。我定义了要传递给decorator的ValidationOptions接口。是否需要此opts参数取决于您。此外,如果未指定选项,则由您定义默认行为。在我的示例中,我将其设置为可选的,并使其表现为默认情况下是必需的。只有在定义了optsrequired === false的情况下,我才调用IsOptional装饰器。否则我不会称之为

interface ValidationOptions {
required?: boolean
}
function IsCompanyName(opts?: ValidationOptions) {
return function (target: any, propertyKey: string) {
IsString({ message: 'Must be text format' })(target, propertyKey);
MinLength(2, { message: "Must have at least 2 characters" })(target, propertyKey);
MaxLength(20, { message: "Can't be longer than 20 characters" })(target, propertyKey);
IsDefined({ message: 'Must specify a receiver' })(target, propertyKey);
if (opts && opts.required === false) {
IsOptional()(target, propertyKey);
}
}
}

相关内容

  • 没有找到相关文章

最新更新