-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const jsyaml = require('js-yaml'); | ||
|
||
const cwd = process.cwd(); | ||
|
||
const file = (refValue, options) => { | ||
let refPath = refValue; | ||
const baseFolder = options.baseFolder ? path.resolve(cwd, options.baseFolder) : cwd; | ||
|
||
if (refPath.indexOf('file:') === 0) { | ||
refPath = refPath.substring(5); | ||
} else { | ||
refPath = path.resolve(baseFolder, refPath); | ||
} | ||
|
||
const filePath = getRefFilePath(refPath); | ||
const filePathLowerCase = filePath.toLowerCase(); | ||
|
||
const data = fs.readFileSync(filePath, 'utf8'); | ||
if (filePathLowerCase.endsWith('.json')) { | ||
return JSON.parse(data); | ||
} else if (filePathLowerCase.endsWith('.yml') || filePathLowerCase.endsWith('.yaml')) { | ||
return jsyaml.load(data); | ||
} | ||
}; | ||
|
||
function getRefFilePath(refPath) { | ||
const hashIndex = refPath.indexOf('#'); | ||
if (hashIndex > 0) { | ||
return refPath.substring(0, hashIndex); | ||
} else { | ||
return refPath; | ||
} | ||
} | ||
|
||
module.exports = { file }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,77 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const jsyaml = require('js-yaml'); | ||
const fs = require('fs'); | ||
|
||
const cwd = process.cwd(); | ||
const { getOAIVersion } = require('./schemaUtils'); | ||
|
||
const file = (refValue, options) => { | ||
let refPath = refValue; | ||
const baseFolder = options.baseFolder ? path.resolve(cwd, options.baseFolder) : cwd; | ||
async function loadSchemaAsync(swaggerPath) { | ||
const SwaggerParser = require('swagger-parser'); | ||
|
||
if (refPath.indexOf('file:') === 0) { | ||
refPath = refPath.substring(5); | ||
} else { | ||
refPath = path.resolve(baseFolder, refPath); | ||
const [dereferencedSchema, jsonSchema] = await Promise.all([ | ||
SwaggerParser.dereference(swaggerPath), | ||
SwaggerParser.parse(swaggerPath) | ||
]); | ||
|
||
return { dereferencedSchema, jsonSchema }; | ||
} | ||
|
||
function loadSchema(pathOrSchema, options) { | ||
const schemaValidators = require('./schemaValidators'); | ||
|
||
const jsonSchema = getJsonSchema(pathOrSchema); | ||
const basePath = getSchemaBasePath(pathOrSchema, options); | ||
const dereferencedSchema = dereference(basePath, jsonSchema); | ||
|
||
if (getOAIVersion(dereferencedSchema) === 3) { | ||
const validationResult = schemaValidators.getOAI3Validator().validate(dereferencedSchema); | ||
if (validationResult.errors && validationResult.errors.length > 0) { | ||
const error = new Error('Invalid OpenAPI 3 schema'); | ||
error.errors = validationResult.errors; | ||
throw error; | ||
} | ||
} | ||
|
||
const filePath = getRefFilePath(refPath); | ||
const filePathLowerCase = filePath.toLowerCase(); | ||
return { jsonSchema, dereferencedSchema }; | ||
} | ||
|
||
var data = fs.readFileSync(filePath, 'utf8'); | ||
if (filePathLowerCase.endsWith('.json')) { | ||
return JSON.parse(data); | ||
} else if (filePathLowerCase.endsWith('.yml') || filePathLowerCase.endsWith('.yaml')) { | ||
return jsyaml.load(data); | ||
function dereference(basePath, jsonSchema) { | ||
const deref = require('json-schema-deref-sync'); | ||
const schemaLoaders = require('./fileLoaders'); | ||
|
||
const dereferencedSchema = deref(jsonSchema, { | ||
baseFolder: basePath, | ||
failOnMissing: true, | ||
loaders: schemaLoaders | ||
}); | ||
|
||
return dereferencedSchema; | ||
} | ||
|
||
function getJsonSchema(pathOrSchema) { | ||
if (typeof pathOrSchema === 'string') { | ||
// file path | ||
const yaml = require('js-yaml'); | ||
|
||
const fileContents = fs.readFileSync(pathOrSchema); | ||
const jsonSchema = yaml.load(fileContents, 'utf8'); | ||
return jsonSchema; | ||
} else { | ||
// json schema | ||
return pathOrSchema; | ||
} | ||
}; | ||
} | ||
|
||
function getRefFilePath(refPath) { | ||
let filePath = refPath; | ||
const hashIndex = filePath.indexOf('#'); | ||
if (hashIndex > 0) { | ||
filePath = refPath.substring(0, hashIndex); | ||
function getSchemaBasePath(pathOrSchema, options = {}) { | ||
// always return basePath from options if exists | ||
if (options.basePath) { | ||
return options.basePath; | ||
} | ||
|
||
return filePath; | ||
// in case a path to definitions file was given | ||
if (typeof pathOrSchema === 'string') { | ||
const fullPath = path.resolve(pathOrSchema).split(path.sep); | ||
fullPath.pop(); | ||
return fullPath.join(path.sep); | ||
} | ||
} | ||
|
||
module.exports = { file }; | ||
module.exports = { loadSchema, loadSchemaAsync }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
const OpenAPISchemaValidator = require('openapi-schema-validator').default; | ||
const openApi3Validator = new OpenAPISchemaValidator({ | ||
version: 3, | ||
version3Extensions: { | ||
|
||
let validator; | ||
|
||
function getOAI3Validator() { | ||
if (!validator) { | ||
validator = new OpenAPISchemaValidator({ | ||
version: 3, | ||
version3Extensions: { | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
return validator; | ||
} | ||
|
||
module.exports = { | ||
openApi3Validator | ||
getOAI3Validator | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters