added apibuilder/validators endpoint to linter
nclazz/api-cli/pipeline/head There was a failure building this commit Details

develop
Niclas Thobaben 2021-07-08 23:49:02 +02:00
parent 017130ecac
commit dfa73d1256
4 changed files with 38 additions and 23 deletions

View File

@ -2,13 +2,13 @@ const linter = require('../linter')
const path = require('path')
module.exports = {
run(args) {
async run(args) {
if(!args.spec) {
throw 'No spec provided!'
}
let spec = require(path.resolve(process.cwd(), args.spec))
let result = linter.lint(spec)
let result = await linter.lint(spec)
if(!result.success) {
console.error('Errors while linting!')
process.exit(-1)

View File

@ -37,10 +37,14 @@ function regex(regex) {
}
async function validateOnApiBuilder(spec) {
let res = await axios.post(`${apibuilder_baseurl}/validations`, {
data: spec
})
console.log(res)
try {
let res = await axios.post(`${apibuilder_baseurl}/validations`, spec)
return res.data
}catch(err) {
return err.response.data
}
}
const linterMappings = [
@ -64,10 +68,17 @@ const linterMappings = [
module.exports.validate = validateOnApiBuilder
module.exports.lint = function(spec) {
module.exports.lint = async function(spec) {
let results = {
success: true
}
let apibuilder_result = await validateOnApiBuilder(spec)
if(!apibuilder_result) {
results.success = false
results.apibuilder = apibuilder_result.errors
apibuilder_result.errors.forEach(console.error)
}
linterMappings.forEach(element => {
let value = objectPath.get(spec, element.path);
element.linters.forEach(linter => {

View File

@ -1,6 +1,8 @@
{
"name": "",
"description": "",
"apidoc": {
},
"name": "service",
"description": "description",
"info": {},
"imports": [],
"headers": [],
@ -18,18 +20,19 @@
"resources": {
"healthcheck": {
"path": "/_internal_/healthcheck",
"operations": {
"method": "GET",
"path": "/healthcheck",
"description": "Simple healthcheck endpoint to test the status of the service.",
"responses": {
"200": { "type": "healthcheck", "description": "Get the current health status of the service." }
"operations": [
{
"method": "GET",
"path": "/healthcheck",
"description": "Simple healthcheck endpoint to test the status of the service.",
"responses": {
"200": { "type": "healthcheck", "description": "Get the current health status of the service." }
}
}
}
]
}
},
"attributes": {},
"annotations": {
}
"attributes": [],
"annotations": {}
}

View File

@ -52,8 +52,9 @@ describe('linter for spec', () => {
})
})
describe('apibuilder validator', () => {
it('validates the spec', () => {
linter.validate(require('../templates/default.json'))
describe('apibuilder validator', async () => {
await it('validates the spec', async () => {
let result = await linter.validate(require('../templates/default.json'))
expect(result.valid).to.be.true
})
})