songs page refactor schema

This commit is contained in:
2026-02-06 08:51:01 -08:00
parent 0531a1f5c0
commit 4b58d71b7c
3 changed files with 44 additions and 63 deletions

View File

@@ -0,0 +1,38 @@
import { z } from "zod";
import { SongTypeMap } from "$lib/utils/amq";
// Base schema for raw URL search parameters as strings
const BaseSearchParamsSchema = z.object({
q: z.string().optional(),
artist: z.string().optional(),
anime: z.string().optional(),
gpm: z.string().optional(),
gpx: z.string().optional(),
cat: z.string().optional(),
});
// Schema for +page.ts load function (parses comma-separated types into number array)
export const LoadSearchParamsSchema = BaseSearchParamsSchema.extend({
type: z
.string()
.optional()
.transform((s) => {
if (!s) return undefined;
return s
.split(",")
.map((t) => SongTypeMap[t.trim().toUpperCase()])
.filter((n) => n !== undefined);
}),
}).strict();
// Schema for +page.svelte useSearchParams (handles 'type' as an array of strings from URL)
export const SvelteSearchParamsSchema = BaseSearchParamsSchema.extend({
type: z.array(z.string()).default([]),
}).strict();
// Define the type for the Svelte form, which will have default values
export type SvelteSearchForm = z.infer<typeof SvelteSearchParamsSchema> & {
gpm: string; // To allow empty string in form input
gpx: string; // To allow empty string in form input
cat: string; // To allow empty string in form input
};