-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzod.js
More file actions
67 lines (57 loc) · 1.64 KB
/
Copy pathzod.js
File metadata and controls
67 lines (57 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// const { z } = require("zod");
// exports.signupSchema = z.object({
// name: z.string(),
// email: z.string().email(),
// role: z.enum(["user", "admin"]),
// password: z.string().min(6)
// })
// exports.loginSchema = z.object({
// name: z.string(),
// password: z.string()
// })
// exports.questionSchema = z.object({
// text: z.string(),
// options: z.array(z.string()).min(1),
// correctOptionIndex: z.number()
// })
// exports.quizSchema = z.object({
// title: z.string(),
// questions: z.array(exports.questionSchema).min(1)
// })
// exports.validate =
// (schema) =>
// (req, res, next) => {
// const result = schema.safeParse(req.body);
// if (!result.success) {
// return res.status(400).json({
// success: false,
// error: "Invalid request schema",
// details: result.error.errors
// });
// }
// req.body = result.data
// next()
// }
const {z} = require('zod')
exports.signupSchema = z.object({
name: z.string(),
email: z.string().email(),
password: z.string().min(6),
role: z.enum(["user", "admin"])
})
exports.loginSchema = z.object({
name: z.string(),
password: z.string().min(6)
})
exports.validate = (schema) => (req, res, next) => { // this is a middleware for schema check
const result = schema.safeParse(req.body)
if(!result.success){
return res.status(400).json({
success: false,
error: "Invalid request schema",
details: result.error.errors
})
}
req.body = result.data
next()
}