-
Notifications
You must be signed in to change notification settings - Fork 512
Description
Is there an existing issue that is already proposing this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe it
As a practical development, to use a RegExp it is common that it is itself except in some global constant of the solution and then it is used in all applications.
The problem is that to export this RegExp, @ApiProperty({ pattern }) does not need to be converted to a string and then remove the initial and final bars as well.
// src/domain/constants/regex.ts
export const REGEX = {
ALIAS: /^[a-z0-9]+(-[a-z0-9]+)*$|^.{5,50}$/
}
// src/resources/organization/dtos/create-organization.dto.ts
import {ApiProperty} from '@nestjs/swagger';
import {REGEX} from '#/domain/constants';
export class CreateOrganizationDTO {
@ApiProperty({ pattern: REGEX.ALIAS.toString().slice(1, -1) })
alias: string
}
Describe the solution you'd like
It is a common behavior to specify converting the RegExp to string and removing the initial and final bars, and, as it is also possible to identify the type of the value attributed in a pattern using the options.pattern instanceof RegExp
, I believe that it is simple to implement the behavior of using RegExp in this field. A possible solution is a simple addition here:
// lib/decorators/api-property.decorator.ts
export function createApiPropertyDecorator(
options: ApiPropertyOptions = {},
overrideExisting = true
): PropertyDecorator {
if(options.pattern instanceof RegExp) {
options.pattern = options.pattern.toString().slice(1, -1);
}
const [type, isArray] = getTypeIsArrayTuple(options.type, options.isArray);
// ... rest of code here
Teachability, documentation, adoption, migration strategy
The developers will be define properties passing the pattern in ApiProperty decorator as RegExp directly without adjust like:
import {ApiProperty} from '@nestjs/swagger';
export class ExampleDTO {
@ApiProperty({ pattern: /^\w+$/ })
example: string
}
What is the motivation / use case for changing the behavior?
Simplify use an coding experience of @nestjs/swagger
package to developers