-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.ts
278 lines (262 loc) · 7.42 KB
/
config.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// What is this file?
// Perhaps you had a weird error while editing the config sending you here
// if that was the case you probably messed up your config, this file makes sure the config is properly written
// if you are a developer and plan on editing this file, do so carefully
// this file is the single source of truth for our configs and types
// A note about zod, we could just define the types with zod but we define them twice
// we then check the configuration against our typescript types, this makes it easier to
// ensure our typescript values are what we want, additionally this allows for strict checks with looser parent types
// An additional note, do not use `z.object` instead use `z.strictObject` or else we are not fully validating the values
import z, { type ZodError } from 'zod';
import { fromZodError } from 'zod-validation-error';
// Import Config
import rawConfig from './config.yaml';
// General Types
export interface ImageDescription {
src: string;
alt: string;
}
const imageDescription = z.strictObject({
src: z.string(),
alt: z.string(),
});
// Section
export enum SectionType {
TextSection = 'TextSection',
NewsSection = 'NewsSection',
EventSection = 'EventSection',
}
interface SectionBase {
// section_type: SectionType; -- We cannot have this here because of the whole filtering by sections stuff, but it is necessary on each type
section_header: string;
}
const sectionBase = z.strictObject({
section_type: z.nativeEnum(SectionType),
section_header: z.string(),
});
// Config Types
interface SocialIcon {
alt_text: string;
link: string;
path: string;
}
const socialIcon = z.strictObject({
alt_text: z.string(),
link: z.string(),
path: z.string(),
});
interface MetaConfig {
title: string;
description: string;
}
const metaConfig = z.strictObject({
title: z.string(),
description: z.string(),
});
interface WebsiteConfig {
title: string;
meta: MetaConfig;
hackathon_url: string;
email: string;
discord: string;
instagram: string;
linkedin: string;
tagline: string;
social_icons: SocialIcon[];
banner_text?: string;
}
const websiteConfig = z.strictObject({
title: z.string(),
meta: metaConfig,
hackathon_url: z.string(),
email: z.string(),
discord: z.string(),
instagram: z.string(),
linkedin: z.string(),
tagline: z.string(),
social_icons: z.array(socialIcon),
banner_text: z.optional(z.string()),
});
interface PageItem {
page_name: string;
page_link: string;
display_in_navbar: boolean;
}
const pageItem = z.strictObject({
page_name: z.string(),
page_link: z.string(),
display_in_navbar: z.boolean(),
});
interface FooterConfig {
text: string;
}
const footerConfig = z.strictObject({
text: z.string(),
});
// Events
export interface EventItem {
title: string;
href: string;
main_event: boolean;
start_date: Date;
end_date: Date;
image: ImageDescription;
location: string;
}
const eventItem = z
.strictObject({
title: z.string(),
href: z.string(),
main_event: z.boolean().optional().default(false),
start_date: z.date(),
end_date: z.date(),
image: imageDescription,
location: z.string(),
})
.refine(
({ start_date, end_date }) => {
if (end_date < start_date) return false;
return true;
},
{
message: 'Event ends before it starts.',
path: ['events'],
}
);
// Sections
export interface TextSection extends SectionBase {
section_type: SectionType.TextSection;
text: string;
image: ImageDescription;
button?: {
text: string;
href: string;
};
}
const textSection = sectionBase.extend({
section_type: z.literal(SectionType.TextSection),
text: z.string(),
image: imageDescription,
button: z
.strictObject({
text: z.string(),
href: z.string(),
})
.optional(),
});
export interface NewsItem {
text: string;
date: Date;
location?: string;
href?: string;
}
const newsItem = z.strictObject({
text: z.string(),
date: z.date(),
location: z.string().optional(),
href: z.string().optional(),
});
export interface NewsSection extends SectionBase {
section_type: SectionType.NewsSection;
news_feed: NewsItem[];
}
const newsSection = sectionBase.extend({
section_type: z.literal(SectionType.NewsSection),
news_feed: z.array(newsItem),
});
export enum EventGridStyle {
/**
* All events are shown in a grid.
*/
Grid = 'EventGrid',
/**
* 3 Events are shown in a regular list.
*/
List = 'EventList',
/**
* 3 Events are shown in a list, but only future events are shown.
*/
HomeList = 'HomeList',
}
const eventGridStyle = z.nativeEnum(EventGridStyle);
export interface EventSection extends SectionBase {
section_type: SectionType.EventSection;
grid_style: EventGridStyle;
events: EventItem[];
}
const eventSection = sectionBase.extend({
section_type: z.literal(SectionType.EventSection),
grid_style: eventGridStyle,
events: z.array(eventItem).refine(
(events: EventItem[]) => {
// Ensure we can only have one main_event
let foundMainEvent = false;
for (const event of events) {
if (event.main_event) {
if (foundMainEvent) return false;
foundMainEvent = true;
}
}
return true;
},
{
message: 'Only one event can be designated as the main event.',
path: ['events'],
}
),
});
export type Section = TextSection | NewsSection | EventSection;
const section = z.union([textSection, newsSection, eventSection]);
// Home page
interface HomePage {
sections: Section[];
}
const homePage = z.strictObject({
sections: z.array(section),
});
// config
interface ValidConfig {
website_config: WebsiteConfig;
page_list: PageItem[];
footer_config: FooterConfig;
// Page Configs
home_page: HomePage;
events: EventItem[];
}
const configValidator = z.strictObject({
website_config: websiteConfig,
page_list: z.array(pageItem),
footer_config: footerConfig,
home_page: homePage,
events: z.array(eventItem),
});
// Config Section Spaced out for an easier error
// =========================================================================
function formatConfigError(error: ZodError) {
const formattedError = fromZodError(error, {
prefix: 'Configuration error',
prefixSeparator: ': ',
});
return `${formattedError.message}\n\nPlease check your config.yaml file at the following location(s):
${error.errors.map((err) => ` - ${err.path.join('.')}`).join('\n')}`;
}
function validateConfig(rawConfig: unknown): ValidConfig {
const result = configValidator.safeParse(rawConfig);
if (!result.success) {
const errorMessage = formatConfigError(result.error);
throw new Error(errorMessage);
}
return result.data;
}
const validatedConfig: ValidConfig = validateConfig(rawConfig);
// =========================================================================
// ======== Preprocessing if needed ========
type Config = ValidConfig; // if you need todo preprocessing, replace this line with the commented out one below, replace home_page with whatever field you want to replace
// interface Config extends Omit<ValidConfig, 'home_page'> {}
const config: Config = validatedConfig; // if you need todo preprocessing, this is where
// Exports - to ensure our configuration is written well, we export each top level field directly
export const website_config = config.website_config;
export const footer_config = config.footer_config;
export const page_list = config.page_list;
export const home_page = config.home_page;
export const events = config.events;