forked from tdjsnelling/sqtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateConfig.js
More file actions
95 lines (91 loc) · 3.27 KB
/
Copy pathvalidateConfig.js
File metadata and controls
95 lines (91 loc) · 3.27 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
import * as yup from "yup";
const httpRegex = /http(s)?:\/\/.*/;
const mongoRegex = /mongodb:\/\/.*/;
const hexRegex = /#([a-f0-9]){6}/i;
const configSchema = yup
.object({
envs: yup
.object({
SQ_SITE_NAME: yup.string().min(1).max(20).required(),
SQ_SITE_DESCRIPTION: yup.string().min(1).max(80).required(),
SQ_ALLOW_REGISTER: yup
.string()
.oneOf(["open", "invite", "closed"])
.required(),
SQ_ALLOW_ANONYMOUS_UPLOADS: yup.boolean().required(),
SQ_MINIMUM_RATIO: yup.number().min(-1).required(),
SQ_MAXIMUM_HIT_N_RUNS: yup.number().integer().min(-1).required(),
SQ_BP_EARNED_PER_GB: yup.number().min(0).required(),
SQ_BP_EARNED_PER_FILLED_REQUEST: yup.number().min(0).required(),
SQ_BP_COST_PER_INVITE: yup.number().min(0).required(),
SQ_BP_COST_PER_GB: yup.number().min(0).required(),
SQ_SITE_WIDE_FREELEECH: yup.boolean().required(),
SQ_TORRENT_CATEGORIES: yup.lazy((value) => {
const entries = Object.keys(value).reduce((obj, key) => {
obj[key] = yup
.array()
.of(yup.string())
.min(0)
.test(
`${key}-items-unique`,
`Sources in category "${key}" must be unique`,
(value) =>
value.every(
(source) => value.filter((c) => c === source).length === 1
)
);
return obj;
}, {});
return yup.object(entries).required();
}),
SQ_ALLOW_UNREGISTERED_VIEW: yup.boolean().required(),
SQ_CUSTOM_THEME: yup.object({
primary: yup.string().matches(hexRegex),
background: yup.string().matches(hexRegex),
sidebar: yup.string().matches(hexRegex),
border: yup.string().matches(hexRegex),
text: yup.string().matches(hexRegex),
grey: yup.string().matches(hexRegex),
}),
SQ_EXTENSION_BLACKLIST: yup.array().of(yup.string()).min(0),
SQ_BASE_URL: yup.string().matches(httpRegex).required(),
SQ_API_URL: yup.string().matches(httpRegex).required(),
SQ_MONGO_URL: yup.string().matches(mongoRegex).required(),
SQ_MAIL_FROM_ADDRESS: yup.string().email().required(),
SQ_SMTP_HOST: yup.string().required(),
SQ_SMTP_PORT: yup.number().integer().min(1).max(65535).required(),
SQ_SMTP_SECURE: yup.boolean().required(),
})
.strict()
.noUnknown()
.required(),
secrets: yup
.object({
SQ_JWT_SECRET: yup.string().required(),
SQ_SERVER_SECRET: yup.string().required(),
SQ_ADMIN_EMAIL: yup.string().email().required(),
SQ_SMTP_USER: yup.string().required(),
SQ_SMTP_PASS: yup.string().required(),
})
.strict()
.noUnknown()
.required(),
})
.strict()
.noUnknown()
.required();
const validateConfig = async (config) => {
try {
process.env = {
...process.env,
...config.envs,
...config.secrets,
};
await configSchema.validate(config);
console.log("[sq] configuration is valid");
} catch (e) {
console.error("[sq] ERROR: invalid configuration:", e.message);
process.exit(1);
}
};
export default validateConfig;