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') const path = require('path')
module.exports = { module.exports = {
run(args) { async run(args) {
if(!args.spec) { if(!args.spec) {
throw 'No spec provided!' throw 'No spec provided!'
} }
let spec = require(path.resolve(process.cwd(), args.spec)) let spec = require(path.resolve(process.cwd(), args.spec))
let result = linter.lint(spec) let result = await linter.lint(spec)
if(!result.success) { if(!result.success) {
console.error('Errors while linting!') console.error('Errors while linting!')
process.exit(-1) process.exit(-1)

View File

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

View File

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

View File

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