Skip to content
Snippets Groups Projects
Commit f552a2cf authored by michael.minelli's avatar michael.minelli
Browse files

Add ExerciceHelper with results json file validation

parent 0c9528be
No related branches found
No related tags found
No related merge requests found
import Ajv, { ErrorObject, JTDSchemaType } from 'ajv/dist/jtd';
import fs from 'fs-extra';
import ExerciceResultsFile from '../types/Dojo/ExerciceResultsFile';
import hjson from 'hjson';
class ExerciceHelper {
validateResultFile(resultsFilePath: string): {
results: ExerciceResultsFile, isValid: boolean, errors: ErrorObject<string, Record<string, any>, unknown>[] | null | undefined
} {
const ajv = new Ajv();
const schema: JTDSchemaType<ExerciceResultsFile> = {
properties : {
success: { type: 'boolean' }
},
optionalProperties : {
successfulTests: { type: 'uint32' },
failedTests : { type: 'uint32' },
successfulTestsList: {
elements: {
type: 'string'
}
},
failedTestsList : {
elements: {
type: 'string'
}
}
},
additionalProperties: false
};
const validator = ajv.compile(schema);
const results = hjson.parse(fs.readFileSync(resultsFilePath, 'utf8'));
const isValid = validator(results);
return {
results: isValid ? results as ExerciceResultsFile : results as any,
isValid: isValid,
errors : validator.errors
};
}
}
export default new ExerciceHelper();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment