diff --git a/helpers/Dojo/SharedAssignmentHelper.ts b/helpers/Dojo/SharedAssignmentHelper.ts index 94e50051bbb79fcc7ddb20619f8ac34b183cf4ae..b103f8cc526ed60a9aa6b7c5276557bcf1ab6635 100644 --- a/helpers/Dojo/SharedAssignmentHelper.ts +++ b/helpers/Dojo/SharedAssignmentHelper.ts @@ -1,75 +1,21 @@ -import Ajv, { ErrorObject, JTDSchemaType } from 'ajv/dist/jtd'; -import fs from 'fs'; -import JSON5 from 'json5'; -import AssignmentFile from '../../types/Dojo/AssignmentFile'; -import GitlabPipelineStatus from '../../types/Gitlab/GitlabPipelineStatus'; -import DojoStatusCode from '../../types/Dojo/DojoStatusCode'; -import GitlabPipeline from '../../types/Gitlab/GitlabPipeline'; -import SharedGitlabManager from '../../managers/SharedGitlabManager'; +import AssignmentFile from '../../types/Dojo/AssignmentFile'; +import GitlabPipelineStatus from '../../types/Gitlab/GitlabPipelineStatus'; +import DojoStatusCode from '../../types/Dojo/DojoStatusCode'; +import GitlabPipeline from '../../types/Gitlab/GitlabPipeline'; +import SharedGitlabManager from '../../managers/SharedGitlabManager'; +import Json5FileValidator from '../Json5FileValidator'; class SharedAssignmentHelper { - private validateDescriptionFileV1(resultsFilePathOrStr: string, isFile: boolean = true): { results: AssignmentFile | undefined, isValid: boolean, errors: Array<ErrorObject | string> | null | undefined } { - const ajv = new Ajv(); - - const schema: JTDSchemaType<AssignmentFile> = { - properties : { - dojoAssignmentVersion: { type: 'uint32' }, - version : { type: 'uint32' }, - - immutable: { - elements: { - properties : { - path: { type: 'string' } - }, - optionalProperties: { - description: { type: 'string' }, - isDirectory: { type: 'boolean' } - } - } - }, - - result: { - properties : { - container: { type: 'string' } - }, - optionalProperties: { - volume: { type: 'string' } - } - } - }, - additionalProperties: false - }; - - const validator = ajv.compile(schema); - - try { - const results = JSON5.parse(isFile ? fs.readFileSync(resultsFilePathOrStr, 'utf8') : resultsFilePathOrStr); - const isValid = validator(results); - - return { - results: isValid ? results : results as AssignmentFile, - isValid: isValid, - errors : validator.errors - }; - } catch ( error ) { - return { - results: undefined, - isValid: false, - errors : [ `JSON5 invalid : ${ JSON.stringify(error) }` ] - }; - } - } - - validateDescriptionFile(resultsFilePathOrStr: string, isFile: boolean = true, version: number = 1): { results: AssignmentFile | undefined, isValid: boolean, errors: Array<ErrorObject | string> | null | undefined } { + validateDescriptionFile(filePathOrStr: string, isFile: boolean = true, version: number = 1): { content: AssignmentFile | undefined, isValid: boolean, error: string | null } { switch ( version ) { case 1: - return this.validateDescriptionFileV1(resultsFilePathOrStr, isFile); + return Json5FileValidator.validateFile(AssignmentFile, filePathOrStr, isFile); default: return { - results: undefined, + content: undefined, isValid: false, - errors : [ `Version ${ version } not supported` ] + error : `Version ${ version } not supported` }; } }