Skip to content

Commit

Permalink
added zod validation-backend complete
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkar1713 committed Sep 6, 2024
1 parent 7f83452 commit 2659285
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
10 changes: 9 additions & 1 deletion backend/package-lock.json

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

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"@prisma/client": "^5.19.1",
"@prisma/extension-accelerate": "^1.1.0",
"@pushkar1713/week13-common": "^1.0.1",
"hono": "^4.5.11",
"prisma": "^5.19.1"
},
Expand Down
16 changes: 15 additions & 1 deletion backend/src/routes/posts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Hono } from "hono";
import { getPrisma } from "../prismaFunction";
import { decode, sign, verify } from "hono/jwt";

import { createPost, updatePost } from "@pushkar1713/week13-common";}
export const postRouter = new Hono<{
Bindings: {
DATABASE_URL: string;
Expand Down Expand Up @@ -40,6 +40,13 @@ postRouter.use("/*", async (c, next) => {
postRouter.post("/", async (c) => {
const prisma = getPrisma(c.env.DATABASE_URL);
const body = await c.req.json();
const { success } = createPost.safeParse(body);
if (!success) {
c.status(403);
return c.json({
msg: "invalid types",
});
}
const userId = c.get("userId");

const blog = await prisma.post.create({
Expand Down Expand Up @@ -84,6 +91,13 @@ postRouter.get("/:id", async (c) => {
postRouter.put("/", async (c) => {
const prisma = getPrisma(c.env.DATABASE_URL);
const body = await c.req.json();
const { success } = updatePost.safeParse(body);
if (!success) {
c.status(403);
return c.json({
msg: "invalid types",
});
}

const updatedBlog = await prisma.post.update({
where: {
Expand Down
15 changes: 15 additions & 0 deletions backend/src/routes/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from "hono";
import { getPrisma } from "../prismaFunction";
import { decode, sign, verify } from "hono/jwt";
import { signinInput, signupInput } from "@pushkar1713/week13-common";

export const userRouter = new Hono<{
Bindings: {
Expand All @@ -12,6 +13,13 @@ export const userRouter = new Hono<{
userRouter.post("/signup", async (c) => {
const prisma = getPrisma(c.env.DATABASE_URL);
const body = await c.req.json();
const { success } = signupInput.safeParse(body);
if (!success) {
c.status(403);
return c.json({
msg: "invalid types",
});
}

try {
const user = await prisma.user.create({
Expand All @@ -38,6 +46,13 @@ userRouter.post("/signup", async (c) => {
userRouter.post("/signin", async (c) => {
const prisma = getPrisma(c.env.DATABASE_URL);
const body = await c.req.json();
const { success } = signinInput.safeParse(body);
if (!success) {
c.status(403);
return c.json({
msg: "invalid types",
});
}

try {
const user = prisma.user.findUnique({
Expand Down

0 comments on commit 2659285

Please sign in to comment.