From df41d4fa0bca651ba63a40d02bcff97805b93649 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Wed, 18 Nov 2020 18:37:00 -0600 Subject: [PATCH] Removed the validate command. We'll be replacing it with jsonschema soon. --- .../__snapshots__/validate.spec.js.snap | 8 -- bin/cli/__tests__/validate.spec.js | 27 ------- bin/cli/commands/validate.js | 30 -------- src/__tests__/schema.spec.js | 15 ---- src/schema.js | 77 ------------------- schema.json => src/schema.json | 0 6 files changed, 157 deletions(-) delete mode 100644 bin/cli/__tests__/__snapshots__/validate.spec.js.snap delete mode 100644 bin/cli/__tests__/validate.spec.js delete mode 100644 bin/cli/commands/validate.js delete mode 100644 src/__tests__/schema.spec.js delete mode 100644 src/schema.js rename schema.json => src/schema.json (100%) diff --git a/bin/cli/__tests__/__snapshots__/validate.spec.js.snap b/bin/cli/__tests__/__snapshots__/validate.spec.js.snap deleted file mode 100644 index f7f7fbb..0000000 --- a/bin/cli/__tests__/__snapshots__/validate.spec.js.snap +++ /dev/null @@ -1,8 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Validate Command should validate the configuration file 1`] = ` -Array [ - "beau.yml is valid. -", -] -`; diff --git a/bin/cli/__tests__/validate.spec.js b/bin/cli/__tests__/validate.spec.js deleted file mode 100644 index 8f99821..0000000 --- a/bin/cli/__tests__/validate.spec.js +++ /dev/null @@ -1,27 +0,0 @@ -const ValidateCommand = require('../commands/validate'); - -jest.mock('../utils'); - -describe('Validate Command', () => { - let result; - - beforeEach(() => { - result = []; - jest.spyOn(process.stdout, 'write').mockImplementation(val => - result.push(require('strip-ansi')(val.toString('utf8'))) - ); - }); - - afterEach(() => jest.restoreAllMocks()); - - it('should validate the configuration file', async () => { - await ValidateCommand.run([]); - expect(result).toMatchSnapshot(); - }); - - it('should show schema errors', async () => { - await expect( - ValidateCommand.run(['invalid-conf.yml']) - ).rejects.toThrow(); - }); -}); diff --git a/bin/cli/commands/validate.js b/bin/cli/commands/validate.js deleted file mode 100644 index 311ad7b..0000000 --- a/bin/cli/commands/validate.js +++ /dev/null @@ -1,30 +0,0 @@ -const { flags, Command } = require('@oclif/command'); -const { baseFlags, openConfigFile } = require('../utils'); -const { validate } = require('../../../src/schema.js'); - -class ValidateCommand extends Command { - async run() { - const { flags, args } = this.parse(ValidateCommand); - const configFile = args.alias || flags.config; - - const config = openConfigFile(configFile); - - let result = await validate(config); - if (result.valid) { - this.log(`${configFile} is valid.`); - } else { - this.error(result.message); - } - } -} - -ValidateCommand.description = `Validates the given configuration file against Beau's configuration schema.`; -ValidateCommand.flags = { ...baseFlags }; -ValidateCommand.args = [ - { - name: 'alias', - required: false, - description: `The configuration file to validate.` - } -]; -module.exports = ValidateCommand; diff --git a/src/__tests__/schema.spec.js b/src/__tests__/schema.spec.js deleted file mode 100644 index c4e818b..0000000 --- a/src/__tests__/schema.spec.js +++ /dev/null @@ -1,15 +0,0 @@ -const schema = require('../schema'); - -describe('Schema', () => { - it(`should validate an object against the schema`, async () => { - await expect( - schema.validate({ endpoint: 'http://example.com' }) - ).resolves.toHaveProperty('valid', true); - }); - - it(`should indicate the error when an schema is invalid`, async () => { - await expect( - schema.validate({ plugins: [{ hello: 1, world: 2 }] }) - ).resolves.toHaveProperty('valid', false); - }); -}); diff --git a/src/schema.js b/src/schema.js deleted file mode 100644 index 7c1a3fc..0000000 --- a/src/schema.js +++ /dev/null @@ -1,77 +0,0 @@ -const Joi = require('joi'); -const { requestRegex } = require('./shared.js'); - -const pluginSchema = [ - Joi.string(), - Joi.object() - .keys(null) - .max(1) -]; - -const requestSchema = [ - Joi.object() - .keys({ - HEADERS: Joi.object().keys(null), - PAYLOAD: [Joi.object().keys(null), Joi.string()], - PARAMS: Joi.object().keys(null), - FORM: Joi.object().keys(null), - ALIAS: Joi.string().required(), - FORMDATA: Joi.object().keys(null) - }) - .without('FORM', ['PAYLOAD', 'FORMDATA']) - .without('PAYLOAD', ['FORM', 'FORMDATA']) - .without('FORMDATA', ['FORM', 'PAYLOAD']) - .rename(/headers/i, 'HEADERS', { override: true }) - .rename(/payload/i, 'PAYLOAD', { override: true }) - .rename(/params/i, 'PARAMS', { override: true }) - .rename(/form/i, 'FORM', { override: true }) - .rename(/alias/i, 'ALIAS', { override: true }), - - Joi.string() -]; - -const hostSchema = Joi.object() - .keys({ - HOST: Joi.string().required(), - ENDPOINT: Joi.string(), - DEFAULTS: Joi.object().keys(null) - }) - .pattern(requestRegex, requestSchema) - .rename(/host/i, 'HOST', { override: true }) - .rename(/defaults/i, 'DEFAULTS', { override: true }) - .rename(/endpoint/i, 'ENDPOINT', { override: true }); - -const schema = Joi.object() - .keys({ - VERSION: Joi.number().integer(), - ENDPOINT: Joi.string().uri(), - PLUGINS: Joi.array().items(pluginSchema), - DEFAULTS: Joi.object(), - ENVIRONMENT: Joi.object(), - HOSTS: Joi.array().items(hostSchema), - COOKIEJAR: Joi.boolean() - }) - .pattern(requestRegex, requestSchema) - .rename(/version/i, 'VERSION', { override: true }) - .rename(/endpoint/i, 'ENDPOINT', { override: true }) - .rename(/hosts/i, 'HOSTS', { override: true }) - .rename(/plugins/i, 'PLUGINS', { override: true }) - .rename(/defaults/i, 'DEFAULTS', { override: true }) - .rename(/environment/i, 'ENVIRONMENT', { override: true }) - .rename(/cookiejar/i, 'COOKIEJAR', { override: true }); - -const validate = async function(config) { - try { - await Joi.validate(config, schema, { allowUnknown: true }); - return { valid: true }; - } catch ({ name, details }) { - return { - valid: false, - message: `${name}: \n ${details - .map(d => d.message + ' @ ' + d.path) - .join(' \n ')}` - }; - } -}; - -module.exports = { schema, validate }; diff --git a/schema.json b/src/schema.json similarity index 100% rename from schema.json rename to src/schema.json