Skip to content

Commit

Permalink
feat: grading script validation api end point
Browse files Browse the repository at this point in the history
  • Loading branch information
williams-jack committed Aug 28, 2024
1 parent 813cb17 commit 3a4757d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ export const gradingJobValidation = async (req: Request, res: Response) => {
res.json({ "message": "Provided request body is a valid grading job configuration." }) :
res.json({ "errors": formatValidationErrors(validator.errors) });
}

export const gradingScriptValidation = async (req: Request, res: Response) => {
if (!req.body || req.body.length === 0) {
return errorResponse(res, 400, ['No request body provided for grading script schema validation.']);
}
const validator = validations.gradingScript;
return validator(req.body) ?
res.json({ "message": "Provided request body is a valid grading script configuration." }) :
res.json({ "errors": formatValidationErrors(validator.errors) });
}
3 changes: 2 additions & 1 deletion orchestrator/packages/api/src/routes/validations.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Router } from "express";
import verifyAPIKey from "../middleware/verify-api-key";
import { gradingJobValidation } from "../controllers/validations-controller";
import { gradingJobValidation, gradingScriptValidation } from "../controllers/validations-controller";

const validationsRouter = Router();

validationsRouter.post('/validate_grading_job', verifyAPIKey, gradingJobValidation);
validationsRouter.post('/validate_grading_script', verifyAPIKey, gradingScriptValidation);

export default validationsRouter;
4 changes: 3 additions & 1 deletion orchestrator/packages/common/src/types/grading-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface FileInfo {

export type CollationType = "team" | "user";

export type GradingScript = GradingScriptCommand[];

export interface Collation {
type: CollationType;
id: string;
Expand All @@ -47,7 +49,7 @@ export interface GradingJobConfig {
metadata_table: Record<string, string | string[]>;
files: Record<string, FileInfo>;
priority: number;
script: GradingScriptCommand[];
script: GradingScript;
response_url: string;
container_response_url?: string;
grader_image_sha: string;
Expand Down
7 changes: 5 additions & 2 deletions orchestrator/packages/common/src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { collationSchema } from "./schemas/shared";
import {
gradingJobConfigSchema,
gradingJobConfigSubSchemas,
gradingScriptSchema
} from "./schemas/grading-job-config";
import { moveJobRequestSchema } from "./schemas/move-job-request";
import {
GradingJobConfig,
GradingScript,
MoveJobRequest,
} from "../types/grading-queue";
import { GraderImageBuildRequest } from "../types/image-build-service";
import { graderImageBuildRequestSchema } from "./schemas/grader-image-build-request";

let ajv = new Ajv().addSchema(collationSchema);
ajv = gradingJobConfigSubSchemas.reduce(
ajv = [gradingJobConfigSubSchemas].reduce(
(prev, curr) => prev.addSchema(curr),
ajv,
);
Expand All @@ -22,8 +24,9 @@ export type ValidationErrors = typeof ajv.errors;

export default {
gradingJobConfig: ajv.compile<GradingJobConfig>(gradingJobConfigSchema),
gradingScript: ajv.compile<GradingScript>(gradingScriptSchema),
moveJobRequest: ajv.compile<MoveJobRequest>(moveJobRequestSchema),
graderImageBuildRequest: ajv.compile<GraderImageBuildRequest>(
graderImageBuildRequestSchema,
),
)
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const fileInfo = {
additionalProperties: false,
} as const;

const bashGradingScriptCommand = {
const bashGradingScriptCommandSchema = {
$id: "https://orca-schemas.com/grading-job-config/bash-grading-script-command",
type: "object",
properties: {
Expand Down Expand Up @@ -53,7 +53,7 @@ const bashGradingScriptCommand = {
additionalProperties: false,
} as const;

const gradingScriptCondition = {
const gradingScriptConditionSchema = {
$id: "https://orca-schemas.com/grading-job-config/grading-script-condition",
type: "object",
properties: {
Expand All @@ -67,7 +67,7 @@ const gradingScriptCondition = {
additionalProperties: false,
} as const;

const conditionalGradingScriptCommand = {
const conditionalGradingScriptCommandSchema = {
$id: "https://orca-schemas.com/grading-job-config/conditional-grading-script-command",
type: "object",
properties: {
Expand Down Expand Up @@ -102,7 +102,7 @@ const conditionalGradingScriptCommand = {
additionalProperties: false,
} as const;

const gradingScriptCommand = {
export const gradingScriptCommandSchema = {
$id: "https://orca-schemas.com/grading-job-config/grading-script-command",
oneOf: [
{
Expand All @@ -114,12 +114,25 @@ const gradingScriptCommand = {
],
} as const;

export const gradingScriptSubSchemas = [
bashGradingScriptCommandSchema,
gradingScriptConditionSchema,
conditionalGradingScriptCommandSchema,
gradingScriptCommandSchema
];

export const gradingScriptSchema = {
$id: "https://orca-schemas.com/grading-job-config/grading-script",
type: "array",
items: {
$ref: "https://orca-schemas.com/grading-job-config/grading-script-command",
},
} as const;

export const gradingJobConfigSubSchemas = [
fileInfo,
gradingScriptCommand,
bashGradingScriptCommand,
gradingScriptCondition,
conditionalGradingScriptCommand,
...gradingScriptSubSchemas,
gradingScriptSchema
];

export const gradingJobConfigSchema = {
Expand All @@ -140,12 +153,7 @@ export const gradingJobConfigSchema = {
},
},
priority: { type: "number" },
script: {
type: "array",
items: {
$ref: "https://orca-schemas.com/grading-job-config/grading-script-command",
},
},
script: { $ref: "https://orca-schemas.com/grading-job-config/grading-script" },
response_url: { type: "string" },
container_response_url: { type: "string" },
grader_image_sha: { type: "string" },
Expand Down

0 comments on commit 3a4757d

Please sign in to comment.