@vulcan-sql/build
Version:
VulcanSQL package for building projects
115 lines • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = exports.getSchemas = void 0;
const tslib_1 = require("tslib");
/* eslint @nrwl/nx/enforce-module-boundaries: 0 */
/* istanbul ignore file */
const glob = require("glob");
const path = require("path");
const fs_1 = require("fs");
const core_1 = require("@vulcan-sql/core");
const jsYaml = require("js-yaml");
const lodash_1 = require("lodash");
const build_1 = require("../../../src/index");
const compose = require("koa-compose");
const middleware_1 = require("../../../src/lib/schema-parser/middleware");
const sinon = require("ts-sinon");
const inversify_1 = require("inversify");
const getSchemaPaths = () => new Promise((resolve, reject) => {
glob(path.resolve(__dirname, 'schemas', '*.yaml').split(path.sep).join('/'), (err, paths) => {
if (err)
return reject(err);
resolve((0, lodash_1.sortBy)(paths));
});
});
class MockValidator extends core_1.InputValidator {
constructor(name, constraintsFn) {
super({}, name);
this.name = name;
this.constraintsFn = constraintsFn;
}
validateData() {
return;
}
validateSchema() {
return;
}
getConstraints(args) {
return this.constraintsFn(args);
}
}
const getStubLoader = () => {
const validatorLoader = sinon.stubInterface();
validatorLoader.getValidator.callsFake((name) => {
switch (name) {
case 'required':
return new MockValidator('required', () => [core_1.Constraint.Required()]);
case 'minValue':
return new MockValidator('minValue', (args) => [
core_1.Constraint.MinValue(args.value),
]);
case 'maxValue':
return new MockValidator('maxValue', (args) => [
core_1.Constraint.MaxValue(args.value),
]);
case 'minLength':
return new MockValidator('minLength', (args) => [
core_1.Constraint.MinLength(args.value),
]);
case 'maxLength':
return new MockValidator('maxLength', (args) => [
core_1.Constraint.MaxLength(args.value),
]);
case 'regex':
return new MockValidator('regex', (args) => [
core_1.Constraint.Regex(args.value),
]);
case 'enum':
return new MockValidator('enum', (args) => [
core_1.Constraint.Enum(args.value),
]);
default:
throw new Error(`Validator ${name} is not implemented in test bed.`);
}
});
return validatorLoader;
};
const getSchemas = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const schemas = [];
const paths = yield getSchemaPaths();
for (const schemaFile of paths) {
const content = yield fs_1.promises.readFile(schemaFile, 'utf-8');
schemas.push(jsYaml.load(content));
}
const loader = getStubLoader();
const templateEngine = sinon.stubInterface();
const container = new inversify_1.Container();
container.bind(core_1.TYPES.ValidatorLoader).toConstantValue(loader);
container.bind(core_1.TYPES.TemplateEngine).toConstantValue(templateEngine);
container.bind(core_1.TYPES.Factory_DataSource).toConstantValue(() => ({}));
middleware_1.SchemaParserMiddlewares.forEach((middleware) => {
container.bind(build_1.TYPES.SchemaParserMiddleware).to(middleware);
});
const execute = compose(container
.getAll(build_1.TYPES.SchemaParserMiddleware)
.map((middleware) => middleware.handle.bind(middleware)));
for (const schema of schemas) {
yield execute(schema);
}
return schemas;
});
exports.getSchemas = getSchemas;
const getConfig = () => {
return {
name: 'An API schema for testing',
version: '1.2.3',
description: `Some description with **markdown** supported.`,
// We don't care about the options of these components.
template: {},
artifact: {},
'schema-parser': {},
extensions: {},
};
};
exports.getConfig = getConfig;
//# sourceMappingURL=schema.js.map