8000 Allow to pass RegExp to @ApiProperty({ pattern }) · Issue #3374 · nestjs/swagger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow to pass RegExp to @ApiProperty({ pattern }) #3374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? 8000 Sign in to your account

Open
1 task done
leandroluk opened this issue Apr 1, 2025 · 0 comments
Open
1 task done

Allow to pass RegExp to @ApiProperty({ pattern }) #3374

leandroluk opened this issue Apr 1, 2025 · 0 comments

Comments

@leandroluk
Copy link
leandroluk commented Apr 1, 2025

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: 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

@leandroluk leandroluk changed the title Allow to pass RegExp to @ApiProperty({ pattern }) property Allow to pass RegExp to @ApiProperty({ pattern }) Apr 1, 2025
@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/nest Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant
0