zod schema and hook
This commit is contained in:
1 parent
a2dfbd29c7
commit
0941f3ce1a
2 files changed
+72
No files matched your search
@@ -0,0 +1,35 @@
|
|||||||
|
import { useMemo } from "react";
|
||||||
|
import { settingsSchema } from "@/Validation/settings";
|
||||||
|
import type { Settings } from "@/Types/Settings";
|
||||||
|
import type { SettingsFormData } from "@/Validation/settings";
|
||||||
|
|
||||||
|
interface UseSettingsFormOptions {
|
||||||
|
data?: Settings | null;
|
||||||
|
}
|
||||||
|
export const useSettingsForm = ({ data = null }: UseSettingsFormOptions = {}) => {
|
||||||
|
return useMemo(() => {
|
||||||
|
const defaults: SettingsFormData = {
|
||||||
|
systemEmailIgnoreTLS: data?.systemEmailIgnoreTLS || false,
|
||||||
|
systemEmailRequireTLS: data?.systemEmailRequireTLS || false,
|
||||||
|
systemEmailRejectUnauthorized: data?.systemEmailRejectUnauthorized || true,
|
||||||
|
systemEmailConnectionHost: data?.systemEmailConnectionHost || "",
|
||||||
|
systemEmailSecure: data?.systemEmailSecure || true,
|
||||||
|
systemEmailPool: data?.systemEmailPool || false,
|
||||||
|
showURL: data?.showURL || false,
|
||||||
|
//timezone: data?.timezone || "UTC",
|
||||||
|
language: data?.language || "en",
|
||||||
|
//chartType: data?.chartType || "histogram",
|
||||||
|
checkTTL: data?.checkTTL || 30,
|
||||||
|
pagespeedApiKey: data?.pagespeedApiKey || "",
|
||||||
|
systemEmailHost: data?.systemEmailHost || "",
|
||||||
|
systemEmailPort: data?.systemEmailPort || "",
|
||||||
|
systemEmailAddress: data?.systemEmailAddress || "",
|
||||||
|
systemEmailUser: data?.systemEmailUser || "",
|
||||||
|
systemEmailPassword: data?.systemEmailPassword || "",
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const settingsSchema = z.object({
|
||||||
|
systemEmailIgnoreTLS: z.boolean(),
|
||||||
|
systemEmailRequireTLS: z.boolean(),
|
||||||
|
systemEmailRejectUnauthorized: z.boolean(),
|
||||||
|
systemEmailConnectionHost: z.string().optional().or(z.literal("")),
|
||||||
|
systemEmailSecure: z.boolean().optional(),
|
||||||
|
systemEmailPool: z.boolean().optional(),
|
||||||
|
showURL: z.boolean().optional(),
|
||||||
|
timezone: z.string().min(1, "Timezone is required").optional(),
|
||||||
|
mode: z.enum(["light", "dark"]).optional(),
|
||||||
|
language: z.string().min(2, "Language is required"),
|
||||||
|
chartType: z.enum(["histogram", "heatmap"]).optional(),
|
||||||
|
checkTTL: z.coerce.number().int().min(1, "Please enter a value"),
|
||||||
|
pagespeedApiKey: z.string().optional().or(z.literal("")),
|
||||||
|
systemEmailHost: z.string().optional().or(z.literal("")),
|
||||||
|
systemEmailPort: z.coerce.number().nullable().optional().or(z.literal("")),
|
||||||
|
systemEmailAddress: z
|
||||||
|
.email("Please enter a valid email address")
|
||||||
|
.transform((val) => val.toLowerCase().trim())
|
||||||
|
.optional()
|
||||||
|
.or(z.literal("")),
|
||||||
|
systemEmailUser: z.string().optional().or(z.literal("")),
|
||||||
|
systemEmailPassword: z.string().optional().or(z.literal("")),
|
||||||
|
systemEmailTLSServername: z.string().optional().or(z.literal("")),
|
||||||
|
globalThresholds: z
|
||||||
|
.object({
|
||||||
|
cpu: z.coerce.number().min(1).max(100).optional(),
|
||||||
|
memory: z.coerce.number().min(1).max(100).optional(),
|
||||||
|
disk: z.coerce.number().min(1).max(100).optional(),
|
||||||
|
temperature: z.coerce.number().min(1).max(150).optional(),
|
||||||
|
})
|
||||||
|
.optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type SettingsFormData = z.infer<typeof settingsSchema>;
|
||||||
Reference in new issue
Block a user