Spaces:
Sleeping

File size: 4,795 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a1ffec
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
import z from "zod";

import { mongoIdSchema } from "@/dtos/zodHelpers.js";
import { ProjectProvider, ProjectStatus } from "@/models/Projects.js";
import { DEPLOYMENT_ID_LENGTH, DEPLOYMENT_SEPARATOR_LENGTH, MAX_SUBDOMAIN_LENGTH } from "@/constants/subdomain.js";

export const envSchema = z
	.object({
		name: z
			.string()
			.min(1, "Name is required")
			.max(100, "Name too long")
			.regex(/^[A-Z_][A-Z0-9_]*$/, "Name must be uppercase with underscores"),
		value: z.string().max(5000, "Value too long"),
	})
	.strict();

export const CreateProjectSchema = z.object({
	ghRepoId: z.number().optional(),
	provider: z.enum(ProjectProvider),
	isPrivate: z.boolean(),
	name: z.string().min(3, "Name should not exceed 3 charecters").max(50, "Name max 50 characters").regex(/^[^\p{Emoji_Presentation}\p{Extended_Pictographic}]*$/u, "Name cannot contain emojis"),
	repoURL: z
		.string()
		.regex(
			/^(?:https?:\/\/)?(?:www\.)?(github\.com|gitlab\.com|bitbucket\.org)\/[\w.-]+\/[\w.-]+\/?$/,
			"Invalid repository URL (GitHub, GitLab, Bitbucket supported)",
		),
	branch: z.string().min(1, "Branch cannot be empty").default("main").optional(),
	buildCommand: z
		.string()
		.regex(/^[a-zA-Z0-9_\-./ ]+$/, "Build command contains invalid characters")
		.optional()
		.default("build"),
	// installCommand: z
	// 	.string()
	// 	.regex(/^[a-zA-Z0-9_\-./ ]+$/, "Install command contains invalid characters")
	// 	.optional()
	// 	.default("install"),
	rootDir: z
		.string()
		.trim()
		.min(1, "Root directory cannot be empty")
		.regex(
			/^\/(?!\.\.)[a-zA-Z0-9\/_-]*$/,
			"Path must start with / and contain only alphanumeric characters, hyphens, underscores, and forward slashes",
		)
		.refine(
			(path) => !path.includes("..") && !["/etc", "/root", "/sys", "/proc", "/dev"].some((dir) => path.startsWith(dir)),
			"Invalid or restricted path",
		)
		.default("/")
		.catch("/")
		.optional(),
	outputDirectory: z.string().optional().default("dist"),
	env: z.array(envSchema).max(100).default([]).optional(),
});

export const UpdateProjectSchema = z.object({
	name: z.string().min(3, "Name should be at least 3 charecters").max(50, "Name max 50 characters").optional(),
	branch: z.string().optional(),
	buildCommand: z
		.string()
		.regex(/^[a-zA-Z0-9_\-./ ]+$/, "Build command contains invalid characters")
		.optional(),
	installCommand: z
		.string()
		.regex(/^[a-zA-Z0-9_\-./ ]+$/, "Install command contains invalid characters")
		.optional(),
	rootDir: z
		.string()
		.trim()
		.min(1, "Root directory cannot be empty")
		.regex(
			/^\/(?!\.\.)[a-zA-Z0-9\/_-]*$/,
			"Path must start with / and contain only alphanumeric characters, hyphens, underscores, and forward slashes",
		)
		.refine(
			(path) => !path.includes("..") && !["/etc", "/root", "/sys", "/proc", "/dev"].some((dir) => path.startsWith(dir)),
			"Invalid or restricted path",
		)
		.catch("/")
		.optional(),
	rewriteNonFilePaths: z.boolean().optional(),
	autoDeployEnabled: z.boolean().optional(),
	outputDirectory: z.string().optional(),
	env: z.array(envSchema).max(100).optional(),
	isDisabled: z.boolean().optional(),
	projectId: z.string(),
});

export const ProjectQuerySchema = z
	.object({
		status: z.enum(Object.values(ProjectStatus)).optional(),
		page: z.coerce.number().int().positive().default(1),
		limit: z.coerce.number().int().min(1).max(100).default(10),
		search: z.string().max(100).optional().default(""),
		include: z.string().optional(),
		full: z.string().optional(),
	})
	.strict();

export const ProjectSubdomainSchema = z.object({
	projectId: mongoIdSchema,
	newSubdomain: z
		.string()
		.min(3)
		.max(MAX_SUBDOMAIN_LENGTH - (DEPLOYMENT_ID_LENGTH + DEPLOYMENT_SEPARATOR_LENGTH))
		.regex(/^[a-z0-9-]+$/, "Only lowercase letters, numbers and hyphens allowed")
		.regex(/^[a-z0-9]/, "Must start with a letter or number")
		.regex(/[a-z0-9]$/, "Must end with a letter or number"),
});
export const SubdomainQuerySchema = z.object({
	value: z
		.string()
		.min(3)
		.max(MAX_SUBDOMAIN_LENGTH - (DEPLOYMENT_ID_LENGTH + DEPLOYMENT_SEPARATOR_LENGTH))
		.regex(/^[a-z0-9-]+$/, "Only lowercase letters, numbers and hyphens allowed")
		.regex(/^[a-z0-9]/, "Must start with a letter or number")
		.regex(/[a-z0-9]$/, "Must end with a letter or number"),
});
export const ProjectDeploymentUpdateSchema = z.object({
	newCurrentDeployment: mongoIdSchema,
});

export type CreateProjectDTO = z.infer<typeof CreateProjectSchema>;
export type UpdateProjectDTO = z.infer<typeof UpdateProjectSchema>;
export type QueryProjectDTO = z.infer<typeof ProjectQuerySchema>;
export type UpdateSubdomainDTO = z.infer<typeof ProjectSubdomainSchema>;
export type checkSubdomainDTO = z.infer<typeof SubdomainQuerySchema>;
export type ProjectDeploymentUpdateDTO = z.infer<typeof ProjectDeploymentUpdateSchema>;