Skip to content

Commit

Permalink
Adding the Swagger validator for both YAML and JSON formatted specifi…
Browse files Browse the repository at this point in the history
…cations.
  • Loading branch information
khalidx committed Jul 24, 2017
1 parent 5604a67 commit 15689a6
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/content/handler/swagger/2/index.ts
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!')
})
}

0 comments on commit 15689a6

Please sign in to comment.