@stoplight/spectral
Advanced tools
Comparing version 2.1.1 to 2.2.0
import { Command, flags as flagHelpers } from '@oclif/command'; | ||
import { IRuleResult } from '../..'; | ||
export default class Lint extends Command { | ||
@@ -8,9 +7,11 @@ static description: string; | ||
help: import("@oclif/parser/lib/flags").IBooleanFlag<void>; | ||
config: flagHelpers.IOptionFlag<string | undefined>; | ||
encoding: flagHelpers.IOptionFlag<string | undefined>; | ||
format: flagHelpers.IOptionFlag<string | undefined>; | ||
output: flagHelpers.IOptionFlag<string | undefined>; | ||
'max-results': import("@oclif/parser/lib/flags").IOptionFlag<number | undefined>; | ||
maxResults: import("@oclif/parser/lib/flags").IOptionFlag<number | undefined>; | ||
ruleset: flagHelpers.IOptionFlag<string[]>; | ||
'skip-rule': flagHelpers.IOptionFlag<string[]>; | ||
verbose: import("@oclif/parser/lib/flags").IBooleanFlag<boolean>; | ||
config: flagHelpers.IOptionFlag<string | undefined>; | ||
ruleset: flagHelpers.IOptionFlag<string[]>; | ||
}; | ||
@@ -22,3 +23,2 @@ static args: { | ||
} | ||
export declare function formatOutput(results: IRuleResult[], flags: any): Promise<string>; | ||
export declare function writeOutput(outputStr: string, flags: any, command: Lint): Promise<void>; |
@@ -67,2 +67,6 @@ "use strict"; | ||
help: command_1.flags.help({ char: 'h' }), | ||
config: command_1.flags.string({ | ||
char: 'c', | ||
description: 'path to a config file', | ||
}), | ||
encoding: command_1.flags.string({ | ||
@@ -81,14 +85,9 @@ char: 'e', | ||
}), | ||
'max-results': command_1.flags.integer({ | ||
description: '[default: all] maximum results to show', | ||
}), | ||
maxResults: command_1.flags.integer({ | ||
char: 'm', | ||
description: '[default: all] maximum results to show', | ||
description: 'deprecated: use --max-results instead', | ||
}), | ||
verbose: command_1.flags.boolean({ | ||
char: 'v', | ||
description: 'increase verbosity', | ||
}), | ||
config: command_1.flags.string({ | ||
char: 'c', | ||
description: 'path to a config file', | ||
}), | ||
ruleset: command_1.flags.string({ | ||
@@ -99,27 +98,48 @@ char: 'r', | ||
}), | ||
'skip-rule': command_1.flags.string({ | ||
char: 's', | ||
description: 'ignore certain rules if they are causing trouble', | ||
multiple: true, | ||
}), | ||
verbose: command_1.flags.boolean({ | ||
char: 'v', | ||
description: 'increase verbosity', | ||
}), | ||
}; | ||
Lint.args = [{ name: 'source' }]; | ||
exports.default = Lint; | ||
function lint(name, flags, command, customRules) { | ||
function lint(name, flags, command, rules) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
command.log(`Linting ${name}`); | ||
if (flags.verbose) { | ||
command.log(`Linting ${name}`); | ||
} | ||
const spec = yield reader_1.readParsable(name, flags.encoding); | ||
const spectral = new spectral_1.Spectral(); | ||
if (customRules) { | ||
command.log('Applying custom rules. Automatic rule detection is off.'); | ||
spectral.addRules(customRules); | ||
if (rules !== undefined) { | ||
if (flags.verbose) { | ||
command.log(`Found ${Object.keys(rules).length} rules`); | ||
} | ||
} | ||
else if (parseInt(spec.data.swagger) === 2) { | ||
command.log('OpenAPI 2.0 (Swagger) detected'); | ||
spectral.addFunctions(oas2_1.oas2Functions()); | ||
spectral.addRules(oas2_1.oas2Rules()); | ||
else { | ||
if (flags.verbose) { | ||
command.log('No rules loaded, attempting to detect document type'); | ||
} | ||
if (parseInt(spec.data.swagger) === 2) { | ||
command.log('OpenAPI 2.0 (Swagger) detected'); | ||
spectral.addFunctions(oas2_1.oas2Functions()); | ||
rules = oas2_1.oas2Rules(); | ||
} | ||
else if (parseInt(spec.data.openapi) === 3) { | ||
command.log('OpenAPI 3.x detected'); | ||
spectral.addFunctions(oas3_1.oas3Functions()); | ||
rules = oas3_1.oas3Rules(); | ||
} | ||
} | ||
else if (parseInt(spec.data.openapi) === 3) { | ||
command.log('OpenAPI 3.x detected'); | ||
spectral.addFunctions(oas3_1.oas3Functions()); | ||
spectral.addRules(oas3_1.oas3Rules()); | ||
if (flags.skipRule) { | ||
rules = skipRules(Object.assign({}, rules), flags, command); | ||
} | ||
else { | ||
throw new Error('Input document specification type could not be determined'); | ||
if (!rules) { | ||
throw new Error('No rules provided, and document type does not have any default rules, so lint has nothing to do'); | ||
} | ||
spectral.addRules(rules); | ||
let results = []; | ||
@@ -153,2 +173,22 @@ try { | ||
} | ||
const skipRules = (rules, flags, command) => { | ||
const skippedRules = []; | ||
const invalidRules = []; | ||
for (const rule of flags.skipRule) { | ||
if (rule in rules) { | ||
delete rules[rule]; | ||
skippedRules.push(rule); | ||
} | ||
else { | ||
invalidRules.push(rule); | ||
} | ||
} | ||
if (invalidRules.length !== 0) { | ||
command.warn(`ignoring invalid ${invalidRules.length > 1 ? 'rules' : 'rule'} "${invalidRules.join(', ')}"`); | ||
} | ||
if (skippedRules.length !== 0 && flags.verbose) { | ||
command.log(`INFO: skipping ${skippedRules.length > 1 ? 'rules' : 'rule'} "${skippedRules.join(', ')}"`); | ||
} | ||
return rules; | ||
}; | ||
function formatOutput(results, flags) { | ||
@@ -165,3 +205,2 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
} | ||
exports.formatOutput = formatOutput; | ||
function writeOutput(outputStr, flags, command) { | ||
@@ -181,7 +220,8 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
output: flags.output, | ||
maxResults: flags.maxResults, | ||
maxResults: flags.maxResults > 0 ? flags.maxResults : flags['max-results'], | ||
verbose: flags.verbose, | ||
ruleset: flags.ruleset, | ||
skipRule: flags['skip-rule'], | ||
}, lodash_1.isNil)); | ||
} | ||
//# sourceMappingURL=lint.js.map |
@@ -1,1 +0,1 @@ | ||
{"version":"2.1.1","commands":{"lint":{"id":"lint","description":"lint a JSON/YAML document from a file or URL","pluginName":"@stoplight/spectral","pluginType":"core","aliases":[],"examples":["$ spectral lint .openapi.yaml\nlinting ./openapi.yaml\n"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"encoding":{"name":"encoding","type":"option","char":"e","description":"text encoding to use"},"format":{"name":"format","type":"option","char":"f","description":"formatter to use for outputting results","options":["json","stylish"]},"output":{"name":"output","type":"option","char":"o","description":"output to a file instead of stdout"},"maxResults":{"name":"maxResults","type":"option","char":"m","description":"[default: all] maximum results to show"},"verbose":{"name":"verbose","type":"boolean","char":"v","description":"increase verbosity","allowNo":false},"config":{"name":"config","type":"option","char":"c","description":"path to a config file"},"ruleset":{"name":"ruleset","type":"option","char":"r","description":"path to a ruleset file (supports remote files)"}},"args":[{"name":"source"}]}}} | ||
{"version":"2.2.0","commands":{"lint":{"id":"lint","description":"lint a JSON/YAML document from a file or URL","pluginName":"@stoplight/spectral","pluginType":"core","aliases":[],"examples":["$ spectral lint .openapi.yaml\nlinting ./openapi.yaml\n"],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"config":{"name":"config","type":"option","char":"c","description":"path to a config file"},"encoding":{"name":"encoding","type":"option","char":"e","description":"text encoding to use"},"format":{"name":"format","type":"option","char":"f","description":"formatter to use for outputting results","options":["json","stylish"]},"output":{"name":"output","type":"option","char":"o","description":"output to a file instead of stdout"},"max-results":{"name":"max-results","type":"option","description":"[default: all] maximum results to show"},"maxResults":{"name":"maxResults","type":"option","char":"m","description":"deprecated: use --max-results instead"},"ruleset":{"name":"ruleset","type":"option","char":"r","description":"path to a ruleset file (supports remote files)"},"skip-rule":{"name":"skip-rule","type":"option","char":"s","description":"ignore certain rules if they are causing trouble"},"verbose":{"name":"verbose","type":"boolean","char":"v","description":"increase verbosity","allowNo":false}},"args":[{"name":"source"}]}}} |
{ | ||
"name": "@stoplight/spectral", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "A flexible object linter with out of the box support for OpenAPI v2 and v3.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -63,2 +63,8 @@ ![Spectral logo](img/spectral-banner.png) | ||
### Docker | ||
```bash | ||
docker run --rm -it stoplight/spectral lint "${URL}"` | ||
``` | ||
## Usage | ||
@@ -77,8 +83,12 @@ | ||
``` text | ||
-e, --encoding=encoding [default: utf8] text encoding to use | ||
-f, --format=json|stylish [default: stylish] formatter to use for outputting results | ||
-c, --config=config path to a config file | ||
-e, --encoding=encoding text encoding to use | ||
-f, --format=json|stylish formatter to use for outputting results | ||
-h, --help show CLI help | ||
-m, --maxResults=maxResults [default: all] maximum results to show | ||
-m, --maxResults=maxResults deprecated: use --max-results instead | ||
-o, --output=output output to a file instead of stdout | ||
-r, --ruleset=ruleset path to a ruleset file (supports remote files) | ||
-s, --skip-rule=skip-rule ignore certain rules if they are causing trouble | ||
-v, --verbose increase verbosity | ||
--max-results=max-results [default: all] maximum results to show | ||
``` | ||
@@ -99,2 +109,11 @@ | ||
const { oas3Functions, oas3Rules } = require('@stoplight/spectral/rulesets/oas3'); | ||
// for YAML | ||
const { parseWithPointers } = require("@stoplight/yaml"); | ||
const myOAS = parseWithPointers(` | ||
responses: | ||
'200': | ||
description: '' | ||
schema: | ||
$ref: '#/definitions/error-response' | ||
`) | ||
@@ -101,0 +120,0 @@ // an OASv3 document |
@@ -13,4 +13,5 @@ export declare enum ConfigFormat { | ||
output?: string; | ||
ruleset?: string[]; | ||
skipRule?: string[]; | ||
verbose: boolean; | ||
ruleset?: string[]; | ||
} | ||
@@ -17,0 +18,0 @@ export interface IConfig { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
284446
5929
323