test(persons): extract validatePersonFields and cover validation branches
- New src/lib/person-validation.ts exports validatePersonFields (pure function) - 8 unit tests covering: valid PERSON, lastName missing/undefined, firstName missing/undefined for PERSON, non-PERSON types without firstName - Both edit and new-person server actions now call the shared helper instead of inline if-chains, making the logic testable and non-duplicated Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { error, fail, redirect } from '@sveltejs/kit';
|
||||
import { createApiClient } from '$lib/api.server';
|
||||
import { getErrorMessage } from '$lib/errors';
|
||||
import { validatePersonFields } from '$lib/person-validation';
|
||||
|
||||
type PersonType = 'PERSON' | 'INSTITUTION' | 'GROUP' | 'UNKNOWN' | 'SKIP';
|
||||
|
||||
@@ -51,11 +52,9 @@ export const actions = {
|
||||
const birthYear = birthYearStr ? parseInt(birthYearStr, 10) : undefined;
|
||||
const deathYear = deathYearStr ? parseInt(deathYearStr, 10) : undefined;
|
||||
|
||||
if (!lastName) {
|
||||
return fail(400, { updateError: 'Nachname ist Pflichtfeld.' });
|
||||
}
|
||||
if (personType === 'PERSON' && !firstName) {
|
||||
return fail(400, { updateError: 'Vorname ist Pflichtfeld.' });
|
||||
const validationError = validatePersonFields(personType, firstName, lastName);
|
||||
if (validationError) {
|
||||
return fail(400, { updateError: validationError });
|
||||
}
|
||||
|
||||
const api = createApiClient(fetch);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { error, fail, redirect } from '@sveltejs/kit';
|
||||
import { createApiClient } from '$lib/api.server';
|
||||
import { getErrorMessage } from '$lib/errors';
|
||||
import { validatePersonFields } from '$lib/person-validation';
|
||||
|
||||
export async function load({ locals }: { locals: App.Locals }) {
|
||||
const canWrite =
|
||||
@@ -26,23 +27,14 @@ export const actions = {
|
||||
const deathYearStr = formData.get('deathYear')?.toString().trim();
|
||||
const notes = formData.get('notes')?.toString().trim() || undefined;
|
||||
|
||||
if (!lastName) {
|
||||
const validationError = validatePersonFields(personType, firstName, lastName);
|
||||
if (validationError) {
|
||||
return fail(400, {
|
||||
error: 'Nachname ist Pflichtfeld.',
|
||||
error: validationError,
|
||||
personType,
|
||||
title,
|
||||
firstName,
|
||||
lastName: '',
|
||||
alias
|
||||
});
|
||||
}
|
||||
if (personType === 'PERSON' && !firstName) {
|
||||
return fail(400, {
|
||||
error: 'Vorname ist Pflichtfeld.',
|
||||
personType,
|
||||
title,
|
||||
firstName: '',
|
||||
lastName,
|
||||
firstName: firstName ?? '',
|
||||
lastName: lastName ?? '',
|
||||
alias
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user