mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-27 08:50:56 +01:00
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import type { ZodTypeAny } from "zod";
|
|
import { z } from "zod";
|
|
|
|
import type { SearchEngineType } from "@homarr/definitions";
|
|
|
|
const genericSearchEngine = z.object({
|
|
type: z.literal("generic" satisfies SearchEngineType),
|
|
urlTemplate: z.string().min(1).startsWith("http").includes("%s"),
|
|
});
|
|
|
|
const fromIntegrationSearchEngine = z.object({
|
|
type: z.literal("fromIntegration" satisfies SearchEngineType),
|
|
integrationId: z.string().optional(),
|
|
});
|
|
|
|
const manageSearchEngineSchema = z.object({
|
|
name: z.string().min(1).max(64),
|
|
short: z.string().min(1).max(8),
|
|
iconUrl: z.string().min(1),
|
|
description: z.string().max(512).nullable(),
|
|
});
|
|
|
|
const createManageSearchEngineSchema = <T extends ZodTypeAny>(
|
|
callback: (schema: typeof manageSearchEngineSchema) => T,
|
|
) =>
|
|
z
|
|
.discriminatedUnion("type", [genericSearchEngine, fromIntegrationSearchEngine])
|
|
.and(callback(manageSearchEngineSchema));
|
|
|
|
const editSearchEngineSchema = createManageSearchEngineSchema((schema) =>
|
|
schema
|
|
.extend({
|
|
id: z.string(),
|
|
})
|
|
.omit({ short: true }),
|
|
);
|
|
|
|
export const searchEngineSchemas = {
|
|
manage: createManageSearchEngineSchema((schema) => schema),
|
|
edit: editSearchEngineSchema,
|
|
};
|