Skip to content

Commit

Permalink
Merge pull request #22 from natura-cosmeticos/feature/handler-slug-case
Browse files Browse the repository at this point in the history
feat(handler): added new rule and its handler for resources in paths not using spinal case
  • Loading branch information
robertLichtnow authored Jan 28, 2020
2 parents 2fdba31 + 59b48d7 commit 3c58380
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ These are the rules checked by the linter
* Checks for missing `/domain/context` on server url, defaults to `true`
*/
"must-contain-domain-and-context": boolean;
/**
* Checks for resources not using spinal case
*/
"resource-spinal-case"?: boolean;
}
```
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"npm": "^6.13.6",
"openapi-types": "^1.3.5",
"pluralize": "^8.0.0",
"slugify": "^1.3.6",
"swagger-parser": "^8.0.4",
"typescript": "^3.7.4",
"url-parse": "^1.4.7",
Expand Down
59 changes: 59 additions & 0 deletions src/__tests__/data/with-spinal-case/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
servers:
- url: 'https://api.geodatasource.com'
info:
contact:
x-twitter: _geodatasource
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.'
title: GeoDataSource Location Search
version: '1.0'
x-apisguru-categories:
- location
x-logo:
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png'
x-origin:
- converter:
url: 'https://github.com/lucybot/api-spec-converter'
version: 2.7.31
format: openapi
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml'
version: '3.0'
x-preferred: true
x-providerName: geodatasource.com
paths:
/city-with-spinal-case:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
'200':
content:
application/json; charset=utf-8:
examples:
'0':
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
95 changes: 95 additions & 0 deletions src/__tests__/data/without-spinal-case-once-two-path/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
openapi: 3.0.1
servers:
- url: "https://api.geodatasource.com/v1/abc/123/test"
info:
contact:
x-twitter: _geodatasource
description: "GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information."
title: GeoDataSource Location Search
version: "1.0"
x-apisguru-categories:
- location
x-logo:
url: "https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png"
x-origin:
- converter:
url: "https://github.com/lucybot/api-spec-converter"
version: 2.7.31
format: openapi
url: "https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml"
version: "3.0"
x-preferred: true
x-providerName: geodatasource.com
paths:
/pathWithoutSpinalCase:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
"200":
content:
application/json; charset=utf-8:
examples:
"0":
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
/anotherPathWithoutSpinalCase:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
"200":
content:
application/json; charset=utf-8:
examples:
"0":
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
59 changes: 59 additions & 0 deletions src/__tests__/data/without-spinal-case-twice-one-path/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
servers:
- url: 'https://api.geodatasource.com/v1/abc/123/test'
info:
contact:
x-twitter: _geodatasource
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.'
title: GeoDataSource Location Search
version: '1.0'
x-apisguru-categories:
- location
x-logo:
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png'
x-origin:
- converter:
url: 'https://github.com/lucybot/api-spec-converter'
version: 2.7.31
format: openapi
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml'
version: '3.0'
x-preferred: true
x-providerName: geodatasource.com
paths:
/pathWithoutSpinalCase/anotherPathWithoutSpinalCase:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
'200':
content:
application/json; charset=utf-8:
examples:
'0':
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
59 changes: 59 additions & 0 deletions src/__tests__/data/without-spinal-case/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
servers:
- url: 'https://api.geodatasource.com/v1/abc/123/test'
info:
contact:
x-twitter: _geodatasource
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.'
title: GeoDataSource Location Search
version: '1.0'
x-apisguru-categories:
- location
x-logo:
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png'
x-origin:
- converter:
url: 'https://github.com/lucybot/api-spec-converter'
version: 2.7.31
format: openapi
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml'
version: '3.0'
x-preferred: true
x-providerName: geodatasource.com
paths:
/pathWithoutSpinalCase:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
'200':
content:
application/json; charset=utf-8:
examples:
'0':
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
67 changes: 67 additions & 0 deletions src/__tests__/rules/handlers/resource-spinal-case.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import path from 'path';
import { parse } from '../../../index';
import { resourceSpinalCase } from '../../../rules/handlers/resource-spinal-case';
import { RuleFault } from '../../../rules/rule-fault';

describe('resourceSpinalCase function', () => {
const apiWithoutErrors = path.join(__dirname, '..', '..', 'data', 'openapi-3.0', 'swagger.yml');
const apiWithSpinalCase = path.join(__dirname, '..', '..', 'data', 'with-spinal-case', 'swagger.yml');
const apiWithOneFaultAndOneError = path.join(__dirname, '..', '..', 'data', 'without-spinal-case', 'swagger.yml');
const apiWithOneFaultAndTwoErrors = path.join(__dirname, '..', '..', 'data', 'without-spinal-case-twice-one-path', 'swagger.yml');
const apiWithTwoFaultsOneErrorEach = path.join(__dirname, '..', '..', 'data', 'without-spinal-case-once-two-path', 'swagger.yml');


it('should have no faults', async () => {
const faults: RuleFault[] = [];

const api = await parse(apiWithoutErrors);

resourceSpinalCase(api, faults);

expect(faults.length).toBe(0);
});

it('should have no faults with spinal case', async () => {
const faults: RuleFault[] = [];

const api = await parse(apiWithSpinalCase);

resourceSpinalCase(api, faults);

expect(faults.length).toBe(0);
});

it('should have one fault with one error', async () => {
const faults: RuleFault[] = [];

const api = await parse(apiWithOneFaultAndOneError);

resourceSpinalCase(api, faults);

expect(faults.length).toBe(1);
expect(faults[0].errors.length).toBe(1);
});

it('should have one fault with two errors', async () => {
const faults: RuleFault[] = [];

const api = await parse(apiWithOneFaultAndTwoErrors);

resourceSpinalCase(api, faults);

expect(faults.length).toBe(1);
expect(faults[0].errors.length).toBe(2);
});

it('should have two faults with one error each', async () => {
const faults: RuleFault[] = [];

const api = await parse(apiWithTwoFaultsOneErrorEach);

resourceSpinalCase(api, faults);

expect(faults.length).toBe(2);
expect(faults[0].errors.length).toBe(1);
expect(faults[1].errors.length).toBe(1);
});
});
6 changes: 4 additions & 2 deletions src/__tests__/validation/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('validate function', () => {
"must-contain-port": true,
"must-contain-version": true,
"no-singular-resource": true,
"must-contain-server-url": true
"must-contain-server-url": true,
"resource-spinal-case": true
};

it('should use use the provided rules', async () => {
Expand All @@ -22,7 +23,8 @@ describe('validate function', () => {
"must-contain-port": false,
"must-contain-version": false,
"no-singular-resource": false,
"must-contain-server-url": false
"must-contain-server-url": false,
"resource-spinal-case": false
};

/**
Expand Down
Loading

0 comments on commit 3c58380

Please sign in to comment.