@trendyol/eslint-plugin-nest-swagger-checker-lint
v1.0.0
Published
Eslint Plugin To Check Swagger Related Fields For NestJs
Downloads
9
Readme
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: 💡 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. 💡 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); }
💡 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: 💡 You can enable/disable this checking by setting
scopes.endpoint.query.description.check
config value and you can set pattern by settingscopes.endpoint.query.description.pattern
value.