mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 00:23:03 +00:00
refactor: adjust code to new configuration
This commit is contained in:
@@ -18,12 +18,7 @@ import {PathLike} from 'fs';
|
||||
import {Schema, Validator as JSONSchemaValidator, ValidatorResult} from 'jsonschema';
|
||||
import * as mustache from 'mustache';
|
||||
import {basename, join, resolve} from 'path';
|
||||
import {
|
||||
ExpectableValidationErrors,
|
||||
globPromisified,
|
||||
readFilePromisified,
|
||||
writeFilePromisified,
|
||||
} from './common';
|
||||
import {ExpectableValidationErrors, globPromisified, readFilePromisified, writeFilePromisified} from './common';
|
||||
|
||||
/**
|
||||
* StAppsCore validator
|
||||
@@ -32,7 +27,7 @@ export class Validator {
|
||||
/**
|
||||
* Map of schema names to schemas
|
||||
*/
|
||||
private readonly schemas: { [type: string]: Schema } = {};
|
||||
private readonly schemas: { [type: string]: Schema; } = {};
|
||||
|
||||
/**
|
||||
* JSONSchema validator instance
|
||||
@@ -60,7 +55,7 @@ export class Validator {
|
||||
|
||||
Logger.log(`Adding schemas from ${schemaDir} to validator.`);
|
||||
|
||||
// Iterate over schema files
|
||||
// tslint:disable-next-line:no-magic-numbers - iterate over schema files
|
||||
await asyncPool(2, schemaFiles, async (file) => {
|
||||
// read schema file
|
||||
const buffer = await readFilePromisified(file);
|
||||
@@ -84,7 +79,7 @@ export class Validator {
|
||||
* @param instance Instance to validate
|
||||
* @param schema Name of schema to validate instance against or the schema itself
|
||||
*/
|
||||
public validate(instance: any, schema: string | Schema): ValidatorResult {
|
||||
public validate(instance: unknown, schema: string | Schema): ValidatorResult {
|
||||
if (typeof schema === 'string') {
|
||||
// if you want to access a schema that is contained in the validator object
|
||||
if (typeof this.schemas[schema] !== 'object') {
|
||||
@@ -92,10 +87,10 @@ export class Validator {
|
||||
}
|
||||
|
||||
return this.validator.validate(instance, this.schemas[schema]);
|
||||
} else {
|
||||
// if you have a schema and want to validate it directly
|
||||
return this.validator.validate(instance, schema);
|
||||
}
|
||||
|
||||
// if you have a schema and want to validate it directly
|
||||
return this.validator.validate(instance, schema);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +117,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
||||
// map of errors per file
|
||||
const errors: ExpectableValidationErrors = {};
|
||||
|
||||
// iterate over files to test
|
||||
// tslint:disable-next-line:no-magic-numbers - iterate over files to test
|
||||
await asyncPool(2, testFiles, async (testFile) => {
|
||||
const testFileName = basename(testFile);
|
||||
|
||||
@@ -145,7 +140,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
||||
errors[testFileName] = [];
|
||||
|
||||
// iterate over errors
|
||||
result.errors.forEach((error) => {
|
||||
for (const error of result.errors) {
|
||||
// get idx of expected error
|
||||
const errorIdx = expectedErrors.indexOf(error.name);
|
||||
let expected = false;
|
||||
@@ -156,7 +151,7 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
||||
expected = true;
|
||||
} else {
|
||||
unexpectedErrors++;
|
||||
Logger.error(`Unexpected error ${error.name} in ${testFile}`);
|
||||
await Logger.error(`Unexpected error ${error.name} in ${testFile}`);
|
||||
}
|
||||
|
||||
// add error to list of errors
|
||||
@@ -164,12 +159,13 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
||||
...error,
|
||||
expected,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (expectedErrors.length > 0) {
|
||||
expectedErrors.forEach((error) => {
|
||||
Logger.error(`Extraneous expected error '${error}' in ${testFile}.`);
|
||||
for (const error of expectedErrors) {
|
||||
await Logger.error(`Extraneous expected error '${error}' in ${testFile}.`);
|
||||
|
||||
errors[testFileName].push({
|
||||
argument: false,
|
||||
expected: false,
|
||||
@@ -177,9 +173,9 @@ export async function validateFiles(schemaDir: string, resourcesDir: string): Pr
|
||||
message: `expected error ${error} did not occur`,
|
||||
name: `expected ${error}`,
|
||||
property: 'unknown',
|
||||
schema: undefined as any,
|
||||
schema: 'undefined',
|
||||
});
|
||||
});
|
||||
}
|
||||
} else if (unexpectedErrors === 0) {
|
||||
Logger.info(`Successfully validated ${testFile}.`);
|
||||
}
|
||||
@@ -203,15 +199,21 @@ export async function writeReport(reportPath: PathLike, errors: ExpectableValida
|
||||
|
||||
let output = '';
|
||||
|
||||
Object.keys(errors).forEach((fileName) => {
|
||||
for (const fileName in errors) {
|
||||
if (!errors.hasOwnProperty(fileName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let fileOutput = '';
|
||||
|
||||
errors[fileName].forEach((error, idx) => {
|
||||
fileOutput += mustache.render(errorTemplate, {
|
||||
idx: idx + 1,
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
instance: JSON.stringify(error.instance, null, 2),
|
||||
message: error.message,
|
||||
property: error.property,
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
schema: JSON.stringify(error.schema, null, 2),
|
||||
status: (error.expected) ? 'alert-success' : 'alert-danger',
|
||||
});
|
||||
@@ -221,7 +223,7 @@ export async function writeReport(reportPath: PathLike, errors: ExpectableValida
|
||||
errors: fileOutput,
|
||||
testFile: fileName,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
buffer = await readFilePromisified(resolve(__dirname, '..', 'resources', 'report.html.mustache'));
|
||||
const reportTemplate = buffer.toString();
|
||||
|
||||
Reference in New Issue
Block a user