Skip to content

Commit

Permalink
Merge pull request #71 from AbdulrhmanGoni/feature/not-allowed-empty-…
Browse files Browse the repository at this point in the history
…update-payloads

Make updates middlewares reject the empty update payloads
  • Loading branch information
AbdulrhmanGoni authored Oct 28, 2024
2 parents cb844b0 + 4aedd14 commit c88297b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/middlewares/updateDatasetInputValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ import type { ValidationError } from "../types/validation";
import validationSchema from "../validation/validationSchema";
import DatasetSchemaRule from "../validation/DatasetSchemaRule";
import RequiredObjectIdRule from "../validation/RequiredObjectIdRule";
import haveSharedKey from "../utilities/haveSharedKey";

const DatasetBaseRules = DatasetSchemaRule();

const updateDatasetInputSchema = validationSchema<
UpdateDatasetInput & { datasetId: Dataset["id"] }
>({
datasetId: RequiredObjectIdRule(),
...DatasetSchemaRule(),
...DatasetBaseRules,
});

export const emptyUpdateDatasetBodyMessage =
"You have to choose at least one field of the dataset to update it";

export default function updateDatasetInputValidator(request: Req) {
try {
if (haveSharedKey(DatasetBaseRules, request.json)) {
return ErrorResponse(emptyUpdateDatasetBodyMessage, 400);
}

const { datasetId, ...updateData } = updateDatasetInputSchema.validate({
...request.json,
datasetId: request.params.datasetId,
Expand Down
12 changes: 11 additions & 1 deletion src/middlewares/updateInstructionInputValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import type {
UpdateInstructionInput,
} from "../types/instructions";
import InstructionSchemaRules from "../validation/InstructionSchemaRules";
import haveSharedKey from "../utilities/haveSharedKey";

const InstructionBaseRules = InstructionSchemaRules();

const updateInstructionInputSchema = validationSchema<
{
Expand All @@ -18,11 +21,18 @@ const updateInstructionInputSchema = validationSchema<
>({
datasetId: RequiredObjectIdRule(),
instructionId: RequiredObjectIdRule(),
...InstructionSchemaRules(),
...InstructionBaseRules,
});

export const emptyUpdateInstructionBodyMessage =
"You have to choose at least one field of the instruction to update it";

export default function updateInstructionInputValidator(request: Req) {
try {
if (haveSharedKey(InstructionBaseRules, request.json)) {
return ErrorResponse(emptyUpdateInstructionBodyMessage, 400);
}

const { datasetId, instructionId, ...updateData } =
updateInstructionInputSchema.validate({
datasetId: request.search.datasetId,
Expand Down
3 changes: 3 additions & 0 deletions src/utilities/haveSharedKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function haveSharedKey(object: object, target: object): boolean {
return !Object.keys(object).some((key) => Object.hasOwn(target, key));
}

0 comments on commit c88297b

Please sign in to comment.