Skip to content

Commit

Permalink
check all params needed to create the new list exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ebisbe committed Jul 7, 2024
1 parent 67ba4fe commit 54a3308
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@aws-sdk/client-dynamodb": "^3.609.0",
"@middy/core": "^5.4.3",
"@middy/http-error-handler": "^5.4.3",
"@middy/http-json-body-parser": "^5.4.3",
"dynamodb-onetable": "^2.7.4",
"zod": "^3.23.8"
},
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 28 additions & 10 deletions src/functions/list/__tests__/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@ import { describe, it, expect } from "@jest/globals";
import { handler } from "../create";

describe("CREATE List", () => {

it('receives an empty body', async () => {
const response = await handler({})
it("receives an empty body", async () => {
const response = await handler({
headers: {
"Content-Type": "application/json",
},
body: null,
});

expect(response).toStrictEqual({
statusCode: 406,
headers: {'Content-Type': 'text/plain'},
body: 'Invalid request. Missing body params.'
})
})
statusCode: 400,
headers: { "Content-Type": "text/plain" },
body: "Invalid request. Missing body params.",
});
});

it("creates a new list", () => {});

it('creates a new list', () => {})
it("does not received all required properties", async () => {
const event = {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ param: "1" }),
};
const response = await handler(event);

it('does not received all required properties', () => {})
expect(response).toStrictEqual({
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: '{"issues":[{"code":"invalid_type","expected":"string","received":"undefined","path":["userId"],"message":"Required"},{"code":"invalid_type","expected":"string","received":"undefined","path":["name"],"message":"Required"},{"code":"invalid_type","expected":"string","received":"undefined","path":["description"],"message":"Required"}],"name":"ZodError"}',
});
});
});
13 changes: 9 additions & 4 deletions src/functions/list/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { List } from "../../models/table";
import { z } from "zod";
import middy from "@middy/core";
import httpErrorHandler from "@middy/http-error-handler";
import httpJsonBodyParser from "@middy/http-json-body-parser";
import { HttpError } from "../../utils/httpError";
import { ValidationError } from "../../utils/validationError";

const createListHandler: Handler<APIGatewayProxyEventV2> = async (event) => {
if (!event.body) {
throw new HttpError(406, "Invalid request. Missing body params.");
throw new HttpError(400, "Invalid request. Missing body params.");
}

const listSchema = z.object({
Expand All @@ -16,12 +18,14 @@ const createListHandler: Handler<APIGatewayProxyEventV2> = async (event) => {
description: z.string(),
}) satisfies z.ZodType<List>;

const list = listSchema.parse(JSON.parse(event?.body));
const list = listSchema.safeParse(event.body);
if (!list.success) {
throw new ValidationError(400, list.error);
}

const createdList = await List.create(list);
const createdList = await List.create(list.data);
delete createdList.pk;
delete createdList.sk;
createdList.createdAt;

return {
statusCode: 200,
Expand All @@ -31,4 +35,5 @@ const createListHandler: Handler<APIGatewayProxyEventV2> = async (event) => {

export const handler = middy()
.use(httpErrorHandler({ logger: false }))
.use(httpJsonBodyParser())
.handler(createListHandler);
12 changes: 12 additions & 0 deletions src/utils/validationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ZodError } from "zod";

export class ValidationError extends Error {
message: string;
statusCode: number;

constructor(statusCode = 500, message: ZodError) {
super();
this.message = JSON.stringify(message);
this.statusCode = statusCode;
}
}

0 comments on commit 54a3308

Please sign in to comment.