Files
Homarr/packages/validation/src/search-engine.ts
Meier Lukas 75ba3f2ae7 refactor: remove central validation export to improve typescript performance (#2810)
* refactor: remove central validation export to improve typescript performance

* fix: missing package exports change in validation package

* chore: address pull request feedback
2025-04-06 10:37:28 +00:00

39 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"), // Only allow http and https for security reasons (javascript: is not allowed)
});
const fromIntegrationSearchEngine = z.object({
type: z.literal("fromIntegration" satisfies SearchEngineType),
integrationId: z.string().optional(),
});
const baseSearchEngineManageSchema = 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 baseSearchEngineManageSchema) => T,
) =>
z
.discriminatedUnion("type", [genericSearchEngine, fromIntegrationSearchEngine])
.and(callback(baseSearchEngineManageSchema));
export const searchEngineManageSchema = createManageSearchEngineSchema((schema) => schema);
export const searchEngineEditSchema = createManageSearchEngineSchema((schema) =>
schema
.extend({
id: z.string(),
})
.omit({ short: true }),
);