-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the Swagger validator for both YAML and JSON formatted specifi…
…cations.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as SwaggerParser from 'swagger-parser' | ||
import * as YAML from 'yamljs' | ||
|
||
export function validateYaml (content: string): Promise<void> { | ||
return Promise | ||
.resolve() | ||
.then(function () { | ||
let data: any | ||
try { | ||
data = YAML.parse(content) | ||
} catch (e) { | ||
throw new Error('Unable to parse YAML string into object.') | ||
} | ||
return SwaggerParser.validate(data) | ||
}) | ||
.then(function (api) { | ||
// swagger specification is valid | ||
return | ||
}) | ||
.catch(function (error) { | ||
console.error(error) | ||
throw new Error('Swagger is invalid!') | ||
}) | ||
} | ||
|
||
export function validateJson (content: string): Promise<void> { | ||
return Promise | ||
.resolve() | ||
.then(function () { | ||
let data: any | ||
try { | ||
data = JSON.parse(content) | ||
} catch (e) { | ||
throw new Error('Unable to parse JSON string into object.') | ||
} | ||
return SwaggerParser.validate(data) | ||
}) | ||
.then(function (api) { | ||
// swagger specification is valid | ||
return | ||
}) | ||
.catch(function (error) { | ||
console.error(error) | ||
throw new Error('Swagger is invalid!') | ||
}) | ||
} |