mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-07 07:09:21 +01:00
* refactor: remove central validation export to improve typescript performance * fix: missing package exports change in validation package * chore: address pull request feedback
39 lines
1.2 KiB
TypeScript
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 }),
|
|
);
|