-
Notifications
You must be signed in to change notification settings - Fork 1
/
validateConfig.js
215 lines (195 loc) · 5.61 KB
/
validateConfig.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import chalk from "chalk";
// Validate config schema
const validateObject = (obj, schema, path = []) => {
for (const key in schema) {
if (schema.hasOwnProperty(key)) {
const fieldType = schema[key];
const fieldValue = obj[key];
const fieldPath = [...path, key];
if (typeof fieldType === 'string') {
if (typeof fieldValue !== fieldType) {
return `Field ${chalk.italic(fieldPath.join(' > '))} should be of type ${fieldType}.`;
}
} else if (typeof fieldType === 'object' && fieldType !== null) {
if (typeof fieldValue !== 'object') {
return `Field ${chalk.italic(fieldPath.join(' > '))} should be an object.`;
}
const nestedError = validateObject(fieldValue, fieldType, fieldPath);
if (nestedError) {
return nestedError;
}
}
}
}
return null;
}
const validateConfigSchema = (config) => {
const schema = {
clientId: 'string',
rich_presence: {
details: {
text: 'string',
show: 'boolean'
},
state: {
text: 'string',
show: 'boolean'
},
assets: {
largeImage: {
text: 'string',
key: 'string',
show: 'boolean'
},
smallImage: {
text: 'string',
key: 'string',
show: 'boolean'
}
},
buttons: {
primary: {
labelText: 'string',
redirectUrl: 'string',
show: 'boolean'
},
secondary: {
labelText: 'string',
redirectUrl: 'string',
show: 'boolean'
}
},
timestamps: {
mode: 'string',
autoStart: 'boolean',
startTimestamp: 'number',
endTimestamp: 'number',
show: 'boolean'
},
party: {
id: 'string',
size: {
currentSize: 'number',
maxSize: 'number'
},
show: 'boolean'
}
}
};
return validateObject(config, schema);
}
// Validate config values
const validateValues = (config) => {
validateDetails(config.rich_presence.details);
validateState(config.rich_presence.state);
validateImage(config.rich_presence.assets.largeImage, "LargeImage");
validateImage(config.rich_presence.assets.smallImage, "SmallImage");
validateButton(config.rich_presence.buttons.primary, "Primary");
validateButton(config.rich_presence.buttons.secondary, "Secondary");
validateTimestamps(config.rich_presence.timestamps);
validateParty(config.rich_presence.party);
}
const validateDetails = (details) => {
if (details.show) {
if (details && details.text) {
if (details.text.length > 128) {
logError("Details provided exceeds the maximum character length of 128.");
} else if (details.text.length < 2) {
logError("Details provided does not meet the minimum character length of 2.");
}
}
}
};
const validateState = (state) => {
if (state.show) {
if (state && state.text) {
if (state.text.length > 128) {
logError("State provided exceeds the maximum character length of 128.");
} else if (state.text.length < 2) {
logError("State provided does not meet the minimum character length of 2.");
}
}
}
};
const validateImage = (largeImage, imagetype) => {
if (largeImage.show) {
if (largeImage.text.length > 128) {
logError(`${imagetype}Text provided exceeds the maximum character length of 128.`);
} else if (largeImage.text.length < 2) {
logError(`${imagetype}Text provided does not meet the minimum character length of 2.`);
}
if (largeImage.key.length > 256) {
logError(`${imagetype}Key provided exceeds the maximum character length of 256.`);
}
}
};
const validateButton = (button, buttonType) => {
if (button.show) {
if (button.labelText.length > 32) {
logError(`${buttonType} button label provided exceeds the maximum character length of 32.`);
} else if (button.labelText.length < 1) {
logError(`${buttonType} button label provided does not meet the minimum character length of 1.`);
}
if (button.redirectUrl.length > 512) {
logError(`${buttonType} button URL provided exceeds the maximum character length of 512.`);
} else if (button.redirectUrl.length < 1) {
logError(`${buttonType} button URL provided does not meet the minimum character length of 1.`);
}
if (!/^https?:\/\//.test(button.redirectUrl)) {
logError(`${buttonType} button URL provided does not contain either 'http://' OR 'https://'.`);
}
}
}
const validateTimestamps = (timestamps) => {
if (timestamps.show) {
if (timestamps) {
const validModes = ["remaining", "elapsed"];
if (timestamps.mode && !validModes.includes(timestamps.mode)) {
logError("Invalid mode provided for timestamps.");
}
if (timestamps.startTimestamp !== undefined) {
if (typeof timestamps.startTimestamp !== 'number') {
logError("startTimestamp should be a number.");
}
}
if (timestamps.endTimestamp !== undefined) {
if (typeof timestamps.endTimestamp !== 'number') {
logError("endTimestamp should be a number.");
}
}
}
}
};
const validateParty = (party) => {
if (party.show) {
if (party) {
if (!party.id) {
logError("Party id is required.");
}
if (party.size) {
if (typeof party.size.currentSize !== 'number' || typeof party.size.maxSize !== 'number') {
logError("Party size should be numbers.");
}
}
}
}
};
export const validateConfig = (config) => {
console.log(chalk.blue("Validating configuration..."));
const schemaValidResult = validateConfigSchema(config);
if (schemaValidResult) {
logError(schemaValidResult);
} else {
console.log(chalk.green("Configuration Schema is valid!"));
}
const configValidResult = validateValues(config);
if (configValidResult) {
logError(configValidResult);
} else {
console.log(chalk.green("Configuration values are valid!"));
}
}
const logError = (message) => {
console.error(chalk.red.bold(message));
process.exit(1);
};