Skip to content

Commit

Permalink
Add onSaveAction setting, refs #20
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRalphson committed Apr 14, 2020
1 parent 69540e2 commit 6047dab
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ All notable changes to the "openapi-lint" extension will be documented in this f

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [1.3.0]
- Add configurable `onSaveAction` setting
- Update `oas-kit` to latest versions

## [1.2.0]
- Add AsyncAPI v2.0.x support
- Lint-on-save functionality
- Validate-on-save functionality

## [1.1.0]
- Integrate errors/warnings with the 'Problems' (diagnostics) pane
- Accurate line/column positions will be coming in version 1.3 or 2.0
- Accurate line/column positions will be coming in version 1.4 or 2.0

## [1.0.0]
- Update `oas-kit` to latest versions
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The extension should work as-is

## Extension Settings

No configuration is currently possible or needed.
The `onSaveAction` can be set to one of `none`, `validate` (the default), `resolveAndValidate`, `lint` or `resolveAndLint`.

## Known Issues

Expand Down
21 changes: 19 additions & 2 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,21 @@ function validate(lint, resolve) {
}

function optionallyValidateOnSave(document) {
const onSaveAction = vscode.workspace.getConfiguration('openapi-lint').get('onSaveAction');

if (onSaveAction === 'none') return true;
let text = document.getText();
try {
let obj = yaml.parse(text);
if (!obj || (!obj.openapi && !obj.swagger)) return false;
let options = {};
validator.validateSync(obj, options)
if ((onSaveAction === 'resolveAndValidate') || (onSaveAction === 'resolveAndLint')) {
options.resolve = true;
}
if ((onSaveAction === 'lint') || (onSaveAction === 'resolveAndLint')) {
options.lint = true;
}
validator.validate(obj, options)
.then(result => {
dc.delete(document.uri);
return result;
Expand All @@ -185,7 +194,15 @@ function optionallyValidateOnSave(document) {
let range; // TODO
diagnostics.push(new vscode.Diagnostic(range, ex.message, vscode.DiagnosticSeverity.Error));
for (let warning of options.warnings||[]) {
diagnostics.push(new vscode.Diagnostic(range, warning.message + ' ' + warning.ruleName, vscode.DiagnosticSeverity.Warning));
let warn = new vscode.Diagnostic(range, warning.message, vscode.DiagnosticSeverity.Warning);
warn.source = 'openapi-lint';
if (warning.rule.url) {
warn.code = { value: warning.ruleName, target: vscode.Uri.parse(warning.rule.url+'#'+warning.ruleName) };
}
else {
warn.code = warning.ruleName;
}
diagnostics.push(warn);
}
dc.set(document.uri, diagnostics);
return false;
Expand Down
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,28 @@
"fileMatch": "*oas2.json",
"url": "./schemas/openapi-2.0.json"
}
]
],
"configuration": {
"title": "OpenAPI-Lint",
"properties": {
"openapi-lint.onSaveAction": {
"type": "string",
"description": "Action to take on saving recognised files.",
"default": "validate",
"enum": [
"none",
"validate",
"resolveAndValidate",
"lint",
"resolveAndLint"
]
}
}
}
},
"icon": "images/icon.png",
"scripts": {
"xostinstall": "node ./node_modules/vscode/bin/install",
"old-postinstall": "node ./node_modules/vscode/bin/install",
"test": "node ./node_modules/vscode/bin/test"
},
"devDependencies": {
Expand Down

0 comments on commit 6047dab

Please sign in to comment.