Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added and Corrected the Initial Files of the Zod #185

Merged
merged 6 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion services/Auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"express": "^4.19.2",
"firebase-admin": "^12.1.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.3.3"
"mongoose": "^8.3.3",
"zod": "^3.23.8"
},
"scripts": {
"dev": "nodemon index.js"
Expand Down
2 changes: 1 addition & 1 deletion services/Auth/src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function signup(req, res) {
const { name, email, password } = req.body;

//Check if input is as expected or not
if (!email || !password || !email) {
if (!name || !password || !email) {
res.json (errorHadnler(401,'All fields must be filled'))
return
}
Expand Down
15 changes: 15 additions & 0 deletions services/Auth/src/middleware/validate.schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const validate = (schema) => async (req, res, next) => {
try {
const parsedBody = await schema.parseAsync(req.body);
req.body = parsedBody;
next();
} catch (err) {
console.log(err);
const yourerror = err.errors[0].message;
res.status(400).json({
msg: yourerror,
});
}
}

module.exports = validate;
7 changes: 4 additions & 3 deletions services/Auth/src/routes/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const express = require("express");

const router = express.Router();
const { signup, signin } = require("../controllers/auth.controller");

const validate = require("../middleware/validate.schema");
const RegisterSchema = require("../validation/zodschema");
// auth routes...;

router.post("/sign-up", signup);
router.post("/sign-up",validate(RegisterSchema), signup);

router.post("/sign-in", signin);
router.post("/sign-in",validate(RegisterSchema), signin);

module.exports = router;
24 changes: 24 additions & 0 deletions services/Auth/src/validation/zodschema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const zod = require("zod");


const RegisterSchema = zod.object({
name: zod
.string({ required_error: "username Is Required" })
.trim()
.min(3, { message: "Username must be at least 3 characters" })
.max(255, { message: "Username can be at most 255 characters" }),
email: zod
.string({ required_error: "Email is required" })
.trim()
.email({ message: "Invalid Email Address" })
.min(3, { message: "Email should be at least 3 characters" })
.max(255, { message: "Email can be at most 255 characters" }),
password: zod
.string({ required_error: "Password is required" })
.trim()
.min(7, { message: "Password must be at least 7 characters" })
.max(1024, { message: "Password must be at most 1024 characters" })
});


module.exports = RegisterSchema ;