forked from fahrPool/fahrPool
57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import type { Role } from "@/lib/auth/roles";
|
|
|
|
const GUELTIGE_ROLLEN: Role[] = ["admin", "disponent", "fahrer", "readonly"];
|
|
|
|
export type RolleValidationResult = { valid: true; rolle: Role } | { valid: false; error: string };
|
|
|
|
export function validateRolle(input: FormDataEntryValue | null): RolleValidationResult {
|
|
if (typeof input !== "string" || !GUELTIGE_ROLLEN.includes(input as Role)) {
|
|
return { valid: false, error: "Bitte eine gueltige Rolle auswaehlen." };
|
|
}
|
|
return { valid: true, rolle: input as Role };
|
|
}
|
|
|
|
export type NutzerErstellenValidationResult =
|
|
| { valid: true; name: string; email: string; passwort: string; rolle: Role }
|
|
| { valid: false; error: string };
|
|
|
|
function parseText(value: FormDataEntryValue | null): string | null {
|
|
if (typeof value !== "string" || value.trim() === "") {
|
|
return null;
|
|
}
|
|
return value.trim();
|
|
}
|
|
|
|
export function validateNutzerErstellen(input: {
|
|
name: FormDataEntryValue | null;
|
|
email: FormDataEntryValue | null;
|
|
passwort: FormDataEntryValue | null;
|
|
rolle: FormDataEntryValue | null;
|
|
}): NutzerErstellenValidationResult {
|
|
const name = parseText(input.name);
|
|
if (!name) {
|
|
return { valid: false, error: "Bitte einen Namen eingeben." };
|
|
}
|
|
|
|
const email = parseText(input.email);
|
|
if (!email || !email.includes("@")) {
|
|
return { valid: false, error: "Bitte eine gueltige E-Mail-Adresse eingeben." };
|
|
}
|
|
|
|
const passwort = typeof input.passwort === "string" ? input.passwort : "";
|
|
if (passwort.length < 8) {
|
|
return { valid: false, error: "Das Passwort muss mindestens 8 Zeichen lang sein." };
|
|
}
|
|
|
|
const rolleValidation = validateRolle(input.rolle);
|
|
if (!rolleValidation.valid) {
|
|
return rolleValidation;
|
|
}
|
|
|
|
return { valid: true, name, email, passwort, rolle: rolleValidation.rolle };
|
|
}
|
|
|
|
export function istSelbstschutzVerletzt(eigeneId: string, zielId: string): boolean {
|
|
return eigeneId === zielId;
|
|
}
|