Files
openstapps/src/schema.ts
2019-06-05 17:25:02 +02:00

142 lines
4.2 KiB
TypeScript

/*
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* 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 {Schema as JSONSchema} from 'jsonschema';
import {join} from 'path';
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';
import {ProjectReflection} from 'typedoc';
import {getTsconfigPath, isSchemaWithDefinitions} from './common';
/**
* StAppsCore converter
*
* Converts TypeScript source files to JSON schema files
*/
export class Converter {
/**
* Generator instance
*/
private readonly generator: SchemaGenerator;
/**
* Schema validator instance
*/
private readonly schemaValidator: Ajv.Ajv;
/**
* Create a new converter
*
* @param path Path to the project
*/
constructor(path: string) {
// set config for schema generator
const config = {
...DEFAULT_CONFIG,
// expose: 'exported' as any,
// jsDoc: 'extended' as any,
path: join(getTsconfigPath(path), 'tsconfig.json'),
sortProps: true,
topRef: false,
type: 'SC',
};
// create TypeScript program from config
const program = createProgram(config);
// create generator
this.generator = new SchemaGenerator(
program,
createParser(program, config),
createFormatter(config),
);
// 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 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`;
if (isSchemaWithDefinitions(schema)) {
const selfReference = {
...{},
...schema,
};
delete selfReference.$schema;
delete selfReference.definitions;
delete selfReference.id;
// add self reference to definitions
schema.definitions[`SC${type}`] = {
...{},
...selfReference as unknown as Definition,
};
}
if (!this.schemaValidator.validateSchema(schema)) {
throw new Error(`Generated schema for ${type} is invalid!`);
}
return schema;
}
}
/**
* Get a list of validatable types from a reflection
*
* @param projectReflection Reflection to get validatable types from
*/
export function getValidatableTypesFromReflection(projectReflection: ProjectReflection): string[] {
const validatableTypes: string[] = [];
if (typeof projectReflection.children === 'undefined') {
throw new Error('Project reflection doesn\'t contain any modules.');
}
// iterate over modules
projectReflection.children.forEach((module) => {
if (Array.isArray(module.children) && module.children.length > 0) {
// iterate over types
module.children.forEach((type) => {
// check if type has annotation @validatable
if (typeof type.comment === 'object'
&& Array.isArray(type.comment.tags)
&& type.comment.tags.findIndex((tag) => tag.tagName === 'validatable') >= 0) {
// add type to list
validatableTypes.push(type.name);
}
});
}
});
return validatableTypes;
}