Skip to content

Commit

Permalink
Sync version of lint function
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Apr 17, 2016
1 parent af5fda6 commit 55f9541
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"dependencies": {
"babel-core": "^6.7.4",
"json-pointer": "^0.5.0",
"json-schema-ref-parser": "^3.0.2",
"lodash": "^4.7.0",
"swagger-parser": "^3.4.0",
"yargs": "^4.6.0",
Expand Down
3 changes: 0 additions & 3 deletions src/checkers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export function runCheckers(api, checkers = ALL_CHECKERS) {
}

if (schema.$$ref && json.$ref) {
//console.log('llol');
//console.log(json.$ref);
let subSchema = api.$refs.get(json.$ref);
//console.log(schema.$$ref);
let $$ref = jp.get(schemaCopy, schema.$$ref.substr(1));
validator.validate(subSchema, $$ref);
}
Expand Down
10 changes: 8 additions & 2 deletions src/openapi-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import API from './types/api';
import chalk from 'chalk';

const OpenAPILint = {
lint(specUrl) {
return API.create(specUrl).then((api) => {
lint(spec) {
return API.create(spec).then((api) => {
let checks = runCheckers(api);
return groupBy(checks, 'code');
});
},

lintResolved(spec) {
let api = API.createResolved(spec);
let checks = runCheckers(api);
return groupBy(checks, 'code');
},

getIssuesInfo
};

Expand Down
28 changes: 20 additions & 8 deletions src/types/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwaggerParser from 'swagger-parser';
import $Refs from 'json-schema-ref-parser/lib/refs';
import {cloneDeep} from 'lodash';

export default class API {
Expand All @@ -7,16 +8,27 @@ export default class API {
this.$refs = $refs;
}

static create(specUrl) {
let spec;
return SwaggerParser.parse(specUrl)
.then(_spec => {
spec = _spec;
return SwaggerParser.resolve(spec, {$refs: {internal: true}});
static create(spec, resolved = false) {
if (typeof spec === 'object' && resolved) {
return API.createResolved(spec);
}

let parsed;
return SwaggerParser.parse(spec)
.then(_parsed => {
parsed = _parsed;
return SwaggerParser.resolve(parsed, {$refs: {internal: true}});
})
.then($refs => {
console.log($refs.paths());
return new API(spec, $refs);
return new API(parsed, $refs);
});
}

// sync version of create
static createResolved(spec) {
let $refs = new $Refs();
//carefull: using internal API
$refs._add('#', spec);
return new API(spec, $refs);
}
}

0 comments on commit 55f9541

Please sign in to comment.