-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use validation with transformer for mooclet policy params
- Loading branch information
1 parent
caf1c0a
commit be1c720
Showing
13 changed files
with
2,798 additions
and
4,767 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/packages/Upgrade/src/api/middlewares/ValidateMoocletPolicyParameters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { BadRequestError, ExpressMiddlewareInterface, Middleware } from 'routing-controllers'; | ||
import { env } from '../../env'; | ||
import { UnprocessableEntityException } from '@nestjs/common'; | ||
import { validateMoocletPolicyParameters } from '../validators/MoocletPolicyParametersFactory'; | ||
|
||
@Middleware({ type: 'before' }) | ||
export class ValidateMoocletPolicyParametersMiddleware implements ExpressMiddlewareInterface { | ||
public async use(req: Request, res: Response, next: NextFunction): Promise<void> { | ||
const experiment = req.body; | ||
|
||
if (!env.mooclets.enabled && 'moocletPolicyParameters' in experiment) { | ||
throw new UnprocessableEntityException( | ||
'Failed to create Experiment: moocletPolicyParameters was provided but mooclets are not enabled on backend.' | ||
); | ||
} | ||
|
||
if (env.mooclets.enabled) { | ||
try { | ||
const policyParameters = await validateMoocletPolicyParameters( | ||
experiment.assignmentAlgorithm, | ||
experiment.moocletPolicyParameters | ||
); | ||
req.body.moocletPolicyParameters = policyParameters; | ||
} catch (error) { | ||
throw new BadRequestError(`Validation failed: ${error}`); | ||
} | ||
} | ||
|
||
next(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/packages/Upgrade/src/api/validators/MoocletPolicyParametersFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { plainToInstance, Type, Transform } from 'class-transformer'; | ||
import { validate } from 'class-validator'; | ||
import { ASSIGNMENT_ALGORITHM, MOOCLET_POLICY_SCHEMA_MAP } from 'upgrade_types'; | ||
export class MoocletPolicyParametersDTO { | ||
@Transform(({ value: policyParameters, obj: experiment }) => { | ||
const policyParameterSchema = MOOCLET_POLICY_SCHEMA_MAP[experiment.assignmentAlgorithm]; | ||
if (!policyParameterSchema) { | ||
throw new Error('Invalid assignment algorithm: ' + experiment.assignmentAlgorithm); | ||
} | ||
return plainToInstance(policyParameterSchema, policyParameters); | ||
}) | ||
@Type(() => Object) | ||
moocletPolicyParameters: any; | ||
} | ||
|
||
export async function validateMoocletPolicyParameters(assignmentAlgorithm: ASSIGNMENT_ALGORITHM, moocletPolicyParameters: any) { | ||
const moocletPolicyParametersDTO = plainToInstance(MoocletPolicyParametersDTO, { assignmentAlgorithm, moocletPolicyParameters }); | ||
const errors = await validate(moocletPolicyParametersDTO.moocletPolicyParameters); | ||
|
||
if (errors.length > 0) { | ||
throw new Error(`Policy Parameter validation failed: ${errors}`); | ||
} | ||
|
||
return moocletPolicyParametersDTO.moocletPolicyParameters; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.