Skip to content

Commit

Permalink
added the 'validate' command
Browse files Browse the repository at this point in the history
Removed the 'hello' command as well
  • Loading branch information
whitlockjc committed Jul 21, 2018
1 parent 96b6c41 commit 90c75fa
Show file tree
Hide file tree
Showing 13 changed files with 1,094 additions and 177 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
16 changes: 13 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": "oclif"
}
# ESLint
#
# Configuration: http://eslint.org/docs/user-guide/configuring
# Rules: http://eslint.org/docs/rules/
extends: oclif
rules:
indent: [error, 2]
no-console: 0
no-process-exit: 0
max-nested-callbacks: [error, 8]
quotes: [2, single, avoid-escape]
space-before-function-paren: [error, always]
unicorn/no-process-exit: 0
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,62 @@ oval: CLI for (O)penAPI Specification document (val)idation
[![License](https://img.shields.io/npm/l/oval.svg)](https://github.com/whitlockjc/oval/blob/master/package.json)

<!-- toc -->
* [Usage](#usage)
* [Commands](#commands)
<!-- tocstop -->
# Usage
<!-- usage -->
```sh-session
$ npm install -g oval
$ oval COMMAND
running command...
$ oval (-v|--version|version)
oval/0.0.1 darwin-x64 node-v8.3.0
$ oval --help [COMMAND]
USAGE
$ oval COMMAND
...
```
<!-- usagestop -->
# Commands
<!-- commands -->
* [`oval help [COMMAND]`](#oval-help-command)
* [`oval validate LOCATION`](#oval-validate-location)

## `oval help [COMMAND]`

display help for oval

```
USAGE
$ oval help [COMMAND]
ARGUMENTS
COMMAND command to show help for
OPTIONS
--all see all commands in CLI
```

_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.0.5/src/commands/help.ts)_

## `oval validate LOCATION`

validate an OpenAPI Specification (OAS) document

```
USAGE
$ oval validate LOCATION
ARGUMENTS
LOCATION The path/URL to the OAS document being validated
OPTIONS
-N, --no-color turn off colored output
-j, --json output results as JSON
-p, --print-success print message for success
-w, --warnings-as-errors treat warnings as errors
```

_See code: [lib/commands/validate.js](https://github.com/whitlockjc/oval/blob/v0.0.1/lib/commands/validate.js)_
<!-- commandsstop -->
25 changes: 25 additions & 0 deletions bin/oval
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
#!/usr/bin/env node

/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Jeremy Whitlock
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

// oclif (https://oclif.io/) entry point
require('@oclif/command').run()
.then(require('@oclif/command/flush'))
.catch(require('@oclif/errors/handle'))
25 changes: 24 additions & 1 deletion bin/oval.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
@echo off

node "%~dp0\run" %*
:: The MIT License (MIT)
::
:: Copyright (c) 2018 Jeremy Whitlock
::
:: Permission is hereby granted, free of charge, to any person obtaining a copy
:: of this software and associated documentation files (the "Software"), to deal
:: in the Software without restriction, including without limitation the rights
:: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
:: copies of the Software, and to permit persons to whom the Software is
:: furnished to do so, subject to the following conditions:
::
:: The above copyright notice and this permission notice shall be included in
:: all copies or substantial portions of the Software.
::
:: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
:: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
:: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
:: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
:: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
:: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
:: THE SOFTWARE.

:: Use node to run the JavaScript version
node "%~dp0\oval" %*
20 changes: 0 additions & 20 deletions lib/commands/hello.js

This file was deleted.

133 changes: 133 additions & 0 deletions lib/commands/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Jeremy Whitlock
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

const chalk = require('chalk')
const {Command, flags} = require('@oclif/command')
const {pathToPtr} = require('json-refs')
const Sway = require('sway')

const printDetails = (label, items) => {
var color = label === 'ERRORS' ? chalk.red : chalk.yellow

console.error()
console.error(chalk.bold(label))

items.forEach(item => {
console.error(' ' + chalk.bold(chalk.underline.gray(pathToPtr(item.path))) + ': ' + color(item.message))
})
}

class ValidateCommand extends Command {
async run () {
const {args, flags} = this.parse(ValidateCommand)

// Turn off color output if requested
if (flags['no-color']) {
chalk.enabled = false
}

Sway.create({
definition: args.location,
})
.then(api => {
const {errors, warnings} = api.validate()
const success = errors.length + (flags['warnings-as-errors'] ? warnings.length : 0) === 0

if (success) {
if (flags['print-success']) {
console.log('OpenAPI Specification document is valid!')
}
} else {
const err = new Error('OpenAPI Specification document is not valid!')

err.isValidationError = true
err.errors = errors
err.warnings = warnings

throw err
}
})
.catch(err => {
if (err.isValidationError) {
if (flags.json) {
console.error(JSON.stringify({
message: err.message,
errors: err.errors,
warnings: err.warnings,
}, null, 2))
} else {
console.error(err.message)

if (err.errors.length > 0) {
printDetails('ERRORS', err.errors)
}

if (err.warnings.length > 0) {
printDetails('WARNINGS', err.warnings)
}

console.error()
}
} else {
console.error(err.stack)
}

process.exit(1)
})
}
}

ValidateCommand.description = 'validate an OpenAPI Specification (OAS) document'

ValidateCommand.args = [
{
description: 'The path/URL to the OAS document being validated',
name: 'location',
required: true,
},
]

ValidateCommand.flags = {
json: flags.boolean({
char: 'j',
description: 'output results as JSON',
exclusive: ['print-success'],
}),
'no-color': flags.boolean({
char: 'N',
description: 'turn off colored output',
}),
'print-success': flags.boolean({
char: 'p',
description: 'print message for success',
exclusive: ['json'],
}),
'warnings-as-errors': flags.boolean({
allowNo: false,
char: 'w',
description: 'treat warnings as errors',
}),
}

module.exports = ValidateCommand
25 changes: 25 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Jeremy Whitlock
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

// Helper from oclif (https://oclif.io/) that exposes the oval CLI application programmatically
module.exports = require('@oclif/command')
Loading

0 comments on commit 90c75fa

Please sign in to comment.