Socket
Book a DemoInstallSign in
Socket

@trendyol/eslint-plugin-nest-swagger-checker-lint

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trendyol/eslint-plugin-nest-swagger-checker-lint

Eslint Plugin To Check Swagger Related Fields For NestJs

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
0
Created
Source

nest-swagger-checker-linter

nest-swagger-checker-linter is linter version of nest-swagger-checker package to use it as ESLint rule.

This package uses nsc(nest-swagger-checker) package inside its own.

configuration

Like nsc package, this linter package can be configured by using .swautomaterc file at the root path of project. You can see detail of configuration

setup

Like other eslint plugins, this plugin needs to be added to eslintrc configuration.

// .eslintrc.json file in project
...
"plugins": [
  "nest-swagger-checker-lint"
],
...
"rules": {
  "nest-swagger-checker-lint/api-property-rule": "error",
  "nest-swagger-checker-lint/api-information-rule": "error",
  "nest-swagger-checker-lint/api-param-rule": "error",
....
}

examples

  • Lets assume you have endpoint method in a controller like below:

    @Post('')
    @HttpCode(HttpStatus.OK)
    ..
    @AnotherDecorator()
    public async create(
      ...
    )
    

    You will get eslint error because there is no ApiOperation decorator ta describe summary and description for endpoint.

    Error should be like below: Screenshot 2024-05-19 at 17 28 19

    💡 You can enable/disable this checking by setting scopes.endpoint.information.check config value

  • There is a request param for endpoint method in below example but name in ApiParam decorator does not match with it.

    ....
    @ApiParam({ name: 'customerId', description: 'Customer ID', type: Number, example: 60 })
    async getCount(@Param('userId') userId: string, @Query() parameters: Parameters): Promise<Count> {
      return this.service.getCount(Number(userId), parameters);
    }
    ...
    

    You will get eslint error like below. Screenshot 2024-05-20 at 23 29 52

    💡 You can enable/disable this checking by setting scopes.endpoint.params.check config value

  • If you have request parameter that given with @Param decorator like above but you don't have any @ApiParam decorator, you will also get eslint error.

    ....
    @ApiResponse({ status: HttpStatus.BAD_REQUEST, description: 'Request Is Not Valid' })
    @ApiResponse({ status: HttpStatus.INTERNAL_SERVER_ERROR, description: 'An Error Occurred' })
    async getCount(@Param('userId') userId: string, @Query() parameters: Paramters): Promise<Count> {
      return this.affiliateAdService.getCount(Number(userId), searchParameters);
    }
    

    Screenshot 2024-05-20 at 23 33 42

    💡 You can enable/disable this checking by setting scopes.endpoint.params.check config value

  • Lets assume you have a custom query class type for your endpoint endpoint method like below:

     async getCount(..., @Query() parameters: Parameters): Promise<Count> {
      return this.service.getCount(Number(sellerId), parameters);
    }
    ....
    export class SearchParametersDto {
    @IsInt()
    @IsOptional()
    @IsPositive()
    @Type(() => Number)
    @ApiProperty({
      type: Number,
      description: 'Pagination Index, Default: 1',
      example: 1,
      required: false,
    })
    public pageIndex?: number;
    ....
    

    If you gave pattern something like that ^[A-Z][a-z0-9]*(?:\s[a-z0-9]*)*$, you will give eslint error because your description text should be match with your pattern.

    In this example, only first words first letter should be uppercase according to example pattern. Eslint error will be like below: Screenshot 2024-05-20 at 23 50 40

    💡 You can enable/disable this checking by setting scopes.endpoint.query.description.check config value and you can set pattern by setting scopes.endpoint.query.description.pattern value.

FAQs

Package last updated on 27 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts