Skip to content

Commit 9bd0138

Browse files
authored
Merge pull request #37 from dimakis/migrate_funcs_to_v6
feat: api validation with spectral v6 and validators for API compilance and KCP integration
2 parents 79cbcde + 84a6631 commit 9bd0138

File tree

9 files changed

+45
-149
lines changed

9 files changed

+45
-149
lines changed

spectral/README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ echo "extends: '@rhoas/spectral-ruleset'" > .spectral.yaml
2121
Run the Spectral CLI:
2222
2323
```shell
24-
npm install -g @stoplight/spectral@5.9.0
24+
npm install -g @stoplight/spectral[email protected]
2525
spectral lint ./path/to/openapi.yaml
2626
```
2727
28+
or with yarn:
29+
```shell
30+
yarn add global @stoplight/[email protected]
31+
yarn spectral lint ./path/to/openapi.yaml
32+
```
33+
2834
## Rules
2935
3036
The RHOAS ruleset extends the Spectral built-in "oas" ruleset (except `operation-tags`, `openapi-tags`). You can see the full list of rules from that ruleset [here](https://meta.stoplight.io/docs/spectral/docs/reference/openapi-rules.md)
@@ -86,12 +92,11 @@ info:
8692

8793
### rhoas-path-regexp
8894

89-
All paths must match the specified regular expression: `\/api\/([a-z_]*){1,}(\/v[0-9]*(alpha|beta)?)(\/{?[a-z_]*}?){0,}$`.
95+
All paths must match the specified regular expression: `\/api\/v.*\/.*$`.
9096

9197
- The first segment must be `/api`
92-
- The second segment can only contain alphabetical characters and underscores "_"
93-
- The third segment must specify the API version. This can be a major version such as `v1` or a channel-version such as `v1beta`, `v1alpha`.
94-
- All following segments must follow `camel_case` and can only contain alphabetical characters.
98+
- The second segment must specify the API version. This can be a major version such as `v1` or a channel-version such as `v1beta`, `v1alpha`.
99+
- All following segments must follow `snake_case` and can only contain alphabetical characters.
95100

96101
**Recommended**: Yes
97102
**Severity**: warning
@@ -102,7 +107,7 @@ The content type for all responses must be `application/json`.
102107

103108
**Recommended**: Yes
104109

105-
**Severity**: error
110+
**Severity**: warning
106111

107112
### rhoas-error-response
108113

@@ -132,16 +137,16 @@ properties:
132137

133138
**Severity**: error
134139

135-
### rhoas-schema-name-camel-case
140+
### rhoas-schema-name-pascal-case
136141

137-
All JSON schema objects defined in `components.schemas` must follow `CamelCase`.
142+
All JSON schema objects defined in `components.schemas` must follow `PascalCase`.
138143

139144
**Recommended**: Yes
140145

141146
**Severity**: warning
142147
### rhoas-schema-properties-snake-case
143148

144-
All JSON schema properties defined must follow `camel_case`.
149+
All JSON schema properties defined must follow `snake_case`.
145150

146151
**Recommended**: Yes
147152

spectral/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"cli-integration-tests": "./scripts/rules.sh"
1515
},
1616
"dependencies": {
17-
"@stoplight/spectral": "5.9.2"
17+
"@stoplight/spectral-cli": "^6.4.1",
18+
"@stoplight/spectral-core": "^1.12.3"
1819
},
1920
"devDependencies": {
2021
"@types/node": "15.14.1",
@@ -45,4 +46,4 @@
4546
"url": "https://github.com/redhat-developer/app-services-api-guidelines/issues"
4647
},
4748
"homepage": "https://github.com/redhat-developer/app-services-api-guidelines#readme"
48-
}
49+
}

spectral/ruleset.yaml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
extends: spectral:oas
1+
extends: spectral:oas
22
functions:
33
- expectServersConfig
4-
- objectReferenceSchema
5-
- securitySchemes
64
- infoLicenseApache2
75
- schemaDefinition
86
- externalRefs
@@ -60,13 +58,13 @@ rules:
6058
rhoas-response-media-type:
6159
given: "$.paths.*.*.responses.*.content"
6260
description: application/json is the only acceptable content type
63-
severity: error
61+
severity: warn
6462
then:
6563
function: truthy
6664
field: application/json
6765
rhoas-error-response:
6866
given: "$.paths..responses[?( @property >= 300)].content.*"
69-
severity: error
67+
severity: error
7068
then:
7169
field: schema
7270
function: schema
@@ -82,9 +80,9 @@ rules:
8280
functionOptions:
8381
schema:
8482
"$ref": "#/components/schemas/ObjectReference"
85-
rhoas-schema-name-camel-case:
86-
description: JSON Schema names should use CamelCase
87-
message: "`{{property}}` object name must follow CamelCase"
83+
rhoas-schema-name-pascal-case:
84+
description: JSON Schema names should use PascalCase
85+
message: "`{{property}}` object name must follow PascalCase"
8886
severity: error
8987
given: "$.components.schemas[*]~"
9088
then:

spectral/src/functions/expectServersConfig.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { IFunctionPaths, IFunctionResult } from "@stoplight/spectral";
1+
import { IFunctionResult } from "@stoplight/spectral-core";
22

3-
export default (targetVal: any, opts: any, paths: IFunctionPaths): IFunctionResult[] => {
3+
export default (targetVal: any, opts: any, context: any ): IFunctionResult[] => {
44
if (!targetVal) {
55
return [
66
{
@@ -15,9 +15,8 @@ export default (targetVal: any, opts: any, paths: IFunctionPaths): IFunctionResu
1515
}
1616
]
1717
}
18-
const rootPath = paths.target !== void 0 ? paths.target : paths.given
18+
const paths = context.path;
1919
const results: IFunctionResult[] = [];
20-
2120
const expectedUrls = opts.required
2221
const missingUrls = [];
2322
for (const u of expectedUrls) {
@@ -29,7 +28,7 @@ export default (targetVal: any, opts: any, paths: IFunctionPaths): IFunctionResu
2928
return [
3029
{
3130
message: `OpenAPI \`servers\` object is missing the following required URLs: ${missingUrls.join(', ')}`,
32-
path: [...rootPath, 0]
31+
path: [...paths, 0]
3332
}
3433
]
3534
}

spectral/src/functions/externalRefs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IFunctionResult } from "@stoplight/spectral";
1+
import { IFunctionResult } from "@stoplight/spectral-core";
22

33
export default (targetVal: any): IFunctionResult[] => {
44
if (!targetVal || !targetVal.length) {
@@ -12,4 +12,4 @@ export default (targetVal: any): IFunctionResult[] => {
1212
message: 'Only local relative `$ref` or absolute external URL `$ref` is allowed'
1313
}]
1414
}
15-
}
15+
};

spectral/src/functions/infoLicenseApache2.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
import { IFunctionPaths, IFunctionResult } from "@stoplight/spectral";
1+
import { IFunctionResult } from "@stoplight/spectral-core";
2+
import type { JsonPath } from '@stoplight/types';
23

3-
export default (targetVal: any, _: any, paths: IFunctionPaths): IFunctionResult[] => {
4-
const path = paths.target;
4+
5+
export default (targetVal: any, _: any, context: any): IFunctionResult[] => {
6+
let p: JsonPath = ['info']
57
if (!targetVal) {
68
return [
79
{
8-
message: "OpenAPI `license` object is required",
9-
path: path,
10+
message: "OpenAPI `license` object is required\n in path: ",
11+
path: p
1012
}
1113
]
1214
}
15+
1316
const results: IFunctionResult[] = [];
1417
const expectName = 'Apache 2.0'
18+
p = context.path[1];
1519
if (targetVal?.name != expectName) {
1620
results.push({
1721
message: '`name` must be "' + expectName + '"',
18-
path: [...path, 'license']
22+
path: [...p, 'license']
1923
})
2024
}
2125
const expectUrl = 'https://www.apache.org/licenses/LICENSE-2.0';
2226
if (targetVal?.url !== expectUrl) {
2327
results.push({
24-
message: '`url` must be "' + expectUrl + '"',
25-
path: [...path, 'license']
28+
message: '`url` must be "' + expectUrl + '" not "' + targetVal?.url + '"',
29+
path: [...p, 'license']
2630
})
2731
}
2832

spectral/src/functions/objectReferenceSchema.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

spectral/src/functions/schemaDefinition.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { IFunctionPaths, IFunctionResult, JSONSchema } from "@stoplight/spectral";
1+
import { IFunctionResult, JSONSchema } from "@stoplight/spectral-core";
22

33
// Recursively compare the target value of the OpenAPI document with an expected value
4-
export default (targetVal: any, { definition }: { definition: SchemaMetadata }, paths: IFunctionPaths): IFunctionResult[] => {
5-
const path = [...paths.target] as string[]
4+
export default (targetVal: any, { definition }: { definition: SchemaMetadata }, context: any): IFunctionResult[] => {
5+
const path = [...context.path] as string[]
66
const targetSchema = path[path.length-1]
77

88
if (!targetVal) {
@@ -15,7 +15,7 @@ export default (targetVal: any, { definition }: { definition: SchemaMetadata },
1515
}
1616

1717
return compareSchemas(definition, targetVal, path)
18-
}
18+
};
1919

2020
type JsonSchemaType = 'string' | 'number' | 'integer' | 'object' | 'array' | 'boolean' | 'null' | 'any';
2121
interface SchemaMetadata {

spectral/src/functions/securitySchemes.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)