1
0
Fork 0

feat: add fahrt validation helpers for km and required photos

This commit is contained in:
Jonny 2026-07-05 17:20:40 +02:00
parent 4407c6de0c
commit 225504f3a9
3 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,12 @@
export const PFLICHT_FOTO_POSITIONEN = [
"front",
"seitlich_rechts",
"rechte_seite",
"seitlich_rechts_heck",
"heck",
"seitlich_links_heck",
"linke_seite",
"seitlich_vorne_links",
] as const;
export type PflichtFotoPosition = (typeof PFLICHT_FOTO_POSITIONEN)[number];

View File

@ -0,0 +1,61 @@
import { describe, expect, it } from "vitest";
import { validateFahrtBeginn, validateFahrtEnde } from "./validate-fahrt";
import { PFLICHT_FOTO_POSITIONEN } from "./foto-positionen";
describe("validateFahrtBeginn", () => {
it("akzeptiert gueltigen KM-Stand mit allen 8 Pflichtfotos", () => {
const result = validateFahrtBeginn({
startKm: "12345",
vorhandenePositionen: [...PFLICHT_FOTO_POSITIONEN],
});
expect(result).toEqual({ valid: true, startKm: 12345 });
});
it("lehnt einen fehlenden KM-Stand ab", () => {
const result = validateFahrtBeginn({
startKm: "",
vorhandenePositionen: [...PFLICHT_FOTO_POSITIONEN],
});
expect(result).toEqual({ valid: false, error: "Bitte einen KM-Stand eingeben." });
});
it("lehnt einen negativen KM-Stand ab", () => {
const result = validateFahrtBeginn({
startKm: "-5",
vorhandenePositionen: [...PFLICHT_FOTO_POSITIONEN],
});
expect(result).toEqual({ valid: false, error: "Der KM-Stand darf nicht negativ sein." });
});
it("lehnt fehlende Pflichtfotos ab und benennt die fehlenden Positionen", () => {
const result = validateFahrtBeginn({
startKm: "12345",
vorhandenePositionen: ["front", "heck"],
});
expect(result).toEqual({
valid: false,
error:
"Es fehlen noch 6 Pflichtfoto(s): seitlich_rechts, rechte_seite, seitlich_rechts_heck, seitlich_links_heck, linke_seite, seitlich_vorne_links",
});
});
});
describe("validateFahrtEnde", () => {
it("akzeptiert einen End-KM-Stand groesser als der Start-KM-Stand", () => {
const result = validateFahrtEnde({ endKm: "12500", startKm: 12345 });
expect(result).toEqual({ valid: true, endKm: 12500 });
});
it("lehnt einen fehlenden End-KM-Stand ab", () => {
const result = validateFahrtEnde({ endKm: "", startKm: 12345 });
expect(result).toEqual({ valid: false, error: "Bitte einen End-KM-Stand eingeben." });
});
it("lehnt einen End-KM-Stand kleiner als der Start-KM-Stand ab", () => {
const result = validateFahrtEnde({ endKm: "100", startKm: 12345 });
expect(result).toEqual({
valid: false,
error: "Der End-KM-Stand darf nicht kleiner als der Start-KM-Stand (12345) sein.",
});
});
});

View File

@ -0,0 +1,58 @@
import { PFLICHT_FOTO_POSITIONEN } from "./foto-positionen";
export type FahrtBeginnValidationResult =
| { valid: true; startKm: number }
| { valid: false; error: string };
export type FahrtEndeValidationResult =
| { valid: true; endKm: number }
| { valid: false; error: string };
function parseKm(value: FormDataEntryValue | null): number | null {
if (typeof value !== "string" || value.trim() === "") {
return null;
}
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
}
export function validateFahrtBeginn(input: {
startKm: FormDataEntryValue | null;
vorhandenePositionen: string[];
}): FahrtBeginnValidationResult {
const startKm = parseKm(input.startKm);
if (startKm === null) {
return { valid: false, error: "Bitte einen KM-Stand eingeben." };
}
if (startKm < 0) {
return { valid: false, error: "Der KM-Stand darf nicht negativ sein." };
}
const vorhanden = new Set(input.vorhandenePositionen);
const fehlend = PFLICHT_FOTO_POSITIONEN.filter((position) => !vorhanden.has(position));
if (fehlend.length > 0) {
return {
valid: false,
error: `Es fehlen noch ${fehlend.length} Pflichtfoto(s): ${fehlend.join(", ")}`,
};
}
return { valid: true, startKm };
}
export function validateFahrtEnde(input: {
endKm: FormDataEntryValue | null;
startKm: number;
}): FahrtEndeValidationResult {
const endKm = parseKm(input.endKm);
if (endKm === null) {
return { valid: false, error: "Bitte einen End-KM-Stand eingeben." };
}
if (endKm < input.startKm) {
return {
valid: false,
error: `Der End-KM-Stand darf nicht kleiner als der Start-KM-Stand (${input.startKm}) sein.`,
};
}
return { valid: true, endKm };
}