Merge pull request #388 from bluewave-labs/feat/speedcheck-body

Updated validation schemas in the joi and changed validation schema in the controller for the page speed check.
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-07-19 13:04:28 -07:00
commit cb2961b9d6
3 files changed
+37 -6

No files matched your search

+21 -1
View File
@@ -4,6 +4,7 @@ const SERVICE_NAME = "pagespeed";
const {
getPageSpeedCheckParamValidation,
createPageSpeedCheckParamValidation,
createPageSpeedCheckBodyValidation,
deletePageSpeedCheckParamValidation,
} = require("../validation/joi");
@@ -42,7 +43,26 @@ const createPageSpeedCheck = async (req, res, next) => {
// Validate monitorId parameter
await createPageSpeedCheckParamValidation.validateAsync(req.params);
return res.status(200).json({ msg: "Hit createPageSpeedCheck" });
// Validate request body
await createPageSpeedCheckBodyValidation.validateAsync(req.body);
const { monitorId } = req.params;
const { accessibility, bestPractices, seo, performance } = req.body;
const newPageSpeedCheck = new PageSpeedCheck({
monitorId,
accessibility,
bestPractices,
seo,
performance,
});
await newPageSpeedCheck.save();
return res.status(201).json({
msg: successMessages.CREATED,
data: newPageSpeedCheck,
});
} catch (error) {
if (error.isJoi) {
return res.status(400).json({ error: error.details[0].message });
+4 -5
View File
@@ -1,8 +1,7 @@
const router = require("express").Router();
const { verifyOwnership } = require("../middleware/verifyOwnership");
const Monitor = require("../models/Monitor");
const { createPageSpeedCheck, getPageSpeedChecks, deletePageSpeedCheck } = require("../controllers/pageSpeedCheckController");
const checkController = require("../controllers/pageSpeedCheckController");
/**
* @route POST /pagespeed/:monitorId
@@ -14,7 +13,7 @@ const { createPageSpeedCheck, getPageSpeedChecks, deletePageSpeedCheck } = requi
router.post(
"/pagespeed/:monitorId",
verifyOwnership(Monitor, "monitorId"),
createPageSpeedCheck
checkController.createPageSpeedCheck
);
/**
@@ -27,7 +26,7 @@ router.post(
router.get(
"/pagespeed/:monitorId",
verifyOwnership(Monitor, "monitorId"),
getPageSpeedChecks
checkController.getPageSpeedChecks
);
/**
@@ -40,7 +39,7 @@ router.get(
router.delete(
"/pagespeed/:monitorId",
verifyOwnership(Monitor, "monitorId"),
deletePageSpeedCheck
checkController.deletePageSpeedCheck
);
module.exports = router;
+12
View File
@@ -182,14 +182,25 @@ const deleteChecksParamValidation = joi.object({
//****************************************
// PageSpeedCheckValidation
//****************************************
const getPageSpeedCheckParamValidation = joi.object({
monitorId: joi.string().required(),
});
//Validation schema for the monitorId parameter
const createPageSpeedCheckParamValidation = joi.object({
monitorId: joi.string().required(),
});
//Validation schema for the monitorId body
const createPageSpeedCheckBodyValidation = joi.object({
monitorId: joi.string().required(),
accessibility: joi.number().required().min(0).max(100),
bestPractices: joi.number().required().min(0).max(100),
seo: joi.number().required().min(0).max(100),
performance: joi.number().required().min(0).max(100),
});
const deletePageSpeedCheckParamValidation = joi.object({
monitorId: joi.string().required(),
});
@@ -221,4 +232,5 @@ module.exports = {
getPageSpeedCheckParamValidation,
createPageSpeedCheckParamValidation,
deletePageSpeedCheckParamValidation,
createPageSpeedCheckBodyValidation,
};