songs page refactor schema
This commit is contained in:
38
src/routes/songs/schema.ts
Normal file
38
src/routes/songs/schema.ts
Normal 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
|
||||
};
|
||||
Reference in New Issue
Block a user