mirror of https://github.com/Seich/Beau.git
Removed the validate command.
We'll be replacing it with jsonschema soon.
This commit is contained in:
parent
614b1bf966
commit
df41d4fa0b
|
|
@ -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.
|
|
||||||
",
|
|
||||||
]
|
|
||||||
`;
|
|
||||||
|
|
@ -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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -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;
|
|
||||||
|
|
@ -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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -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 };
|
|
||||||
Loading…
Reference in New Issue