/azure-openapi-validator

Azure Open API Validator

Primary LanguageTypeScriptMIT LicenseMIT

azure-openapi-validator

azure-openapi-validator is linter for azure openapi specifications, it's an extension of autorest and supports spectral lint rule format. This repo also contains all the automated linter rules that apply to the API specs in the azure-rest-api-sepcs.

Rules

Please refer to rules

Contributing

  • If you want to submit a new rule request or bug, please file an issue

  • If you want to contribute a new linter rule, check out CONTRIBUTING.md

Packages

Name Latest
autorest extension
openapi-validator ](https://www.npmjs.com/package/@microsoft.azure/openapi-validator)
core functionality
openapi-validator-core ](https://www.npmjs.com/package/@microsoft.azure/openapi-validator-core)
ruleset
openapi-validator-rulesets ](https://www.npmjs.com/package/@microsoft.azure/openapi-validator-rulesets)

How to run locally

using the autorest to run the linter

autorest --v3 --azure-validator [--tag=<readme tag>] <path-to-readme>
or
autorest --v3 --azure-validator --input-file=<path-to-swagger>

How to use the Spectral ruleset

Dependencies

The Spectral ruleset requires Node version 14 or later.

Install Spectral

npm i @stoplight/spectral-cli -g

Usage

Azure-openapi-validator currently defines three Spectral ruleset configurations:

  1. az-common.ts : for rules that apply to all Azure REST APIs
  2. az-arm.ts: for rules that only apply to ARM REST APIs
  3. az-dataplane.ts: for rules that only apply to dataplane REST APIs

All rulesets reside in the packages/rulesets/generated/spectral folder of the repo.

You can specify the ruleset directly on the command line:

spectral lint -r https://raw.githubusercontent.com/Azure/azure-openapi-validator/develop/packages/rulesets/generated/spectral/az-dataplane.js <api definition file>

Or you can create a Spectral configuration file (.spectral.yaml) that references the ruleset:

extends:
  - https://raw.githubusercontent.com/Azure/azure-openapi-validator/develop/packages/rulesets/generated/spectral/az-dataplane.js

Example

spectral lint -r https://raw.githubusercontent.com/Azure/azure-openapi-validator/develop/packages/rulesets/generated/spectral/az-dataplane.js petstore.yaml

Using the Spectral VSCode extension

There is a Spectral VSCode extension that will run the Spectral linter on an open API definition file and show errors right within VSCode. You can use this ruleset with the Spectral VSCode extension.

  1. Install the Spectral VSCode extension from the extensions tab in VSCode.
  2. Create a Spectral configuration file (.spectral.yaml) in the root directory of your project as shown above.
  3. Set spectral.rulesetFile to the name of this configuration file in your VSCode settings.

Now when you open an API definition in this project, it should highlight lines with errors. You can also get a full list of problems in the file by opening the "Problems panel" with "View / Problems". In the Problems panel you can filter to show or hide errors, warnings, or infos.

Troubleshooting

See common issues here