-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathinput.validation.js
More file actions
130 lines (109 loc) · 4.71 KB
/
Copy pathinput.validation.js
File metadata and controls
130 lines (109 loc) · 4.71 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const z = require("zod");
module.exports.loginSchema = z.object({
email: z.string()
.min(1, { message: "Email is required." }) // Ensure the input is not empty
.email({ message: "Invalid email format." })
.max(100, { message: "Email is too long." }),
password: z.string()
.min(6, { message: "Password must be at least 6 characters" })
.max(100, { message: "Password is too long." })
});
module.exports.signupSchema = z.object({
username: z.string()
.min(3, { message: "Username must be at least 3 characters." })
.max(30, { message: "Username is too long." }),
email: z.string()
.min(1, { message: "Email is required." })
.email({ message: "Invalid email format." })
.max(100, { message: "Email is too long." }),
password: z.string()
.min(6, { message: "Password must be at least 6 characters." })
.max(100, { message: "Password is too long." })
});
module.exports.changePasswordSchema = z.object({
currentPassword: z.string().min(1, "Current password is required"),
newPassword: z.string().min(6, "New password must be at least 6 characters")
});
module.exports.deleteAccountSchema = z.object({
password: z.string().min(1, "Password is required")
});
module.exports.onlyEmailSchema = z.object({
email: z.string().email("Invalid email format")
});
module.exports.verifyOtpSchema = z.object({
email: z.string().email("Invalid email format"),
otp: z.string().length(6, "OTP must be 6 digits")
});
module.exports.resetPasswordSchema = z.object({
email: z.string().email("Invalid email format"),
otp: z.string().length(6, "OTP must be 6 digits"),
newPassword: z.string().min(6, "Password must be at least 6 characters").max(100, "Password is too long.")
});
module.exports.createProjectSchema = z.object({
name: z.string().min(1, "Project name is required"),
description: z.string().optional()
});
// Create Collection Schema
module.exports.createCollectionSchema = z.object({
projectId: z.string().min(1, "Project ID is required"),
collectionName: z.string().min(1, "Collection Name is required"),
schema: z.array(z.object({
key: z.string(),
type: z.enum(['String', 'Number', 'Boolean', 'Date']),
required: z.boolean().optional()
})).optional()
});
// Create Collection Schema (API Key based)
module.exports.createSchemaApiKeySchema = z.object({
name: z.string().min(1, "Collection Name is required"),
fields: z.array(z.object({
name: z.string(),
type: z.enum(['string', 'number', 'boolean', 'date', 'String', 'Number', 'Boolean', 'Date']),
required: z.boolean().optional()
})).optional()
});
module.exports.sanitize = (obj) => {
const clean = {};
for (const key in obj) {
if (!key.startsWith('$')) {
clean[key] = obj[key];
}
}
return clean;
};
const emptyToUndefined = z.preprocess((val) => (val === "" || val === null ? undefined : val), z.string().optional());
module.exports.updateExternalConfigSchema = z.object({
// DB URI: No .url() check because of mongodb+srv
dbUri: z.preprocess((val) => (val === "" || val === null ? undefined : val),
z.string().optional().refine(val => !val || val.startsWith('mongodb'), {
message: "Invalid Database URI format."
})
),
// Storage URL: Sirf tab check karega jab value empty na ho
storageUrl: z.preprocess((val) => (val === "" || val === null ? undefined : val),
z.string().url("Invalid Storage URL format").optional()
),
storageKey: emptyToUndefined,
storageProvider: z.enum(['supabase', 'aws', 'cloudinary']).optional()
}).refine(data => {
// Condition 1: Agar Storage URL diya hai, toh Storage Key bhi honi chahiye
if (data.storageUrl && !data.storageKey) return false;
// Condition 2: Agar Storage Key di hai, toh Storage URL bhi hona chahiye
if (data.storageKey && !data.storageUrl) return false;
// Condition 3: Kam se kam ek cheez (DB ya Storage) poori honi chahiye
return !!(data.dbUri || (data.storageUrl && data.storageKey));
}, {
message: "Provide either a DB URI or a complete Storage config (URL + Key)."
});
module.exports.userSignupSchema = z.object({
username: z.string()
.min(3, { message: "Username must be at least 3 characters." })
.max(30, { message: "Username is too long." }).optional(),
email: z.string()
.min(1, { message: "Email is required." })
.email({ message: "Invalid email format." })
.max(100, { message: "Email is too long." }),
password: z.string()
.min(6, { message: "Password must be at least 6 characters." })
.max(100, { message: "Password is too long." })
}).passthrough();