Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
91aaba3
feat: add Siemens REST API linter
HeronLi2024 Aug 14, 2026
bdd99d4
chore: sync npm lockfile with api-linter workspace
fh1ch Aug 14, 2026
0a6df86
style(api-linter): apply prettier formatting to project
fh1ch Aug 14, 2026
52928e1
refactor(api-linter): correct renamed ruleset references in test configs
fh1ch Aug 14, 2026
7c139ab
chore(api-linter): lint with root eslint instead of standard
fh1ch Aug 14, 2026
0cc6250
refactor(api-linter): resolve eslint violations in linter sources
fh1ch Aug 14, 2026
74ab701
style(api-linter): add copyright headers
fh1ch Aug 14, 2026
cd1cfa1
chore(api-linter): apply all changes from the current internal Siemen…
toedter Aug 14, 2026
525d47b
chore(api-linter): add spectral_result.json to .gitignore
toedter Aug 14, 2026
3664972
chore(api-linter): add linter-report.png (for README.md)
toedter Aug 14, 2026
35fbcbb
chore(api-linter): fix eclint errors
toedter Aug 14, 2026
493c22f
docs(api-linter): change "Siemens Xcelerator API Guidelines" to "Siem…
toedter Aug 14, 2026
27ff975
docs(api-linter): change "html" to "HTML" in README.md
toedter Aug 14, 2026
76053ea
docs(api-linter): change api-linter version to ^0.8.6 in README.md
toedter Aug 14, 2026
fb175a2
docs(api-linter): improve yaml example comments in README.md
toedter Aug 14, 2026
71f1699
chore(api-linter): fix html error in template.html
toedter Aug 14, 2026
6535090
chore(api-linter): fix prettier issues
toedter Aug 14, 2026
c98f214
docs: add api-linter to README.md
toedter Aug 17, 2026
bc14682
chore(api-linter): set api linter version to 0.0.0-development
toedter Aug 17, 2026
508d4df
fix(api-linter): fix title and repo ref in api-linter template.html
toedter Aug 17, 2026
0ddac4d
chore(api-linter): remove jsonpath from dev dependencies
toedter Aug 17, 2026
68e3272
chore(api-linter): remove jsonpath from dev dependencies
toedter Aug 17, 2026
15310de
chore(api-linter): add example report generation to package.json
toedter Aug 17, 2026
7cc03a4
chore(api-linter): optimize package.json
toedter Aug 17, 2026
9094d23
chore(api-linter): improve example openapi.yml
toedter Aug 17, 2026
b5e6e69
chore(api-linter): add api-linter:test and api-linter:report to ci
toedter Aug 17, 2026
89fa3dc
chore(api-linter): add api-linter to .releaserc.json
toedter Aug 17, 2026
095d6de
chore(api-linter): add integrity to all downloaded external resources…
toedter Aug 17, 2026
e2923b9
fix(api-linter): changed hardcoded 'openapi.yml' to the actual specFi…
toedter Aug 17, 2026
bba1028
fix(api-linter): fix regex for versioning in siemens-api-versioning.js
toedter Aug 17, 2026
d254de6
fix(api-linter): fix individual vs collection response handling in ge…
toedter Aug 18, 2026
aeaba67
fix(api-linter): check all required headers in has-header-parameter-w…
toedter Aug 18, 2026
fe0f608
chore(api-linter): change contact in all test openAPI yml files
toedter Aug 18, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api-linter/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.npm-cache
/test
/example
171 changes: 171 additions & 0 deletions api-linter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Siemens API Linter

This project implements a linter reporter for the [Stoplight Spectral](https://docs.stoplight.io/docs/spectral/674b27b261c3c-overview) for API specifications.
The ruleset has been created based on the [Siemens Xcelerator API guidelines 2.5.0](https://developer.siemens.com/guidelines/api-guidelines/rest/index.html).

## Getting started
### Directory structure

- `example/`: contains a sample API spec file
- `rulesets/`: contains the ruleset files those follow the Siemens Xcelerator API guidelines
- `src/`: contains the source files for creating the linter report
- `test/`: jest test cases for different rules in the rulesets

### Installation

In order to include the rulesets and provide linting within your project, you can install the package from this project with this `@siemens/api-linter` package.

```json
{
"scripts": {
"test": "api-linter -s reference-api-specs/openapi.yml -r reference-api-specs/.spectral.yml"
},
"devDependencies": {
"@siemens/api-linter": "^0.8.4"
}
}
```

The `@siemens/api-linter` package includes the new Siemens REST API linter tool with html report capability.

In order to trigger the linter on your API specifications, it provides a script to execute the linter within the `package.json` specification.

```json
"scripts": {
"test": "api-linter -s reference-api-specs/openapi.yml -r reference-api-specs/.spectral.yml"
}
```
Developers can change the spec file path with `-s` option, ruleset file path with `-r` option accordingly in the scripts.<br>
Or just remove them and set them in the command line as:<br>

Script:
```json
"scripts": {
"test": "api-linter"
}
```
Command:
```groovy
npm test -s reference-api-specs/openapi.yml -r reference-api-specs/.spectral.yml
```

For more options:

```
Usage: api-linter -s "path-to-spec-file" -r "path-to-rule-file" [-f "fail-severity"] [-c "console-severity"] [-v "api-versioning"] [-a "api-security"]

Options:
-s, --specPath <openapi spec> path to openapi specification
-r, --rulesetPath <rule file> path to rules file
-f, --failSeverity <fail severity> test fails when met result the severity equal or higher than it (choices: "error", "warn", "info", "hint", default: warn)
-c, --consoleSeverity <console severity> console output message for linter result no matter job failed or succeeded (choices: "error", "warn", "info", "hint", default: warn)
-o, --outputFilename <output filename> specify the output filename with or without .html extension (default: linter-result.html)
-p, --jsonFile <output json file>, output json file with origin spectral result data
-v, --apiVersioning <api versioning> the api versioning way (choices: "ignore", "url", "header", default: ignore)
-a, --apiSecurity <authorization security> if enable authorization security (choices: "n/f/no/false/y/t/yes/true" default: n)
--resolve enables follow external $refs (same as Spectral --resolve) (default is not enabled, i.e. external references are not resolved and ignored)
```

A linter report will be generated according to the execution as:

![Example Image](linter-report.png)

### Linting within CI/CD

You can trigger linting on your CI/CD pipeline executing `npm test` within the `.gitlab-ci.yml` CI configuration file.

See the following configuration example:

```yaml
build:
script:
- npm test
artifacts:
name: "api-linter-report"
paths:
- linter-result.html
when: always
```

> The Pipeline will fail if linter results with severity of `warn` (By default) or higher found!

> You may download or browse the report file in the job artifact page directly no matter CI job failed or succeeded.

### Selection of rulesets

The package provides several rulesets according to Siemens REST API guidelines.<br>
> They can be included as needed by providing a project specific `.spectral.yml` file on the projects root directory as follows:
```yaml
extends:
- "@siemens/api-linter/rulesets/siemens-api-media-type.yml"
- "@siemens/api-linter/rulesets/siemens-api-versioning.yml"
- "@siemens/api-linter/rulesets/siemens-api-error-reporting.yml"
- "@siemens/api-linter/rulesets/siemens-api-filtering.yml"
- "@siemens/api-linter/rulesets/siemens-api-sparse-fieldsets.yml"
- "@siemens/api-linter/rulesets/siemens-api-pagination.yml"
- "@siemens/api-linter/rulesets/siemens-api-sorting.yml"
- "@siemens/api-linter/rulesets/siemens-api-common-operation.yml"
- "@siemens/api-linter/rulesets/siemens-api-security.yml"
```
> You MAY use as below to achieve this for guideline linting `WITHOUT` `"spectral:oas"`.
```yaml
extends:
- "@siemens/api-linter/rulesets/siemens-api-express.yml"
```
> `OR` just extends the `ALL-IN-ONE` ruleset which with `"spectral:oas"` enabled.
```yaml
extends:
- "@siemens/api-linter/rulesets/siemens-api.yml"
```
### Disable any Ruleset or Rules
> You can disable any rules/rulesets as you wish by [Stoplight Spectral](https://docs.stoplight.io/docs/spectral/674b27b261c3c-overview).
```yaml
#e.g. disable spectral:oas ruleset and disable Siemens-API-[400] rules from the extended ruleset
extends:
- "@siemens/api-linter/rulesets/siemens-api.yml"
- ["spectral:oas", "off"]

rules:
Siemens-API-[400]: false
Siemens-API-[401]: false
```

### Define rule dependency relations
> Sometimes, if you don't want to follow a ruleC that under a high level ruleP, which means when ruleP failed, you don't want to check ruleC.
Then we can define the rules with `"x-dependsOn"` attribute.
```yaml
#e.g. Siemens-API-[101.8.1] depends on Siemens-API-[101.8]
extends:
- "@siemens/api-linter/rulesets/siemens-api.yml"

rules:
Siemens-API-[101.8.1]:
x-dependsOn:
- Siemens-API-[101.8]
```

## Integration Using JavaScript
> In case you want to handle the linting results by yourself
```javascript
const {validator} = require('@siemens/api-linter/src/extension');
const {DiagnosticSeverity} = require('@stoplight/types');
const path = require("path");
const validate = validator();

const validateDoc = function(){
const ruleSetFilePath = path.resolve(__dirname, 'reference-api-specs/.spectral.yml');
const apiSpecFilePath = path.resolve(__dirname, 'reference-api-specs/openapi-prod.yml');
validate(apiSpecFilePath, ruleSetFilePath).then(result => {
const diagnostics = [];
for (let msg of result.results || []){
if (msg.severity <= DiagnosticSeverity.Information ){
var diagnostic = { value: msg.code, severity: msg.severity, message: msg.message };
diagnostics.push(diagnostic);
}
}
// Add your own processes to handle diagnostic array;
});
}
validateDoc();
```

Loading
Loading