refactor: adjust code to new configuration

This commit is contained in:
Karl-Philipp Wulfert
2019-06-05 17:13:28 +02:00
parent e70d5dccab
commit 4d4f7bf7ac
9 changed files with 221 additions and 118 deletions

View File

@@ -12,10 +12,10 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as ajv from 'ajv';
import * as Ajv from 'ajv';
import {Schema as JSONSchema} from 'jsonschema';
import {join} from 'path';
import {DEFAULT_CONFIG, SchemaGenerator} from 'ts-json-schema-generator';
import {DEFAULT_CONFIG, Definition, SchemaGenerator} from 'ts-json-schema-generator';
import {createFormatter} from 'ts-json-schema-generator/dist/factory/formatter';
import {createParser} from 'ts-json-schema-generator/dist/factory/parser';
import {createProgram} from 'ts-json-schema-generator/dist/factory/program';
@@ -28,8 +28,15 @@ import {getTsconfigPath, isSchemaWithDefinitions} from './common';
* Converts TypeScript source files to JSON schema files
*/
export class Converter {
private generator: SchemaGenerator;
private schemaValidator: ajv.Ajv;
/**
* Generator instance
*/
private readonly generator: SchemaGenerator;
/**
* Schema validator instance
*/
private readonly schemaValidator: Ajv.Ajv;
/**
* Create a new converter
@@ -58,24 +65,24 @@ export class Converter {
createFormatter(config),
);
// create ajv instance
this.schemaValidator = new ajv();
// create Ajv instance
this.schemaValidator = new Ajv();
this.schemaValidator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
}
/**
* Get schema for specific StAppsCore type
*
* @param {string} type Type to get the schema for
* @param {string} version Version to set for the schema
* @returns {Schema} Generated schema
* @param type Type to get the schema for
* @param version Version to set for the schema
* @returns Generated schema
*/
getSchema(type: string, version: string): JSONSchema {
// generate schema for this file/type
const schema: JSONSchema = this.generator.createSchema(type);
// set id of schema
schema.id = 'https://core.stapps.tu-berlin.de/v' + version + '/lib/schema/' + type + '.json';
schema.id = `https://core.stapps.tu-berlin.de/v${version}/lib/schema/${type}.json`;
if (isSchemaWithDefinitions(schema)) {
const selfReference = {
@@ -88,7 +95,10 @@ export class Converter {
delete selfReference.id;
// add self reference to definitions
schema.definitions['SC' + type] = Object.assign({}, selfReference as any);
schema.definitions[`SC${type}`] = {
...{},
...selfReference as unknown as Definition,
};
}
if (!this.schemaValidator.validateSchema(schema)) {
@@ -119,7 +129,7 @@ export function getValidatableTypesFromReflection(projectReflection: ProjectRefl
// check if type has annotation @validatable
if (typeof type.comment === 'object'
&& Array.isArray(type.comment.tags)
&& type.comment.tags.find((tag) => tag.tagName === 'validatable')) {
&& type.comment.tags.findIndex((tag) => tag.tagName === 'validatable') >= 0) {
// add type to list
validatableTypes.push(type.name);
}