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

DojoValidators => Add validator for exerciseIdOrUrl

parent 42c3a728
No related branches found
No related tags found
No related merge requests found
openapi: 3.1.0 openapi: 3.1.0
info: info:
title: Dojo API title: Dojo API
version: 3.4.2 version: 3.5.0
description: | description: |
**Backend API of the Dojo project.** **Backend API of the Dojo project.**
......
...@@ -7,6 +7,8 @@ import express from 'expres ...@@ -7,6 +7,8 @@ import express from 'expres
import logger from '../shared/logging/WinstonLogger'; import logger from '../shared/logging/WinstonLogger';
import Json5FileValidator from '../shared/helpers/Json5FileValidator'; import Json5FileValidator from '../shared/helpers/Json5FileValidator';
import ExerciseResultsFile from '../shared/types/Dojo/ExerciseResultsFile'; import ExerciseResultsFile from '../shared/types/Dojo/ExerciseResultsFile';
import ParamsCallbackManager from '../middlewares/ParamsCallbackManager';
import ExerciseManager from '../managers/ExerciseManager';
declare type DojoMeta = Meta & { declare type DojoMeta = Meta & {
...@@ -106,6 +108,32 @@ class DojoValidators { ...@@ -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();
}
});
}
});
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment