Config files are now validated against the json schema.

This commit is contained in:
David Diaz 2020-11-19 21:44:17 -06:00
parent 5029a09b41
commit a39f3b5977
3 changed files with 62 additions and 46 deletions

View File

@ -4,6 +4,10 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const dotenv = require('dotenv') const dotenv = require('dotenv')
const Beau = require('../../src/beau') const Beau = require('../../src/beau')
const Ajv = require('ajv').default
const ajv = new Ajv()
const validate = ajv.compile(require('../../schema.json'))
class Base extends Command { class Base extends Command {
openConfigFile(configFile) { openConfigFile(configFile) {
@ -13,6 +17,13 @@ class Base extends Command {
let config let config
yaml.safeLoadAll(fs.readFileSync(configFile, 'utf-8'), (doc) => { yaml.safeLoadAll(fs.readFileSync(configFile, 'utf-8'), (doc) => {
const valid = validate(doc)
if (!valid) {
this.log(validate.errors)
this.error(`The configuration file is not valid.`)
}
if (typeof config === 'undefined') { if (typeof config === 'undefined') {
config = doc config = doc
} else { } else {

View File

@ -7,9 +7,10 @@ environment:
name: David name: David
GET /anything: GET /anything:
alias: anything - alias: anything
payload: payload:
name: $env.params.name name: $env.params.name
- alias: anything2
GET /cookies/set: GET /cookies/set:
alias: set-cookies alias: set-cookies

View File

@ -9,50 +9,54 @@
{ "type": "string" }, { "type": "string" },
{ {
"type": "object", "type": "object",
"required": ["alias"], "$ref": "#/definitions/requestObject",
"properties": { "required": ["alias"]
"alias": {
"type": "string",
"description": "The name of this request."
},
"headers": {
"type": "object",
"additionalProperties": true,
"description": "Headers that are part of this request."
},
"params": {
"type": "object",
"additionalProperties": true,
"description": "Query String parameters to add to this request."
},
"payload": {
"description": "This request's body. It is converted to json automatically if given an object. It's sent as a string otherwise.",
"oneOf": [
{ "type": "string" },
{
"type": "object",
"additionalProperties": true
}
]
},
"form": {
"type": "object",
"additionalProperties": true,
"description": "This request's body. Sets the content-type to application/x-www-form-urlencoded."
},
"formdata": {
"type": "object",
"additionalProperties": true,
"description": "This request's body. Sets the content-type to multipart/form-data."
}
},
"allOf": [
{ "not": { "required": ["payload", "form"] } },
{ "not": { "required": ["payload", "formdata"] } },
{ "not": { "required": ["formdata", "form"] } }
]
} }
] ]
},
"requestObject": {
"type": "object",
"properties": {
"alias": {
"type": "string",
"description": "The name of this request."
},
"headers": {
"type": "object",
"additionalProperties": true,
"description": "Headers that are part of this request."
},
"params": {
"type": "object",
"additionalProperties": true,
"description": "Query String parameters to add to this request."
},
"payload": {
"description": "This request's body. It is converted to json automatically if given an object. It's sent as a string otherwise.",
"oneOf": [
{ "type": "string" },
{
"type": "object",
"additionalProperties": true
}
]
},
"form": {
"type": "object",
"additionalProperties": true,
"description": "This request's body. Sets the content-type to application/x-www-form-urlencoded."
},
"formdata": {
"type": "object",
"additionalProperties": true,
"description": "This request's body. Sets the content-type to multipart/form-data."
}
},
"allOf": [
{ "not": { "required": ["payload", "form"] } },
{ "not": { "required": ["payload", "formdata"] } },
{ "not": { "required": ["formdata", "form"] } }
]
} }
}, },
"properties": { "properties": {
@ -68,7 +72,7 @@
"type": "boolean" "type": "boolean"
}, },
"defaults": { "defaults": {
"$ref": "#/definitions/request" "$ref": "#/definitions/requestObject"
}, },
"plugins": { "plugins": {
"type": "array", "type": "array",