-
Notifications
You must be signed in to change notification settings - Fork 14
/
local.js
60 lines (54 loc) · 2.3 KB
/
local.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { Flags } = require('@oclif/core')
const BaseValidateCommand = require('../../../support/command/base-validate-command')
const { pipeAsync } = require('../../../utils/general')
const { getResponseContent } = require('../../../support/command/handle-response')
const { postStandardization } = require('../../../requests/standardization')
const { parseDefinition, getSpecification, getVersion } = require('../../../utils/definitions')
const { readFileSync } = require('fs-extra')
class ValidateLocalCommand extends BaseValidateCommand {
async run() {
const { flags } = await this.parse(ValidateLocalCommand)
this.validateDefinition(flags['file'])
const definition = readFileSync(flags['file'])
const validationResult = await this.getValidationResult(flags['organization'], definition)
this.printValidationOutput(flags, validationResult)
this.exitWithCode(flags, validationResult)
}
// Rudimentary checks to check that file is
// at least an OpenAPI/AsyncAPI definition
// and is valid yaml/json
validateDefinition(pathToFile) {
const definition = parseDefinition(pathToFile)
getSpecification(definition)
getVersion(definition)
}
getValidationResult(orgName, body) {
return this.executeHttp({
execute: () => postStandardization([orgName, 'scan'], body),
onResolve: pipeAsync(getResponseContent, JSON.parse),
options: {}
})
}
}
ValidateLocalCommand.description = `Runs a scan against a local API definition using the organization's standardization configuration on SwaggerHub.
If the flag \`-c\` or \`--failOnCritical\` is used and there are standardization
errors with \`Critical\` severity present, the command will exit with error code \`1\`.
`
ValidateLocalCommand.examples = [
'swaggerhub api:validate:local -o myOrg -f ./my-api.yaml -c -j ',
'swaggerhub api:validate:local --organization myOrg --file ./my-api/json --fail-on-critical --json',
]
ValidateLocalCommand.flags = {
'file': Flags.string({
char: 'f',
description: 'Path of API definition file to run scan against',
required: true
}),
'organization': Flags.string({
char: 'o',
description: 'Which organization\'s standardization settings to use for linting the target definition',
required: true
}),
...BaseValidateCommand.flags
}
module.exports = ValidateLocalCommand