Add static checking to i18n and improve types

This commit is contained in:
Bad Manners 2024-07-24 21:48:46 -03:00
parent f8ac450ab5
commit 579e5879e1
16 changed files with 126 additions and 82 deletions

View file

@ -1,7 +1,6 @@
import { defineCollection, reference, z } from "astro:content";
export const adjustDateForUTCOffset = (date: Date) =>
new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
// Constants
export const WEBSITE_LIST = [
"website",
@ -15,6 +14,10 @@ export const WEBSITE_LIST = [
"bluesky",
"itaku",
] as const;
export const GAME_PLATFORMS = ["web", "windows", "linux", "macos", "android", "ios"] as const;
export const DEFAULT_LANG = "eng";
// Validators
const ekaPostUrlRegex = /^(?:https:\/\/)(?:www\.)?aryion\.com\/g4\/view\/([1-9]\d*)\/?$/;
const furaffinityPostUrlRegex = /^(?:https:\/\/)(?:www\.)?furaffinity\.net\/view\/([1-9]\d*)\/?$/;
@ -27,9 +30,16 @@ const mastodonPostUrlRegex = /^(?:https:\/\/)((?:[a-zA-Z0-9_-]+\.)+[a-z]+)\/@([a
const refineAuthors = (value: { id: any } | any[]) => "id" in value || value.length > 0;
const refineCopyrightedCharacters = (value: Record<string, any>) => !("" in value) || Object.keys(value).length == 1;
const lang = z.enum(["eng", "tok"]).default("eng");
// Transformers
export const adjustDateForUTCOffset = (date: Date) =>
new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
// Types
const lang = z.enum(["eng", "tok"]).default(DEFAULT_LANG);
const website = z.enum(WEBSITE_LIST);
const platform = z.enum(["web", "windows", "linux", "macos", "android", "ios"]);
const platform = z.enum(GAME_PLATFORMS);
const mastodonPost = z
.object({
instance: z.string(),
@ -60,8 +70,11 @@ const copyrightedCharacters = z
export type Lang = z.output<typeof lang>;
export type Website = z.infer<typeof website>;
export type GamePlatform = z.infer<typeof platform>;
export type CopyrightedCharacters = z.infer<typeof copyrightedCharacters>;
// Content collections
const storiesCollection = defineCollection({
type: "content",
schema: ({ image }) =>
@ -144,6 +157,8 @@ const gamesCollection = defineCollection({
}),
});
// Data collections
const usersCollection = defineCollection({
type: "data",
schema: ({ image }) =>
@ -186,7 +201,7 @@ const tagCategoriesCollection = defineCollection({
z.object({
name: z.union([
z.string(),
z.record(lang, z.string()).refine((tag) => "eng" in tag, `object-formatted tag must have an "eng" key`),
z.intersection(z.object({ [DEFAULT_LANG]: z.string() }), z.record(lang, z.string())),
]),
description: z.string().optional(),
related: z.array(z.string()).optional(),
@ -196,10 +211,8 @@ const tagCategoriesCollection = defineCollection({
});
export const collections = {
// Content collections
stories: storiesCollection,
games: gamesCollection,
// Data collections
users: usersCollection,
series: seriesCollection,
"tag-categories": tagCategoriesCollection,