diff --git a/bin/cli/base.js b/bin/cli/base.js index 1e1dd3f..35dee02 100644 --- a/bin/cli/base.js +++ b/bin/cli/base.js @@ -4,6 +4,10 @@ const fs = require('fs') const path = require('path') const dotenv = require('dotenv') 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 { openConfigFile(configFile) { @@ -13,6 +17,13 @@ class Base extends Command { let config 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') { config = doc } else { diff --git a/examples/httpbin.yml b/examples/httpbin.yml index fffb8b9..3210d24 100644 --- a/examples/httpbin.yml +++ b/examples/httpbin.yml @@ -7,9 +7,10 @@ environment: name: David GET /anything: - alias: anything - payload: - name: $env.params.name + - alias: anything + payload: + name: $env.params.name + - alias: anything2 GET /cookies/set: alias: set-cookies diff --git a/schema.json b/schema.json index 97dbce4..03a2644 100644 --- a/schema.json +++ b/schema.json @@ -9,50 +9,54 @@ { "type": "string" }, { "type": "object", - "required": ["alias"], - "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"] } } - ] + "$ref": "#/definitions/requestObject", + "required": ["alias"] } ] + }, + "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": { @@ -68,7 +72,7 @@ "type": "boolean" }, "defaults": { - "$ref": "#/definitions/request" + "$ref": "#/definitions/requestObject" }, "plugins": { "type": "array",