Skip to content

Commit 4dc7a11

Browse files
committed
refactor: upgrade to spectral v6 by refactoring existing custom functions
1 parent 315b408 commit 4dc7a11

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

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/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 {

0 commit comments

Comments
 (0)