diff --git a/ExpressAPI/assets/OpenAPI/OpenAPI.yaml b/ExpressAPI/assets/OpenAPI/OpenAPI.yaml index 2bbe1d3a26375c79d0b02d84bb0773b74d13712c..b4d9846f36a75a1b46f1a023543eb92c7137588f 100644 --- a/ExpressAPI/assets/OpenAPI/OpenAPI.yaml +++ b/ExpressAPI/assets/OpenAPI/OpenAPI.yaml @@ -1,7 +1,7 @@ openapi: 3.1.0 info: title: Dojo API - version: 3.4.2 + version: 3.5.0 description: | **Backend API of the Dojo project.** diff --git a/ExpressAPI/src/helpers/DojoValidators.ts b/ExpressAPI/src/helpers/DojoValidators.ts index 98a28252c6bca786fe23ad6b08e56db54c33aca0..38465417c46274cff0e46ee3dbdc2929cb17f05b 100644 --- a/ExpressAPI/src/helpers/DojoValidators.ts +++ b/ExpressAPI/src/helpers/DojoValidators.ts @@ -7,6 +7,8 @@ import express from 'expres import logger from '../shared/logging/WinstonLogger'; import Json5FileValidator from '../shared/helpers/Json5FileValidator'; import ExerciseResultsFile from '../shared/types/Dojo/ExerciseResultsFile'; +import ParamsCallbackManager from '../middlewares/ParamsCallbackManager'; +import ExerciseManager from '../managers/ExerciseManager'; declare type DojoMeta = Meta & { @@ -106,6 +108,32 @@ class DojoValidators { }); } }); + + readonly exerciseIdOrUrlValidator = this.toValidatorSchemaOptions({ + bail : true, + errorMessage: 'ExerciseIdOrUrl: not provided or invalid', + options : (_value, { + req, + path + }) => { + return new Promise((resolve, reject) => { + const exerciseIdOrUrl = this.getParamValue(req, path) as string; + if ( exerciseIdOrUrl ) { + ParamsCallbackManager.initBoundParams(req); + + ExerciseManager.get(exerciseIdOrUrl).then((exercise) => { + req.boundParams.exercise = exercise; + + exercise !== undefined ? resolve(true) : reject(); + }).catch(() => { + reject(); + }); + } else { + reject(); + } + }); + } + }); }